public void TestCallFwdRef() { string test = @" .globals 0 .statedef default call func1() halt .def func1: args=0, locals=0 vconst <1.0,1.0,1.0> trace ret .evt default/state_entry: args=0, locals=0 ret "; Compiler.Compile(new ANTLRStringStream(test)); CompiledScript script = Compiler.Result; Assert.IsNotNull(script); Interpreter i = new Interpreter(script, null); i.TraceDestination = Listener.TraceDestination; while (i.ScriptState.RunState == RuntimeState.Status.Running) { i.Tick(); } Assert.IsTrue(i.ScriptState.Operands.Count == 0); //stack should be empty Console.WriteLine(Listener.TraceDestination.ToString()); }
public void TestBuildList() { string test = @" .globals 1 .statedef default fconst 1.1 fconst 3.2 sconst ""hi"" sconst ""\"""" iconst 5 buildlist 5 trace halt .evt default/state_entry: args=0, locals=0 ret "; Compiler.Compile(new ANTLRStringStream(test)); CompiledScript script = Compiler.Result; Assert.IsNotNull(script); Interpreter i = new Interpreter(script, null); i.TraceDestination = Listener.TraceDestination; while (i.ScriptState.RunState == RuntimeState.Status.Running) { i.Tick(); } Assert.IsTrue(i.ScriptState.Operands.Count == 0); //stack should be empty Console.WriteLine(Listener.TraceDestination.ToString()); Assert.IsTrue(Listener.MessagesContain("[1.1,3.2,hi,\",5]")); }
public void TestSubscriptAccess() { string test = @" .globals 2 .statedef default vconst <1.1,1.2,1.3> gstore 0 gload.sub 0,0 trace gload.sub 0,1 trace gload.sub 0,2 trace rconst <1.1,1.2,1.3,1.4> gstore 1 gload.sub 1,0 trace gload.sub 1,1 trace gload.sub 1,2 trace gload.sub 1,3 trace halt .evt default/state_entry: args=0, locals=0 ret "; Compiler.Compile(new ANTLRStringStream(test)); CompiledScript script = Compiler.Result; Assert.IsNotNull(script); Interpreter i = new Interpreter(script, null); i.TraceDestination = Listener.TraceDestination; while (i.ScriptState.RunState == RuntimeState.Status.Running) { i.Tick(); } Assert.IsTrue(i.ScriptState.Operands.Count == 0); //stack should be empty Console.WriteLine(Listener.TraceDestination.ToString()); Assert.IsTrue(Listener.MessagesContain("1.1\r\n1.2\r\n1.3\r\n1.1\r\n1.2\r\n1.3\r\n1.4")); }
public void TestCountingLoop() { string test = @" .globals 1 .statedef default iconst 0 gstore 0 loop: gload 0 iconst 1 iadd gstore 0 gload 0 trace gload 0 iconst 10 ilt brt loop halt .evt default/state_entry: args=0, locals=0 ret "; Compiler.Compile(new ANTLRStringStream(test)); CompiledScript script = Compiler.Result; Assert.IsNotNull(script); Interpreter i = new Interpreter(script, null); i.TraceDestination = Listener.TraceDestination; while (i.ScriptState.RunState == RuntimeState.Status.Running) { i.Tick(); } Assert.IsTrue(i.ScriptState.Operands.Count == 0); //stack should be empty Console.WriteLine(Listener.TraceDestination.ToString()); Assert.IsTrue(Listener.MessagesContain("1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n")); }
public void TestConcatList() { string test = @" .globals 1 .statedef default fconst 1.1 buildlist 1 gstore 0 gload 0 iconst 25 list.append gstore 0 sconst ""omg"" gload 0 list.prepend gstore 0 gload 0 trace halt .evt default/state_entry: args=0, locals=0 ret "; Compiler.Compile(new ANTLRStringStream(test)); CompiledScript script = Compiler.Result; Assert.IsNotNull(script); Interpreter i = new Interpreter(script, null); i.TraceDestination = Listener.TraceDestination; while (i.ScriptState.RunState == RuntimeState.Status.Running) { i.Tick(); } Assert.IsTrue(i.ScriptState.Operands.Count == 0); //stack should be empty Console.WriteLine(Listener.TraceDestination.ToString()); Assert.IsTrue(Listener.MessagesContain("[omg,1.1,25]")); }
public static Interpreter Run(CompiledScript script) { if (script != null) { System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); Interpreter i = new Interpreter(script, null); i.TraceDestination = Console.Out; while (i.ScriptState.RunState == RuntimeState.Status.Running) { i.Tick(); } watch.Stop(); System.Console.WriteLine("Execution: {0} seconds", watch.ElapsedMilliseconds / 1000.0); System.Console.WriteLine("Free Memory: {0} bytes", i.ScriptState.MemInfo.MemoryFree); return i; } return null; }
public void TestGoodEventDef() { string test = @" .statedef default .evt default/touch_start: args=1, locals=0 halt "; Compiler.Compile(new ANTLRStringStream(test)); CompiledScript script = Compiler.Result; Assert.IsNotNull(script); Interpreter i = new Interpreter(script,null); i.TraceDestination = Listener.TraceDestination; while (i.ScriptState.RunState == RuntimeState.Status.Running) { i.Tick(); } Assert.IsTrue(script.StateEvents[0][0].EventName == "touch_start"); Assert.IsTrue(i.ScriptState.Operands.Count == 0); //stack should be empty Console.WriteLine(Listener.TraceDestination.ToString()); }
static void Main(string[] args) { ICharStream input = null; if (args.Length > 0) { if (args[0] == "bytecompiler") { input = new ANTLRInputStream(Console.OpenStandardInput()); ByteCompilerFrontend fe = new ByteCompilerFrontend(); fe.Listener = new DefaultLSLListener(); fe.Compile(input); CompiledScript script = fe.Result; if (script != null) { System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); Interpreter i = new Interpreter(script, null); i.TraceDestination = Console.Out; while (i.ScriptState.RunState == RuntimeState.Status.Running) { i.Tick(); } watch.Stop(); System.Console.WriteLine(watch.ElapsedMilliseconds/1000.0); } } else { foreach (string arg in args) { Console.WriteLine("Compiling: " + arg); input = new ANTLRFileStream(arg); CompilerFrontend fe = new CompilerFrontend(); fe.TraceDestination = Console.Out; fe.Compile(input); } } } else { ILSLListener listener = new DefaultLSLListener(); LSLListenerTraceRedirectorMono redirector = new LSLListenerTraceRedirectorMono(listener); input = new ANTLRInputStream(Console.OpenStandardInput()); CompilerFrontend fe = new CompilerFrontend(); fe.TraceDestination = redirector; fe.Listener = listener; Console.WriteLine("** compilation output **"); string byteCode = fe.Compile(input); if (! listener.HasErrors() && byteCode != null) { input = new ANTLRStringStream(byteCode); ByteCompilerFrontend bfe = new ByteCompilerFrontend(); bfe.TraceDestination = redirector; bfe.Listener = listener; bfe.Compile(input); CompiledScript script = bfe.Result; Console.WriteLine("** usage info **"); if (script != null) Console.WriteLine("Base memory: {0} bytes", script.CalcBaseMemorySize()); //SaveScript(script); /* if (script != null) { System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); Interpreter i = new Interpreter(script, null); i.TraceDestination = Console.Out; while (i.ScriptState.RunState == RuntimeState.Status.Running) { i.Tick(); } watch.Stop(); System.Console.WriteLine("Execution: {0} seconds", watch.ElapsedMilliseconds / 1000.0); System.Console.WriteLine("Free Memory: {0} bytes", i.ScriptState.MemInfo.MemoryFree); } * */ } } }
public void TestJmpAround() { string test = @" .globals 1 .statedef default jmp lbl2 lbl2: iconst 0 jmp lbl3 lbl3: iconst 1 jmp lbl4 lbl4: iconst 2 sconst ""test"" trace trace trace trace halt .evt default/state_entry: args=0, locals=0 ret "; Compiler.Compile(new ANTLRStringStream(test)); CompiledScript script = Compiler.Result; Assert.IsNotNull(script); Interpreter i = new Interpreter(script, null); i.TraceDestination = Listener.TraceDestination; while (i.ScriptState.RunState == RuntimeState.Status.Running) { i.Tick(); } Assert.IsTrue(i.ScriptState.Operands.Count == 0); //stack should be empty Console.WriteLine(Listener.TraceDestination.ToString()); Assert.IsTrue(Listener.MessagesContain("test\r\n2\r\n1\r\n0")); }
public void TestCallWithParams() { string test = @" .globals 0 .statedef default iconst 20 call func1() trace sconst ""oh hai"" call func2() halt .def func1: args=1, locals=1 load 0 trace iconst 2 ret .def func2: args=1, locals=1 load 0 store 1 sconst ""gnarly"" call func1() pop ret .evt default/state_entry: args=0, locals=0 ret "; Compiler.Compile(new ANTLRStringStream(test)); CompiledScript script = Compiler.Result; Assert.IsNotNull(script); Interpreter i = new Interpreter(script, null); i.TraceDestination = Listener.TraceDestination; while (i.ScriptState.RunState == RuntimeState.Status.Running) { i.Tick(); } Assert.IsTrue(i.ScriptState.Operands.Count == 0); //stack should be empty Console.WriteLine(Listener.TraceDestination.ToString()); Assert.IsTrue(Listener.MessagesContain("20\r\n2\r\ngnarly\r\n")); }