repl.it
@CSharpIsGud/

LuaInterpreter

C#

No description

fork
loading
Files
  • main.cs
  • code.lua
  • Interpreter.cs
  • Lexer.cs
  • LuaGlobals.cs
  • main.exe
  • main.exe.mdb
  • MathParser.cs
  • Parser.cs
main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.IO;
using LuaSharp;

class MainClass {
  public static void Main (string[] args) {
    Interpreter intrp = new Interpreter();
    intrp.LoadGlobals();

    // Example on adding C# closures
    LuaFunction exit = new LuaFunction(intrp, null);

    exit.CS = (Interpreter I, LuaValue[] pars) => {
      int status = 0;

      if (pars.Length > 0) {
        status = (int) pars[0].GetNumber();
      }

      System.Environment.Exit(status);
      return LuaValue.Nil;
    };

    intrp._G.Set("exit", exit);

    // Stream for reading lua output
    StreamReader StdReader = new StreamReader(intrp.StdOut);
    StreamReader code = new StreamReader("./code.lua");
    
    intrp.Evaluate(code.ReadToEnd() + ' ');

    code.Close();
    
    intrp.StdOut.Seek(0, SeekOrigin.Begin);
    Console.WriteLine(StdReader.ReadToEnd());
  }
}