/* [TestInitialize] public void Init() { } [TestCleanup] public void Clean() { } */ protected List<Literal> Run(Block block) { var response = new List<Literal>(); var interpreter = new Interpreter(block); foreach (var s in block.Statements) { var result = interpreter.Evaluate(s); response.Add(result); } return response; }
public static string Run(Block block, Func<double> dyn) { const int loops = 500000; //Expression Tree var start = DateTime.UtcNow; for (int n = 0; n < loops; n++) dyn(); var time = DateTime.UtcNow - start; //Interpreted start = DateTime.UtcNow; var interpreter = new Interpreter(block); for (int n = 0; n < loops; n++) { foreach (var e in block.Statements) { interpreter.Evaluate(e); } } var time2 = DateTime.UtcNow - start; return "\n\nRuntime speed Compiled: " + time.ToStringMicro(loops) + "\nRuntime speed Interpreted: " + time2.ToStringMicro(loops); }
public ConstEvaluater(Block block) { this.block = block; interpreter = new Interpreter(block); }
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; } }
static LaxExpression EvaluateOp(BinaryOp op) { var left = Evaluate(op.Left); var right = Evaluate(op.Right); var result = new BinaryOp(op.CodeRange, op.Operator).WithOperands(left, right); result.ValueType = op.ValueType; if (result.Left.Type == ExprType.Literal && result.Right.Type == ExprType.Literal) { //Operation with two literals, precalculate var interpreter = new Interpreter(); var literal = interpreter.Evaluate(result); return literal; } //No change if (left == op.Left && right == op.Right) return op; //No change, no evaluation possible return result; }