protected List<Instruction> GetInstructions(string assemblerCode) { var app = new Application("getInstructions"); var opt = new LoaderOptions(engine, app); opt.UseIndicesLocally = false; var ldr = new Loader(opt); ldr.LoadFromString("function MyAssemblerFunction does asm {" + assemblerCode + "\n}"); if (ldr.ErrorCount != 0) { Console.WriteLine("--------------- Assembler Code --------------------"); Console.WriteLine(assemblerCode); Console.WriteLine("---------------- End Asm Code ---------------------"); foreach (var error in ldr.Errors) Assert.Fail(string.Format("Error in the expected assembler code: {0}", error)); } return app.Functions["MyAssemblerFunction"].Code; }
public static PValue RunStatically(StackContext sctx, PValue[] args) { if (sctx == null) throw new ArgumentNullException("sctx"); if (args == null) args = new PValue[] {}; var eng = sctx.ParentEngine; foreach (var arg in args) { var path = arg.CallToString(sctx); var ldrOptions = new LoaderOptions(sctx.ParentEngine, sctx.ParentApplication); ldrOptions.ReconstructSymbols = false; var ldr = sctx as Loader ?? new Loader(ldrOptions); var asmFile = ldr.ApplyLoadPaths(path); if (asmFile == null) throw new FileNotFoundException("Prexonite can't load assembly located in " + path); eng.RegisterAssembly(Assembly.LoadFile(asmFile.FullName)); } return PType.Null.CreatePValue(); }
public void AsmIdArgInstructions() { const string input1 = @" function func1 does asm { newobj.1 ""Object(\""System.Text.StringBuilder\"")"" newtype.1 ""Object"" get.3 ToString set.2 __\defaultIndex new.1 ""Object(\""System.DateTime\"")"" sget.10 ""Object(\""System.Console\"")::WriteLine"" sset.1 ""Object(\""System.Console\"")::ForegroundColor"" indloc.2 aFunction indglob.3 anotherFunction } "; var opt = new LoaderOptions(engine, target); opt.UseIndicesLocally = false; var ldr = new Loader(opt); ldr.LoadFromString(input1); Assert.AreEqual(0, ldr.ErrorCount, "There were errors during compilation."); var code = target.Functions["func1"].Code; var i = 0; Assert.AreEqual(OpCode.newobj, code[i].OpCode); Assert.AreEqual("Object(\"System.Text.StringBuilder\")", code[i].Id); Assert.AreEqual(1, code[i++].Arguments); Assert.AreEqual(OpCode.newtype, code[i].OpCode); Assert.AreEqual("Object", code[i].Id); Assert.AreEqual(1, code[i++].Arguments); Assert.AreEqual(OpCode.get, code[i].OpCode); Assert.AreEqual("ToString", code[i].Id); Assert.AreEqual(3, code[i++].Arguments); Assert.AreEqual(OpCode.set, code[i].OpCode); Assert.AreEqual("__\\defaultIndex", code[i].Id); Assert.AreEqual(2, code[i++].Arguments); Assert.AreEqual(OpCode.newobj, code[i].OpCode); Assert.AreEqual("Object(\"System.DateTime\")", code[i].Id); Assert.AreEqual(1, code[i++].Arguments); Assert.AreEqual(OpCode.sget, code[i].OpCode); Assert.AreEqual("Object(\"System.Console\")::WriteLine", code[i].Id); Assert.AreEqual(10, code[i++].Arguments); Assert.AreEqual(OpCode.sset, code[i].OpCode); Assert.AreEqual("Object(\"System.Console\")::ForegroundColor", code[i].Id); Assert.AreEqual(1, code[i++].Arguments); Assert.AreEqual(OpCode.indloc, code[i].OpCode); Assert.AreEqual("aFunction", code[i].Id); Assert.AreEqual(2, code[i++].Arguments); Assert.AreEqual(OpCode.indglob, code[i].OpCode); Assert.AreEqual("anotherFunction", code[i].Id); Assert.AreEqual(3, code[i++].Arguments); }
public void AsmSpecialInstructions() { const string input1 = @" function func1(param1) [ key value; ] does asm { rot.2,3 swap ldc.real -2.53e-3 ldc.real 2.5 ldc.bool false ldc.bool TrUe indarg.3 inda.0 } "; var opt = new LoaderOptions(engine, target); opt.UseIndicesLocally = false; var ldr = new Loader(opt); ldr.LoadFromString(input1); Assert.AreEqual(0, ldr.ErrorCount, "There were errors during compilation."); var code = target.Functions["func1"].Code; var i = 0; Assert.AreEqual(OpCode.rot, code[i].OpCode); Assert.AreEqual(2, code[i].Arguments); Assert.AreEqual(3, (int) code[i++].GenericArgument); Assert.AreEqual(OpCode.rot, code[i].OpCode); Assert.AreEqual(1, code[i].Arguments); Assert.AreEqual(2, (int) code[i++].GenericArgument); Assert.AreEqual(OpCode.ldc_real, code[i].OpCode); Assert.AreEqual(-2.53e-3, (double) code[i++].GenericArgument); Assert.AreEqual(OpCode.ldc_real, code[i].OpCode); Assert.AreEqual(2.5, (double) code[i++].GenericArgument); Assert.AreEqual(OpCode.ldc_bool, code[i].OpCode); Assert.AreEqual(0, code[i++].Arguments); Assert.AreEqual(OpCode.ldc_bool, code[i].OpCode); Assert.AreEqual(1, code[i++].Arguments); Assert.AreEqual(OpCode.indarg, code[i].OpCode); Assert.AreEqual(3, code[i++].Arguments); Assert.AreEqual(OpCode.indarg, code[i].OpCode); Assert.AreEqual(0, code[i++].Arguments); }
public void AsmIdInstructions() { const string input1 = @" var glob1; function func1 does asm { var loc1 ldc.string ""Hello World"" ldr.func func1 ldr.glob glob1 ldr.loc loc1 ldr.type Int ldloc loc1 stloc loc1 ldglob glob1 stglob glob1 check.const Int cast.const Int } "; var opt = new LoaderOptions(engine, target); opt.UseIndicesLocally = false; var ldr = new Loader(opt); ldr.LoadFromString(input1); Assert.AreEqual(0, ldr.ErrorCount, "There were errors during compilation."); var code = target.Functions["func1"].Code; var i = 0; Assert.AreEqual(OpCode.ldc_string, code[i].OpCode); Assert.AreEqual("Hello World", code[i++].Id); Assert.AreEqual(OpCode.ldr_func, code[i].OpCode); Assert.AreEqual("func1", code[i++].Id); Assert.AreEqual(OpCode.ldr_glob, code[i].OpCode); Assert.AreEqual("glob1", code[i++].Id); Assert.AreEqual(OpCode.ldr_loc, code[i].OpCode); Assert.AreEqual("loc1", code[i++].Id); Assert.AreEqual(OpCode.ldr_type, code[i].OpCode); Assert.AreEqual("Int", code[i++].Id); Assert.AreEqual(OpCode.ldloc, code[i].OpCode); Assert.AreEqual("loc1", code[i++].Id); Assert.AreEqual(OpCode.stloc, code[i].OpCode); Assert.AreEqual("loc1", code[i++].Id); Assert.AreEqual(OpCode.ldglob, code[i].OpCode); Assert.AreEqual("glob1", code[i++].Id); Assert.AreEqual(OpCode.stglob, code[i].OpCode); Assert.AreEqual("glob1", code[i++].Id); Assert.AreEqual(OpCode.check_const, code[i].OpCode); Assert.AreEqual("Int", code[i++].Id); Assert.AreEqual(OpCode.cast_const, code[i].OpCode); Assert.AreEqual("Int", code[i++].Id); }
public void AsmNullInstructions() { const string input1 = @" function func1() does asm { //Instructions in the null group nop //0 ldc.null //1 nop //2 nop //3 neg //4 not //5 add //6 sub //7 mul //8 div //9 mod //10 pow //11 ceq //12 cne //13 cgt //14 cge //15 clt //16 cle //17 or //18 and //19 xor //20 check.arg //21 cast . arg //22 ldr.eng //23 ldr. app //24 ret.exit //25 ret.break //26 ret.continue //27 ret.set //28 ret.value //29 //Aliases continue //30 break //31 ret //32 exit //33 } "; var opt = new LoaderOptions(engine, target); opt.UseIndicesLocally = false; var ldr = new Loader(opt); ldr.LoadFromString(input1); Assert.AreEqual(0, ldr.ErrorCount); var code = target.Functions["func1"].Code; var i = 0; Assert.AreEqual(OpCode.nop, code[i++].OpCode); Assert.AreEqual(OpCode.ldc_null, code[i++].OpCode); Assert.AreEqual(OpCode.nop, code[i++].OpCode); Assert.AreEqual(OpCode.nop, code[i++].OpCode); //operator aliases Assert.AreEqual(OpCode.cmd, code[i].OpCode); Assert.AreEqual(1, code[i].Arguments); Assert.AreEqual(UnaryNegation.DefaultAlias, code[i++].Id); Assert.AreEqual(1, code[i].Arguments); Assert.AreEqual(OpCode.cmd, code[i].OpCode); Assert.AreEqual(LogicalNot.DefaultAlias, code[i++].Id); Assert.AreEqual(2, code[i].Arguments); Assert.AreEqual(OpCode.cmd, code[i].OpCode); Assert.AreEqual(Addition.DefaultAlias, code[i++].Id); Assert.AreEqual(OpCode.cmd, code[i].OpCode); Assert.AreEqual(2, code[i].Arguments); Assert.AreEqual(Subtraction.DefaultAlias, code[i++].Id); Assert.AreEqual(OpCode.cmd, code[i].OpCode); Assert.AreEqual(2, code[i].Arguments); Assert.AreEqual(Multiplication.DefaultAlias, code[i++].Id); Assert.AreEqual(OpCode.cmd, code[i].OpCode); Assert.AreEqual(2, code[i].Arguments); Assert.AreEqual(Division.DefaultAlias, code[i++].Id); Assert.AreEqual(OpCode.cmd, code[i].OpCode); Assert.AreEqual(2, code[i].Arguments); Assert.AreEqual(Modulus.DefaultAlias, code[i++].Id); Assert.AreEqual(OpCode.cmd, code[i].OpCode); Assert.AreEqual(2, code[i].Arguments); Assert.AreEqual(Power.DefaultAlias, code[i++].Id); Assert.AreEqual(OpCode.cmd, code[i].OpCode); Assert.AreEqual(2, code[i].Arguments); Assert.AreEqual(Equality.DefaultAlias, code[i++].Id); Assert.AreEqual(OpCode.cmd, code[i].OpCode); Assert.AreEqual(2, code[i].Arguments); Assert.AreEqual(Inequality.DefaultAlias, code[i++].Id); Assert.AreEqual(OpCode.cmd, code[i].OpCode); Assert.AreEqual(2, code[i].Arguments); Assert.AreEqual(GreaterThan.DefaultAlias, code[i++].Id); Assert.AreEqual(OpCode.cmd, code[i].OpCode); Assert.AreEqual(2, code[i].Arguments); Assert.AreEqual(GreaterThanOrEqual.DefaultAlias, code[i++].Id); Assert.AreEqual(OpCode.cmd, code[i].OpCode); Assert.AreEqual(2, code[i].Arguments); Assert.AreEqual(LessThan.DefaultAlias, code[i++].Id); Assert.AreEqual(OpCode.cmd, code[i].OpCode); Assert.AreEqual(2, code[i].Arguments); Assert.AreEqual(LessThanOrEqual.DefaultAlias, code[i++].Id); Assert.AreEqual(OpCode.cmd, code[i].OpCode); Assert.AreEqual(2, code[i].Arguments); Assert.AreEqual(BitwiseOr.DefaultAlias, code[i++].Id); Assert.AreEqual(OpCode.cmd, code[i].OpCode); Assert.AreEqual(2, code[i].Arguments); Assert.AreEqual(BitwiseAnd.DefaultAlias, code[i++].Id); Assert.AreEqual(OpCode.cmd, code[i].OpCode); Assert.AreEqual(2, code[i].Arguments); Assert.AreEqual(ExclusiveOr.DefaultAlias, code[i++].Id); //other nullary instructions Assert.AreEqual(OpCode.check_arg, code[i++].OpCode); Assert.AreEqual(OpCode.cast_arg, code[i++].OpCode); Assert.AreEqual(OpCode.ldr_eng, code[i++].OpCode); Assert.AreEqual(OpCode.ldr_app, code[i++].OpCode); Assert.AreEqual(OpCode.ret_exit, code[i++].OpCode); Assert.AreEqual(OpCode.ret_break, code[i++].OpCode); Assert.AreEqual(OpCode.ret_continue, code[i++].OpCode); Assert.AreEqual(OpCode.ret_set, code[i++].OpCode); Assert.AreEqual(OpCode.ret_value, code[i++].OpCode); //Aliases Assert.AreEqual(OpCode.ret_continue, code[i++].OpCode); Assert.AreEqual(OpCode.ret_break, code[i++].OpCode); Assert.AreEqual(OpCode.ret_value, code[i++].OpCode); Assert.AreEqual(OpCode.ret_exit, code[i++].OpCode); }
public void AsmLocalVariableDeclaration() { const string input1 = @" function func1() does asm { var loc1 ref loc2 } "; var opt = new LoaderOptions(engine, target); opt.UseIndicesLocally = false; var ldr = new Loader(opt); ldr.LoadFromString(input1); Assert.AreEqual(0, ldr.ErrorCount); Assert.IsTrue(target.Functions["func1"].Variables.Contains("loc1")); Assert.AreEqual( SymbolInterpretations.LocalObjectVariable, LookupSymbolEntry(ldr.FunctionTargets["func1"].Symbols, "loc1").Interpretation); Assert.IsTrue(target.Functions["func1"].Variables.Contains("loc2")); Assert.AreEqual( SymbolInterpretations.LocalReferenceVariable, LookupSymbolEntry(ldr.FunctionTargets["func1"].Symbols, "loc2").Interpretation); }
public void VariableDeclarations() { const string input1 = @" function func0 { var obj0; var obj1; var obj2; ref func1; ref ifunc0; ref cor0; declare var gobj0; declare var gobj1; static sobj0; static sobj1; } "; var opt = new LoaderOptions(engine, target) {UseIndicesLocally = false}; var ldr = new Loader(opt); ldr.LoadFromString(input1); foreach (var message in ldr.Errors) { Console.WriteLine(message); } Assert.AreEqual(0, ldr.ErrorCount, "Errors during compilation."); // We allow up to two instructions (`return sobj1`) to accomodate the // relaxed treatment of variable declarations in Prexonite 2 Assert.That(ldr.FunctionTargets["func0"].Code.Count, Is.LessThanOrEqualTo(2)); CompilerTarget tar = ldr.FunctionTargets["func0"]; Assert.AreEqual( new SymbolEntry(SymbolInterpretations.LocalObjectVariable, "obj0", null), LookupSymbolEntry(tar.Symbols, "obj0")); Assert.IsTrue(tar.Function.Variables.Contains("obj0")); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.LocalObjectVariable, "obj1", null), LookupSymbolEntry(tar.Symbols, "obj1")); Assert.IsTrue(tar.Function.Variables.Contains("obj1")); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.LocalObjectVariable, "obj2", null), LookupSymbolEntry(tar.Symbols, "obj2")); Assert.IsTrue(tar.Function.Variables.Contains("obj2")); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.LocalReferenceVariable, "func1", null), LookupSymbolEntry(tar.Symbols, "func1")); Assert.IsTrue(tar.Function.Variables.Contains("func1")); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.LocalReferenceVariable, "ifunc0", null), LookupSymbolEntry(tar.Symbols, "ifunc0")); Assert.IsTrue(tar.Function.Variables.Contains("ifunc0")); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.LocalReferenceVariable, "cor0", null), LookupSymbolEntry(tar.Symbols, "cor0")); Assert.IsTrue(tar.Function.Variables.Contains("cor0")); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.GlobalObjectVariable, "gobj0", target.Module.Name), LookupSymbolEntry(tar.Symbols, "gobj0")); Assert.IsFalse( tar.Function.Variables.Contains("gobj0"), "\"declare var <id>;\" only declares a global variable."); Assert.IsFalse( ldr.Options.TargetApplication.Variables.ContainsKey("gobj0"), "\"global <id>;\" only declares a global variable."); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.GlobalObjectVariable, "gobj1", target.Module.Name), LookupSymbolEntry(tar.Symbols, "gobj1")); Assert.IsFalse( tar.Function.Variables.Contains("gobj1"), "\"declare var <id>;\" only declares a global variable."); Assert.IsFalse( ldr.Options.TargetApplication.Variables.ContainsKey("gobj1"), "\"declare var <id>;\" only declares a global variable."); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.GlobalObjectVariable, "func0\\static\\sobj0", target.Module.Name), LookupSymbolEntry(tar.Symbols, "sobj0")); Assert.IsTrue( ldr.Options.TargetApplication.Variables.ContainsKey("func0\\static\\sobj0")); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.GlobalObjectVariable, "func0\\static\\sobj1", target.Module.Name), LookupSymbolEntry(tar.Symbols, "sobj1")); Assert.IsTrue( ldr.Options.TargetApplication.Variables.ContainsKey("func0\\static\\sobj1")); }
public void DefineGlobal() { //First declare the variables const string input1 = "declare var name1; " + "declare ref name2; " + "declare var value;"; var opt = new LoaderOptions(engine, target); opt.UseIndicesLocally = false; var ldr = new Loader(opt); ldr.LoadFromString(input1); Assert.AreEqual( 0, ldr.ErrorCount, "The compiler reported errors in the first chunk of code."); var symbols = ldr.TopLevelSymbols; Assert.AreEqual( new SymbolEntry(SymbolInterpretations.GlobalObjectVariable, "name1", target.Module.Name), LookupSymbolEntry(symbols, "name1")); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.GlobalReferenceVariable, "name2", target.Module.Name), LookupSymbolEntry(symbols, "name2")); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.GlobalObjectVariable, "value", target.Module.Name), LookupSymbolEntry(symbols, "value")); //Then define them const string input2 = "var name1; " + "ref name2 [ Description NotUseful; ]; " + "var name3;"; ldr.LoadFromString(input2); Assert.AreEqual( 0, ldr.ErrorCount, "The compiler reported errors in the second chunk of code."); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.GlobalObjectVariable, "name1", target.Module.Name), LookupSymbolEntry(symbols, "name1")); Assert.IsNotNull(target.Variables["name1"]); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.GlobalReferenceVariable, "name2", target.Module.Name), LookupSymbolEntry(symbols, "name2")); Assert.IsNotNull(target.Variables["name2"]); Assert.AreEqual("NotUseful", target.Variables["name2"].Meta["description"].Text); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.GlobalObjectVariable, "name3", target.Module.Name), LookupSymbolEntry(symbols, "name3")); Assert.IsNotNull(target.Variables["name3"]); }
public void Redeclare() { const string input = "declare function name1; " + "declare var name2; " + "declare ref name1, name2; " + "declare function name1;"; var opt = new LoaderOptions(engine, target); opt.UseIndicesLocally = false; var ldr = new Loader(opt); ldr.LoadFromString(input); Assert.AreEqual(0, ldr.ErrorCount); var symbols = ldr.TopLevelSymbols; Assert.AreEqual( new SymbolEntry(SymbolInterpretations.Function, "name1", target.Module.Name), LookupSymbolEntry(symbols, "name1")); Assert.AreNotEqual( new SymbolEntry(SymbolInterpretations.GlobalObjectVariable, "name2", target.Module.Name), LookupSymbolEntry(symbols, "name2")); Assert.AreNotEqual( new SymbolEntry(SymbolInterpretations.GlobalReferenceVariable, "name1", target.Module.Name), LookupSymbolEntry(symbols, "name1")); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.GlobalReferenceVariable, "name2", target.Module.Name), LookupSymbolEntry(symbols, "name2")); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.Function, "name1", target.Module.Name), LookupSymbolEntry(symbols, "name1")); const string input2 = "declare function name1; " + "declare function name2; "; ldr.LoadFromString(input2); Assert.AreEqual(0, ldr.ErrorCount); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.Function, "name1", target.Module.Name), LookupSymbolEntry(symbols, "name1")); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.Function, "name2", target.Module.Name), LookupSymbolEntry(symbols, "name2")); }
public void Declare() { const string input = "declare function f\\1; " + "declare var go\\1; " + "declare ref gf\\1, gf\\2; " + "declare function if\\1;"; var opt = new LoaderOptions(engine, target) {UseIndicesLocally = false}; var ldr = new Loader(opt); ldr.LoadFromString(input); Assert.AreEqual(0, ldr.ErrorCount); var symbols = ldr.TopLevelSymbols; Assert.AreEqual( new SymbolEntry(SymbolInterpretations.Function, "f\\1", target.Module.Name), LookupSymbolEntry(symbols, @"f\1")); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.GlobalObjectVariable, "go\\1", target.Module.Name), LookupSymbolEntry(symbols, @"go\1")); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.GlobalReferenceVariable, "gf\\1", target.Module.Name), LookupSymbolEntry(symbols, @"gf\1")); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.GlobalReferenceVariable, "gf\\2", target.Module.Name), LookupSymbolEntry(symbols, @"gf\2")); Assert.AreEqual( new SymbolEntry(SymbolInterpretations.Function, "if\\1", target.Module.Name), LookupSymbolEntry(symbols, @"if\1")); }
public void AddingMetadata() { const string input1 = "MyList { elem1, elem2, elem3 };"; var opt = new LoaderOptions(engine, target); opt.UseIndicesLocally = false; var ldr = new Loader(opt); ldr.LoadFromString(input1); Assert.AreEqual(0, ldr.ErrorCount); Assert.IsTrue(target.Meta.ContainsKey("MyList"), "MyList missing"); Assert.IsTrue(target.Meta["MyList"].IsList, "MyList should be a list"); Assert.AreEqual(3, target.Meta["MyList"].List.Length); Assert.AreEqual("elem2", target.Meta["MyList"].List[1].Text); const string input2 = "Add elem4 to MyList;"; ldr.LoadFromString(input2); Assert.AreEqual(0, ldr.ErrorCount); Assert.IsTrue(target.Meta.ContainsKey("MyList"), "MyList missing after modification"); Assert.IsTrue( target.Meta["MyList"].IsList, "MyList should be a list, even after modification"); Assert.AreEqual(4, target.Meta["MyList"].List.Length); Assert.AreEqual("elem4", target.Meta["MyList"].List[3].Text); const string input3 = "Add { elem5, elem6 } to MyList;"; ldr.LoadFromString(input3); Assert.AreEqual(0, ldr.ErrorCount); Assert.IsTrue( target.Meta.ContainsKey("MyList"), "MyList missing after 2nd modification"); Assert.IsTrue( target.Meta["MyList"].IsList, "MyList should be a list, even after 2nd modification"); Assert.AreEqual(6, target.Meta["MyList"].List.Length); Assert.AreEqual("elem6", target.Meta["MyList"].List[5].Text); const string input4 = @"Import { System, System::Text }; Add System::Xml to Imports; "; ldr.LoadFromString(input4); Assert.AreEqual(0, ldr.ErrorCount, "There were errors during compilator of input4"); Assert.IsTrue(target.Meta.ContainsKey("Import"), "Import missing"); Assert.IsTrue( target.Meta["Import"].IsList, "Import should be a list after 2nd modification"); Assert.AreEqual(3, target.Meta["Import"].List.Length); Assert.AreEqual("System", target.Meta["Import"].List[0].Text); Assert.AreEqual("System.Text", target.Meta["Import"].List[1].Text); Assert.AreEqual("System.Xml", target.Meta["Import"].List[2].Text); }
public void NestedMetadata() { const string input1 = "FirstList { \"first String\", firstId, true, 55 } ;" + "SecondList { \"second String\", { \"first }}Nested{ string\", secondNestedId }, thirdId }; "; var opt = new LoaderOptions(engine, target); opt.UseIndicesLocally = false; var ldr = new Loader(opt); ldr.LoadFromString(input1); Assert.AreEqual(0, ldr.ErrorCount); //First list Assert.IsTrue(target.Meta.ContainsKey("firstList"), "firstList missing"); Assert.IsTrue(target.Meta["firstList"].IsList, "firstList should be a list"); Assert.AreEqual("first String", target.Meta["firstList"].List[0].Text); Assert.AreEqual("firstId", target.Meta["firstList"].List[1].Text); Assert.AreEqual(true, target.Meta["firstList"].List[2].Switch); Assert.AreEqual("55", target.Meta["firstList"].List[3].Text); //Second list Assert.IsTrue(target.Meta.ContainsKey("secondList"), "secondList missing"); Assert.IsTrue(target.Meta["secondList"].IsList, "secondList should be a list"); Assert.AreEqual("second String", target.Meta["secondList"].List[0].Text); Assert.IsTrue( target.Meta["secondList"].List[1].IsList, "second element should be a list"); Assert.AreEqual( "first }}Nested{ string", target.Meta["secondList"].List[1].List[0].Text); Assert.AreEqual("secondNestedId", target.Meta["secondList"].List[1].List[1].Text); Assert.AreEqual("thirdId", target.Meta["secondList"].List[2].Text); }
private Loader _justCompile(string input) { var opt = new LoaderOptions(engine, target) {UseIndicesLocally = false}; var ldr = new Loader(opt); ldr.LoadFromString(input); return ldr; }
public void AsmIntInstructions() { const string input1 = @" function func1 does asm { ldc.int 1 //0 ldc.int -58416325 //1 pop 1 //2 pop 10 //3 dup 2 //4 dup 10 //5 } "; var opt = new LoaderOptions(engine, target); opt.UseIndicesLocally = false; var ldr = new Loader(opt); ldr.LoadFromString(input1); Assert.AreEqual(0, ldr.ErrorCount, "Errors during compilation."); var code = target.Functions["func1"].Code; var i = 0; Assert.AreEqual(OpCode.ldc_int, code[i].OpCode); Assert.AreEqual(1, code[i++].Arguments); Assert.AreEqual(OpCode.ldc_int, code[i].OpCode); Assert.AreEqual(-58416325, code[i++].Arguments); Assert.AreEqual(OpCode.pop, code[i].OpCode); Assert.AreEqual(1, code[i++].Arguments); Assert.AreEqual(OpCode.pop, code[i].OpCode); Assert.AreEqual(10, code[i++].Arguments); Assert.AreEqual(OpCode.dup, code[i].OpCode); Assert.AreEqual(2, code[i++].Arguments); Assert.AreEqual(OpCode.dup, code[i].OpCode); Assert.AreEqual(10, code[i++].Arguments); }
public void ExpressionFunctions() { const string input1 = @" function twice(x) = 2*x; function megabyte = 1024*1024; "; var opt = new LoaderOptions(engine, target); opt.UseIndicesLocally = false; var ldr = new Loader(opt); ldr.LoadFromString(input1); Assert.AreEqual(0, ldr.ErrorCount, "Errors during compilation."); //check "twice" var actual = target.Functions["twice"].Code; var expected = GetInstructions(@" ldc.int 2 ldloc x mul ret.value "); Console.Write(target.StoreInString()); Assert.AreEqual( expected.Count, actual.Count, "Expected and actual instruction count missmatch in twice."); for (var i = 0; i < actual.Count; i++) Assert.AreEqual( expected[i], actual[i], String.Format( "Twice: Instructions at address {0} do not match ({1} != {2})", i, expected[i], actual[i])); //check "megabyte" actual = target.Functions["megabyte"].Code; expected = GetInstructions(@" ldc.int 1048576 ret.value "); Assert.AreEqual( expected.Count, actual.Count, "Expected and actual instruction count missmatch in megabyte."); for (var i = 0; i < actual.Count; i++) Assert.AreEqual( expected[i], actual[i], String.Format( "Megabyte: Instructions at address {0} do not match ({1} != {2})", i, expected[i], actual[i])); }
public void AsmLabelsAndJumps() { const string input1 = @" function func1 does asm { jump 14 ldc.bool true jump.t 14 jump.f 5 //a while loop var i ldc.int 5 stloc i label while0 ldloc i ldc.int 0 cgt jump.f endwhile0 ldloc i sget.1 ""Object(\""System.Console\"")::WriteLine"" dec i jump while0 label endwhile0 } "; var opt = new LoaderOptions(engine, target); opt.UseIndicesLocally = false; var ldr = new Loader(opt); ldr.LoadFromString(input1); Assert.AreEqual(0, ldr.ErrorCount, "Errors during compilation."); var code = target.Functions["func1"].Code; Assert.AreEqual(OpCode.jump, code[0].OpCode); Assert.AreEqual(14, code[0].Arguments); Assert.AreEqual(OpCode.jump_t, code[2].OpCode); Assert.AreEqual(14, code[2].Arguments); Assert.AreEqual(OpCode.jump_f, code[3].OpCode); Assert.AreEqual(5, code[3].Arguments); //The while loop Assert.AreEqual(OpCode.jump, code[13].OpCode); Assert.AreEqual(6, code[13].Arguments); Assert.AreEqual(OpCode.jump_f, code[9].OpCode); Assert.AreEqual(14, code[9].Arguments); }
public void EmptyAsmFunction() { const string input1 = @" //minimalistic function func1 does asm {} //normal function func2(param1, param2, param3) does asm { /* a comment */ } //*bling-bling* function func3(param1, param2) [ Add System::IO to Imports; ] does asm { }"; var opt = new LoaderOptions(engine, target); opt.UseIndicesLocally = false; var ldr = new Loader(opt); ldr.LoadFromString(input1); Assert.AreEqual(0, ldr.ErrorCount); //func1 Assert.IsTrue(target.Functions.Contains("func1"), "func1 is not in the function table"); Assert.AreEqual(SymbolInterpretations.Function, LookupSymbolEntry(ldr.TopLevelSymbols, "func1").Interpretation); Assert.AreEqual(0, target.Functions["func1"].Parameters.Count); Assert.IsTrue(target.Functions.Contains("func2"), "func2 is not in the function table"); Assert.AreEqual(SymbolInterpretations.Function, LookupSymbolEntry(ldr.TopLevelSymbols, "func2").Interpretation); Assert.AreEqual(3, target.Functions["func2"].Parameters.Count); Assert.AreEqual("param2", target.Functions["func2"].Parameters[1]); Assert.IsTrue(target.Functions.Contains("func3"), "func3 is not in the function table"); Assert.AreEqual(SymbolInterpretations.Function, LookupSymbolEntry(ldr.TopLevelSymbols, "func3").Interpretation); Assert.AreEqual(2, target.Functions["func3"].Parameters.Count); Assert.AreEqual("param2", target.Functions["func3"].Parameters[1]); Assert.AreEqual(1, target.Functions["func3"].ImportedNamespaces.Count); }
/// <summary> /// Prints a stored representation of each application in the compound that has its "request_dump" flag set. /// </summary> public void PrintCompound() { var tasks = Application.Compound.Where(app => app.Meta[DumpRequestFlag].Switch).Select( app => new KeyValuePair<ModuleName, Task<ITarget>>(app.Module.Name, ModuleCache.BuildAsync(app.Module.Name))) .ToDictionary(k => k.Key, k => k.Value); foreach (var entry in tasks) { var name = entry.Key; var target = entry.Value.Result; Console.WriteLine(); Console.WriteLine("################################## begin of stored representation for {0} ",name); var opt = new LoaderOptions(Engine, new Application(target.Module), target.Symbols) {ReconstructSymbols = false, RegisterCommands = false, StoreSymbols = true}; var ldr = new Loader(opt); ldr.Store(Console.Out); Console.WriteLine("################################## end of stored representation for {0} ----------", name); } }
public void ExplicitGotos() { const string input1 = @" declare function instruction; function func0 { begin: instruction; //1 goto fifth; //2 third : instruction; //3 fourth: goto sixth; //4 fifth: goto fourth; //5 sixth: goto begin; //6 } function instruction {} "; LoaderOptions opt = new LoaderOptions(engine, target); opt.UseIndicesLocally = false; Loader ldr = new Loader(opt); ldr.LoadFromString(input1); Assert.AreEqual(0, ldr.ErrorCount, "Errors during compilation."); //Check AST AstBlock block = ldr.FunctionTargets["func0"].Ast; //label begin int i = 0; Assert.IsInstanceOf(typeof(AstExplicitLabel), block[i]); Assert.AreEqual("begin", ((AstExplicitLabel) block[i]).Label); //instruction i++; Assert.IsInstanceOf(typeof(AstIndirectCall), block[i]); Assert.AreEqual(PCall.Get, ((AstGetSet) block[i]).Call); Assert.AreEqual(0, ((AstGetSet) block[i]).Arguments.Count); //goto fith i++; Assert.IsInstanceOf(typeof(AstExplicitGoTo), block[i]); Assert.AreEqual("fifth", ((AstExplicitGoTo) block[i]).Destination); //label third i++; Assert.IsInstanceOf(typeof(AstExplicitLabel), block[i]); Assert.AreEqual("third", ((AstExplicitLabel) block[i]).Label); //instruction i++; Assert.IsInstanceOf(typeof(AstIndirectCall), block[i]); Assert.AreEqual(PCall.Get, ((AstGetSet)block[i]).Call); Assert.AreEqual(0, ((AstGetSet)block[i]).Arguments.Count); //label fourth i++; Assert.IsInstanceOf(typeof(AstExplicitLabel), block[i]); Assert.AreEqual("fourth", ((AstExplicitLabel) block[i]).Label); //goto sixth i++; Assert.IsInstanceOf(typeof(AstExplicitGoTo), block[i]); Assert.AreEqual("sixth", ((AstExplicitGoTo) block[i]).Destination); //label fifth i++; Assert.IsInstanceOf(typeof(AstExplicitLabel), block[i]); Assert.AreEqual("fifth", ((AstExplicitLabel) block[i]).Label); //goto fourth i++; Assert.IsInstanceOf(typeof(AstExplicitGoTo), block[i]); Assert.AreEqual("fourth", ((AstExplicitGoTo) block[i]).Destination); //label sixth i++; Assert.IsInstanceOf(typeof(AstExplicitLabel), block[i]); Assert.AreEqual("sixth", ((AstExplicitLabel) block[i]).Label); //goto begin i++; Assert.IsInstanceOf(typeof(AstExplicitGoTo), block[i]); Assert.AreEqual("begin", ((AstExplicitGoTo) block[i]).Destination); Console.WriteLine(target.StoreInString()); }