/// <summary> /// Parse code containing a list of statements. /// </summary> public Block ParseBlock(string code, Block block) { var reader = new BlockParser(code); var analyzer = new BlockAnalyzer(block); foreach (CodeSegment sc in reader) { TokenStream t = TokenReader.Read(sc); LaxExpression s = analyzer.AnalyzeStatement(t); if (s == null) continue; block.Statements.Add(s); } var unitEval = new TypeEvaluator(block); var constEval = new ConstEvaluater(block); foreach (var c in block.Constants.Values) { unitEval.Eval(c.Value); constEval.Eval(c); } foreach (var e in block.Statements) { unitEval.Eval(e); } return block; }
public Interpreter(Block block) { //Variables that are defined. //Their values will be defined later in the expressions. foreach (var v in block.Variables.Values) { variables.Add(v.Name, null); } }
/// <summary> /// Parse code containing a list of statements. /// </summary> public Block ParseBlock(string code) { var block = new Block(); var max = new FuncDefinition("max"); block.Functions.Add(max.Name, max); ParseBlock(code, block); return block; }
public static void PreCaclulate(Block block) { var list = new List<LaxExpression>(); foreach (var s in block.Statements) { list.Add(Evaluate(s)); } block.Statements = list; }
/* [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; }
static void CheckFree(Block block, Definition def) { { UnitDefinition existingUnit; if (block.Units.TryGetValue(def.Name, out existingUnit)) throw new SemanticError(def, "Already defined: " + existingUnit.Name, existingUnit); } { VarDefinition existingVar; if (block.Variables.TryGetValue(def.Name, out existingVar)) throw new SemanticError(def, "Already defined: " + existingVar.Name, existingVar); } { ConstDefinition existingConst; if (block.Constants.TryGetValue(def.Name, out existingConst)) throw new SemanticError(def, "Already defined: " + existingConst.Name, existingConst); } { FuncDefinition existingFunc; if (block.Functions.TryGetValue(def.Name, out existingFunc)) throw new SemanticError(def, "Already defined: " + existingFunc.Name, existingFunc); } }
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 Func<double> Compile(Block block) { ParameterExpression result = Expression.Parameter(typeof(int), "result"); var compiler = new ExpressionGenerator(); compiler.SetReturnType<double>(); foreach (var varDef in block.Variables.Values) { //var v = Expression.Variable(ExpressionCompiler.GetCsType(varDef.ValueType), varDef.Name.Name); //expressions.Add(v); compiler.AddVariable(varDef); } foreach (var statement in block.Statements) { compiler.AddExpression(statement); } BlockExpression blockExpression = compiler.Generate(); var lambda = Expression.Lambda<Func<double>>(blockExpression); var compiled = lambda.Compile(); return compiled; }
public ConstEvaluater(Block block) { this.block = block; interpreter = new Interpreter(block); }
public ExpressionParser(Block block) { this.block = block; }
public TypeEvaluator(Block block) { this.block = block; }