public object Evaluate() { if (expression == null) { throw new ExpressionEvaluatorException("the expression should not be empty or null"); } object result; try { ExpressionLexer lexer = new ExpressionLexer(expression); ExpressionParser parser = new ExpressionParser( new TokenStream(lexer), context); ExpressionNode tree = parser.Parse(); ExpressionVisitor visitor = new ExpressionVisitor(context); IExpression expr = ExpressionBuilder.Build(tree); expr.Accept(visitor); result = visitor.EvaluationResult; } catch (Exception e) // reports all unexpected exceptions { context.ReportError( String.Format( "failed to evaluate expression '{0}' : '{1}'", expression, e.Message)); result = expression; } return(result); }
/// <summary> /// The Literal operation /// </summary> /// <returns>An expression node created based on the operation</returns> protected ExpressionNode Literal() { if (currentToken.Text.Length > 0) { if (currentToken.Type == TokenType.Integer) { int res; if (TryConvertNumber(currentToken.Text, out res)) { ExpressionNode integer = new ExpressionNode( new Token(TokenType.Integer, res.ToString())); GetNextToken(); return(integer); } else { throw new ExpressionEvaluatorException( String.Format("invalid number '{0}'", currentToken.Text)); } } else if (currentToken.Type == TokenType.String) { int x; if (!context.TryResolveSymbol(currentToken.Text, out x)) { string tokenText = currentToken.Text; while (true) { if (context.Variables != null && !context.Variables.ContainsKey(currentToken.Text) && !DatatypeInfoProvider.IsPredefinedDatatype(currentToken.Text) && !DatatypeInfoProvider.isPredefinedModifier(currentToken.Text)) { string typeInfo; if (context.TryResolveCustomType(currentToken.Text, out typeInfo)) { tokenText = typeInfo; break; } else { context.ReportError( String.Format("cannot resolve symbol '{0}'", currentToken.Text)); break; } } if (context.Variables != null && context.Variables.ContainsKey(currentToken.Text)) { tokenText = currentToken.Text; break; } // treat predefinedModifier as empty node if (DatatypeInfoProvider.isPredefinedModifier(currentToken.Text)) { GetNextToken(); } else if (DatatypeInfoProvider.IsPredefinedDatatype(currentToken.Text)) { tokenText = currentToken.Text; break; } } // in order to get pointer type size and avoid related issue bool isPointerType = false; GetNextToken(); if (isInSizeOf) { while (currentToken.Text == "*") { GetNextToken(); isPointerType = true; } } if (isPointerType) { ExpressionNode integer = new ExpressionNode( new Token(TokenType.Integer, "4")); return(integer); } ExpressionNode variable = new ExpressionNode( new Token(TokenType.Variable, tokenText)); return(variable); } else if (isInSizeOf) { string tokenText = currentToken.Text; GetNextToken(); //ignoring other expression while (currentToken.Text != ")") { GetNextToken(); } return(new ExpressionNode( new Token(TokenType.Variable, tokenText))); } else { GetNextToken(); return(new ExpressionNode( new Token(TokenType.Integer, x.ToString()))); } } else { throw new ExpressionEvaluatorException( String.Format("unexpected token '{0}'", currentToken)); } } else { throw new ExpressionEvaluatorException("unexpected end of input"); } }