public static MAst Compile(AstHelper runtime, ICharStream stream) { var lexer = new TigerLexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new TigerParser(tokens); ProgramExpression programExpression = parser.parse(); if (parser.NumberOfSyntaxErrors > 0) { IEnumerable <string> errors = from e in parser.Errors select e.ToString(); throw new SyntaxException(errors); } AstHelper helper = runtime.CreateChild(function: true, variables: true, types: true); programExpression.CheckSemantics(helper); if (helper.Errors.HasErrors) { throw new SemanticException(helper.Errors); } return(programExpression.Transform()); }
public void BinaryExpresionOperation(string program, ExpressionType operation) { TigerParser parser = Mother.CreateParser(program); ProgramExpression rootAstExpression = parser.parse(); Expression expression = rootAstExpression.Compile(); Assert.IsInstanceOf <BinaryExpression>(expression); var binary = expression as BinaryExpression; Assert.AreEqual(binary.NodeType, operation); }
public void ConstantExpression_stringValue(string str) { TigerParser parser = Mother.CreateParser(str); Expression expression = parser.parse().Compile(); Assert.IsInstanceOf <ConstantExpression>(expression); var constant = expression as ConstantExpression; Assert.IsInstanceOf <string>(constant.Value); Assert.AreEqual(constant.Value, str.Substring(1, str.Length - 2)); }
public void ConstantExpression_intValue(int number) { TigerParser parser = Mother.CreateParser(number.ToString()); Expression expression = parser.parse().Compile(); Assert.IsInstanceOf <ConstantExpression>(expression); var constant = expression as ConstantExpression; Assert.IsInstanceOf <int>(constant.Value); Assert.AreEqual((int)constant.Value, number); }
public void calls_function_in_runtime() { string program = "Calc()"; TigerParser parser = Mother.CreateParser(program); var runtime = new AstHelper(null); Expression <Func <int> > calc = () => 1; runtime.Functions.Add("Calc", new FunctionReference(calc, typeof(int))); Expression result = parser.parse().Compile(runtime); Mother.Test(result, 1); }