protected void RunAssertResult(string code, string expectedResult) { expectedResult = expectedResult.Replace("\r\n", "\n"); var block = parser.ParseBlock(code); var results = Run(block); var format = new CodeFormat(true); string result = ""; foreach (var r in results) { if (r == null) result += "\n"; else result += CodeFormat.Format(r.ValueType) + " " + CodeFormat.Format(r) + "\n"; } if (result != expectedResult) throw new Exception("Unexpected result"); }
public static string Format(LaxExpression expr) { var format = new CodeFormat(false); return format.FormatExpression(expr); }
public static CompileResult Compile(string code) { var response = new CompileResult(); try { try { code = code.Trim(" \t\n\r".ToCharArray()); response.Source = code; //Parse var parser = new LaxParser(); var block = parser.ParseBlock(code); //Run the code interpreted var interpreter = new Interpreter(block); foreach (var s in block.Statements) { var format = new CodeFormat(extraParentheses: true); var result = interpreter.Evaluate(s); if (result == null) response.Result += "\n"; else response.Result += CodeFormat.Format(result.ValueType.Type) + " " + CodeFormat.Format(result) + " " + CodeFormat.Format(result.ValueType.Unit) + "\n"; try { response.Interpreted += format.FormatExpression(s) + "\n"; } catch (Exception ex) { response.Interpreted += ex.Message + "\n"; } } ExpressionOptimizer.PreCaclulate(block); //Compile the code var compileStart = DateTime.UtcNow; var compiler = new CompilerNET(); var dyn = compiler.Compile(block); var compiletime = DateTime.UtcNow - compileStart; response.Result += "\n\nCompiled in : " + compiletime.ToStringMicro(); response.Result += "\n\nCompiled return value: " + dyn(); #if DEBUGx response.Result += SpeedTest.Run(block, dyn); #endif return response; } catch (LaxError le) { var ef = new ErrorFormat(le, code) { ExtraLines = 2 }; response.Error = ef.Format(); return response; } } catch (Exception ex) { response.Error = ex.GetType().Name + "\n" + ex.Message + "\n" + ex.StackTrace; return response; } }