Deprecated: The behavior of unparenthesized expressions containing both '.' and '+'/'-' will change in PHP 8: '+'/'-' will take a higher precedence in /home/iano/public_html/tpforums-vb5/forum/includes/class_core.php on line 5842

PHP Warning: Use of undefined constant MYSQL_NUM - assumed 'MYSQL_NUM' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: Use of undefined constant MYSQL_ASSOC - assumed 'MYSQL_ASSOC' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: Use of undefined constant MYSQL_BOTH - assumed 'MYSQL_BOTH' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in ..../includes/functions_navigation.php on line 588

PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in ..../includes/functions_navigation.php on line 612

PHP Warning: Use of undefined constant misc - assumed 'misc' (this will throw an Error in a future version of PHP) in ..../global.php(29) : eval()'d code(6) : eval()'d code on line 1

PHP Warning: Use of undefined constant index - assumed 'index' (this will throw an Error in a future version of PHP) in ..../global.php(29) : eval()'d code(6) : eval()'d code on line 1

PHP Warning: Use of undefined constant misc - assumed 'misc' (this will throw an Error in a future version of PHP) in ..../includes/class_bootstrap.php(1422) : eval()'d code(4) : eval()'d code on line 1

PHP Warning: Use of undefined constant index - assumed 'index' (this will throw an Error in a future version of PHP) in ..../includes/class_bootstrap.php(1422) : eval()'d code(4) : eval()'d code on line 1

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6
Creating your Lua interpreter
Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: Creating your Lua interpreter

  1. #1

    Post Lua Interpreter

    Hello!

    I haven't found any tutorial about this here, so i decide to share how to make a textbox fully as lua interpreter using C# windows forms.

    1 - First of all we need to download our textbox plugin, and that DLL to our winform project.
    1.1 - FastColoredTextBox
    2 - Right click on win form project -> Manage NuGet Packages, and search for MoonSharp, install the Interpreter.

    After doing both, your solution should look like this.


    3- Open the toolbox -> Right click -> Choose Items -> Browse , and look for FastColoredTextBox.dll, if it works this will appear on your toolbox control.


    4 - Now drag&drop FastColoredTextBox to your win form, set the ID you want. and in C# section add that code.

    Code:
    private void InitStylesPriority()
    {
    	// we making this to define that our textbox will have lua style
    	// because this plugin, can handle a lot of languages.
    	
        //add this style explicitly for drawing under other styles
        txtAction.AddStyle(SameWordsStyle);
    
        txtAction.ClearStylesBuffer();
        txtAction.Range.ClearStyle(StyleIndex.All);
        txtAction.AddStyle(SameWordsStyle);
        txtAction.AutoIndentNeeded -= txtAction_AutoIndentNeeded;
        txtAction.Language = Language.Lua;
        txtAction.OnSyntaxHighlight(new TextChangedEventArgs(txtAction.Range));
    }
    
    private void txtAction_SelectionChangedDelayed(object sender, EventArgs e)
    {
        txtAction.VisibleRange.ClearStyle(SameWordsStyle);
        if (!txtAction.Selection.IsEmpty)
            return;//user selected diapason
    
        //get fragment around caret
        var fragment = txtAction.Selection.GetFragment(@"\w");
        string text = fragment.Text;
        if (text.Length == 0)
            return;
    	
        //highlight same words
        var ranges = txtAction.VisibleRange.GetRanges("\\b" + text + "\\b").ToArray();
        if (ranges.Length > 1)
            foreach (var r in ranges)
                r.SetStyle(SameWordsStyle);
    }
    
    private void txtAction_AutoIndentNeeded(object sender, AutoIndentEventArgs args)
    {
        //block {}
        if (Regex.IsMatch(args.LineText, @"^[^""']*\{.*\}[^""']*$"))
            return;
        //start of block {}
        if (Regex.IsMatch(args.LineText, @"^[^""']*\{"))
        {
            args.ShiftNextLines = args.TabLength;
            return;
        }
        //end of block {}
        if (Regex.IsMatch(args.LineText, @"}[^""']*$"))
        {
            args.Shift = -args.TabLength;
            args.ShiftNextLines = -args.TabLength;
            return;
        }
        //label
        if (Regex.IsMatch(args.LineText, @"^\s*\w+\s*:\s*($|//)") &&
            !Regex.IsMatch(args.LineText, @"^\s*default\s*:"))
        {
            args.Shift = -args.TabLength;
            return;
        }
        //some statements: case, default
        if (Regex.IsMatch(args.LineText, @"^\s*(case|default)\b.*:\s*($|//)"))
        {
            args.Shift = -args.TabLength / 2;
            return;
        }
        //is unclosed operator in previous line ?
        if (Regex.IsMatch(args.PrevLineText, @"^\s*(if|for|foreach|while|[\}\s]*else)\b[^{]*$"))
            if (!Regex.IsMatch(args.PrevLineText, @"(;\s*$)|(;\s*//)"))//operator is unclosed
            {
                args.Shift = args.TabLength;
                return;
            }
    }
    5 - Now in your Form_Load event call that the event we created.
    Code:
            private void Form_Load(object sender, EventArgs e)
            {
                // our event we created 
                InitStylesPriority();
            }
    6 - With that our textbox already fully syntax highlight, now we need to interpret Lua. We must add MoonSharp references, and we can define a static method as beeing a new lua function like this.
    Code:
    using MoonSharp.Interpreter;
    Code:
            Script script = new Script();
    
            private void Form_Load(object sender, EventArgs e)
            {
                // our event we created 
                InitStylesPriority();
    
                // we are telling to moonsharp that C# method Say in lua will be reconigze as say(string)
                script.Globals["say"] = (Action<String>)(Say);
            }
    
            /// <summary>
            /// This method will be interpreted on lua, just because its static, it's automated to recognize.
            /// </summary>
            /// <param name="message">Parameter message</param>
            public static void Say(String message)
            {
                MessageBox.Show(message);
            }
    7 - Now just create a button and inside button click event, make that
    Code:
                // we telling the moonsharp to interpret the text inside our textbox
                script.DoString(txtAction.Text);
    8 - See these 2 pictures with examples.






    Hope it help someone !
    Last edited by brunopbarrote; 11-26-2015 at 04:18 PM.

  2. #2
    Junior Member
    Join Date
    Oct 2015
    Posts
    6
    That's very helpful, lua scripter is the next thing im going to be working on in my bot. Thanks for contribution!

  3. #3
    Quote Originally Posted by rewisal View Post
    That's very helpful, lua scripter is the next thing im going to be working on in my bot. Thanks for contribution!
    Nop, let me know if u have any issue, if can help, i'll do with pleasure.

  4. #4
    Junior Member
    Join Date
    Oct 2015
    Posts
    12
    What a nice tutorial!

    Thanks for that, i'll probably use it on my project.

  5. #5
    Senior Member
    Join Date
    Nov 2009
    Posts
    320
    You are not creating a lua interpreter, just using it.
    But nice tutorial!
    oi amiguinhos

  6. #6
    Senior Member
    Join Date
    Jan 2012
    Posts
    417
    If you care about your text editor, take a look at Scintilla-based solutions like ScintillaNET.

    Scintilla has advanced support for programming languages out-of-the-box. They also provide fantastic support to allow different themes.

    I used Scintilla in the past and was able to get light and dark themes working (like those found in Sublime, Visual Studio) for Lua. Really an awesome library.

    The only drawback I can think of is their specific Lua version supported by their syntax highlighting engine, something like 5.1.

    Nice stuff you've shown here. Good job.

  7. #7
    Quote Originally Posted by Devil View Post
    You are not creating a lua interpreter, just using it.
    But nice tutorial!
    i've just expressed wrong, srry for that!

  8. #8
    Quote Originally Posted by Blequi View Post
    If you care about your text editor, take a look at Scintilla-based solutions like ScintillaNET.

    Scintilla has advanced support for programming languages out-of-the-box. They also provide fantastic support to allow different themes.

    I used Scintilla in the past and was able to get light and dark themes working (like those found in Sublime, Visual Studio) for Lua. Really an awesome library.

    The only drawback I can think of is their specific Lua version supported by their syntax highlighting engine, something like 5.1.

    Nice stuff you've shown here. Good job.
    Never heard about it. I'll check, thx for the tip

    I know my tutorial its a little amateur, but hope it at least give a hint for ppl who wanna make this xD

  9. #9
    Nice one, but probably you will have problems running more than one lua script at same time, take care about that.

  10. #10
    Quote Originally Posted by Casky View Post
    Nice one, but probably you will have problems running more than one lua script at same time, take care about that.
    i don't have

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •