Beispiel #1
0
 public static Func <IReadOnlyList <ExpressionTerm>, decimal> EvaluateExpression(string expression)
 {
     using (var stringReader = new StringReader(expression))
     {
         var inputStream    = new AntlrInputStream(stringReader);
         var lexer          = new MyGrammarLexer(inputStream);
         var tokens         = new CommonTokenStream(lexer);
         var parser         = new MyGrammarParser(tokens);
         var expressionTree = parser.expr();// lets call the parser on the whole thing
         var visitor        = new MyGrammarExpressionVisitor();
         var func           = visitor.Visit(expressionTree);
         return(func);
     }
 }
 public static (Func <IReadOnlyList <IGrammarTerm>, decimal> function, IReadOnlyList <IGrammarTerm> rawTerms) EvaluateExpression(string expression)
 {
     using (var stringReader = new StringReader(expression))
     {
         var inputStream    = new AntlrInputStream(stringReader);
         var lexer          = new MyGrammarLexer(inputStream);
         var tokens         = new CommonTokenStream(lexer);
         var parser         = new MyGrammarParser(tokens);
         var expressionTree = parser.expr();// lets parse the whole thing
         var visitor        = new MyGrammarExpressionVisitor();
         // now that we have a "parsed expression" let's visit all the nodes.
         var func = visitor.Visit(expressionTree);
         return(func, visitor.Terms);
     }
 }