This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Wilma456's profile picture

Load unfinished function

Started by Wilma456, 11 September 2016 - 11:05 AM
Wilma456 #1
Posted 11 September 2016 - 01:05 PM
I'm working on a programm, who read a file line by line and run the content of the file with load(). But theres a problem: A function is in a file normaly in more than one line. For example:

function test()
print("test")
end
If load() only get "function test()" it gaves a error, that end exeptet. Why can I make this work?
KingofGamesYami #2
Posted 11 September 2016 - 01:38 PM
Don't run individual lines of a file. This will screw up more than functions, local variables will not work. Since local variables are considered good practice, this will break the majority of programs.


What do you intend to accomplish by running individual lines of a file?
Wilma456 #3
Posted 11 September 2016 - 01:50 PM
I want to make a debugger with run a Programm line by line and show, waht the Program does and read/write Variables.
TYKUHN2 #4
Posted 11 September 2016 - 03:57 PM
Warning: Psuedocode detected!


count = 1
everyOther = false
debugLine = "<some code here>"
load(readFunction)
function readFunction(file)
   if not everyOther then
	   line = readLine(count)
	   everyOther = true
	   return line
   else
       everyOther = false
       count = count + 1
	   return debugLine
   end
end
Edited on 11 September 2016 - 02:00 PM
KingofGamesYami #5
Posted 11 September 2016 - 07:17 PM
I want to make a debugger with run a Programm line by line and show, waht the Program does and read/write Variables.

This is, unfortunately, impractical within CC. It would work for only the simplest of programs - those without loops or local variables, or any number of things.

IIRC, the debug library is included with CCTweaks. That may be worth looking into if you want to make a proper debugging tool.
H4X0RZ #6
Posted 11 September 2016 - 10:47 PM
One way to log all the functions called would be to set a custom env for the file which has an __index that wraps every requested function inside another one which then logs the stuff, or you return the original function and just log that the function has been accessed.
Lupus590 #7
Posted 12 September 2016 - 02:10 PM
There are some debuggers on the forums already, here are a few from my bookmarks:
http://www.computerc...ps-like-a-boss/
http://www.computerc...-file-extracts/
http://www.computerc...-step-debugger/
http://www.computerc...k-trace-for-cc/

You may want to use them as reference.
Edited on 12 September 2016 - 12:10 PM