4

I added Lua scripting to my C# Application by using the DynamicLua libary and it works very well. I would like to implement that you get the current line which is being executed (Like in Visual Studio) and highlight it.

Currently I am doing this:

   public static void RunLua(string LuaToExecute)
    {
        dynamic lua = new DynamicLua.DynamicLua();

        string[] lua_s_split = LuaToExecute.Split('\n');
        int counter = 0;
        foreach (string line in lua_s_split)
        {
            // highlight current line in editor
            HighlightLine(counter + 1);

            //execute current line 
            lua(line);
            counter++;
        }
    }

This is working great with my Lua code like

move(20, 19)
sleep(1000)
move(5, 19)

but I cant only execute one line statements. Like my bound function move(). But I would also like to use multiline statements like functions and loops. If the text editor contains:

function test()
    return  "Hallo Welt"
end

The lua(line) will raise an exception because only the first line function test() is passed and the interpreter is missing the end statement.

What can I do? Should I check if the line begins with an function,while... command and than scan for end blocks and add it to a string so I can execute and highlight this multiline statement all at one? Would this possible? How would I do it?

Please Help.

pixelport
  • 83
  • 1
  • 6

2 Answers2

5

I would like to implement that you get the current line which is being executed (Like in Visual Studio) and highlight it.

Don't do this by feeding Lua the script a line at a time. Run the entire script and let Lua notify you when execution switches to a new line, via a debug hook:

Programming in Lua: Hooks
Lua manual: debug.sethook

Mud
  • 28,277
  • 11
  • 59
  • 92
  • Awesome! Thanks! Didn't knew this method. So I can just bound my own `_setHighlightLine` function to the dynamic Lua object. Set the Lua hook and my function will get called automatically on each new line lua executes. Will try it out. Thanks. – pixelport Jun 21 '14 at 10:38
2

[EDIT] At the time of this answer, I was unaware of the LUA parser mentioned in the accepted answer. I am agreed with that poster that you should use official parsing libraries wherever possible rather than rolling your own. The below answers the original question, but should not be seen as the ultimate answer. Please view the accepted answer for the correct method to handle this problem.[/EDIT]

You're explicitly calling it to parse the line of code / execute the LUA.

If you have a multiline function, ensure you're passing the full thing before calling the execution block.

var myCommand = new StringBuilder()

... myCommand.Append(line) ...

foreach (string line in lua_s_split)
    {
        // highlight current line in editor
        HighlightLine(counter + 1);

        //execute current line 
        If(NeedsToExecute(istrue))
        {
            lua(myCommand.ToString());
            counter++;
        }
        else{myCommand.appendline(line)
    }
Matches
  • 131
  • 8
  • Thank you for answering, this was exactly my thought. But how do I check if I can execute the line or need to add more lines? Scanning for keywords like "while", "function" etc.? Or adding and executing the next line until I got no exception? What should I do? – pixelport Jun 20 '14 at 20:45
  • Entirely depends on your implementation, I'm afraid. You should know when you're getting ready to send a multi line command, so maybe something like, if(beginning of line.startswith(¤¤¤)) continue read until line terminator of /¤¤¤ is encountered. -- You might consider using a slightly more advanced technique, like passing JSON data, instead of just blindly splitting on new lines. Then you will have an easier time of specifying the full line. – Matches Jun 20 '14 at 21:31
  • 2
    @user2414724 This is a bad approach in general. By the time you've handled every corner case you'll have written a full blown Lua parser. Lua already has a parser, and a standard library for doing exactly what you're trying to do. – Mud Jun 21 '14 at 06:25
  • 1
    I don't have any experience with LUA parsers, and was just replying to the original thread question - I will agree after seeing the other option that it is much better. I'll edit my answer to recommend looking to the actual parser (the accepted answer) rather than this method. – Matches Jun 24 '14 at 18:12