private Expression ParseExpression() { Expression parsedExpression; if (currentToken == listTokens.Count) { ExceptionHandler("expected expression, got EOF"); } if (listTokens[currentToken] is StringBuilder) { string value = ((StringBuilder)listTokens[currentToken++]).ToString(); AlphaNumericValue _AlphaNumericValue = new AlphaNumericValue(); _AlphaNumericValue.Value = value; parsedExpression = _AlphaNumericValue; } else if (listTokens[currentToken] is int) { int intValue = (int)listTokens[currentToken++]; NumericValue _NumericValue = new NumericValue(); _NumericValue.Value = intValue; parsedExpression = _NumericValue; } else if (listTokens[currentToken] is string) { string identifier = (string)listTokens[currentToken++]; Identifier _Identifier = new Identifier(); _Identifier.IdentifierName = identifier; parsedExpression = _Identifier; } else { ExceptionHandler("expected string literal, int literal, or variable"); return(null); } //if (index < listTokens.Count && listTokens[index] != Scanner.Terminator && listTokens[index].GetType() != typeof(StringBuilder)) if (listTokens[currentToken].ToString() != Lexer.Tokens.Terminator.ToString()) { if (listTokens[currentToken].ToString() == Lexer.Tokens.Add.ToString() || listTokens[currentToken].ToString() == Lexer.Tokens.Subtract.ToString() || listTokens[currentToken].ToString() == Lexer.Tokens.Multiply.ToString() || listTokens[currentToken].ToString() == Lexer.Tokens.Divide.ToString()) { ArithmaticExpression arithmaticExpression = new ArithmaticExpression(); arithmaticExpression.Left = parsedExpression; if (listTokens[currentToken].ToString() == Lexer.Tokens.Add.ToString()) { arithmaticExpression.Operand = ArithmaticOperands.Add; } else if (listTokens[currentToken].ToString() == Lexer.Tokens.Subtract.ToString()) { arithmaticExpression.Operand = ArithmaticOperands.Subtract; } else if (listTokens[currentToken].ToString() == Lexer.Tokens.Multiply.ToString()) { arithmaticExpression.Operand = ArithmaticOperands.Multiply; } else if (listTokens[currentToken].ToString() == Lexer.Tokens.Divide.ToString()) { arithmaticExpression.Operand = ArithmaticOperands.Division; } currentToken++; arithmaticExpression.Right = ParseExpression(); parsedExpression = arithmaticExpression; } } return(parsedExpression); }
public void GenerateExpressionMethod(Expression _expression, System.Type expressionType) { System.Type typeOfExpression; if (_expression is AlphaNumericValue) { typeOfExpression = typeof(string); _ILMethod.Emit(OpCodes.Ldstr, ((AlphaNumericValue)_expression).Value); } else if (_expression is NumericValue) { typeOfExpression = typeof(int); _ILMethod.Emit(OpCodes.Ldc_I4, ((NumericValue)_expression).Value); } else if (_expression is ArithmaticExpression) { typeOfExpression = GetExpressionTypeMethod(_expression); ArithmaticExpression arithmaticExpression = (ArithmaticExpression)_expression; GenerateExpressionMethod(arithmaticExpression.Left, GetExpressionTypeMethod(arithmaticExpression.Left)); GenerateExpressionMethod(arithmaticExpression.Right, GetExpressionTypeMethod(arithmaticExpression.Right)); if (arithmaticExpression.Operand == ArithmaticOperands.Add) { _ILMethod.Emit(OpCodes.Add); } else if (arithmaticExpression.Operand == ArithmaticOperands.Subtract) { _ILMethod.Emit(OpCodes.Sub); } else if (arithmaticExpression.Operand == ArithmaticOperands.Multiply) { _ILMethod.Emit(OpCodes.Mul); } else if (arithmaticExpression.Operand == ArithmaticOperands.Division) { _ILMethod.Emit(OpCodes.Div); } } else if (_expression is Identifier) { string identifier = ((Identifier)_expression).IdentifierName; typeOfExpression = GetExpressionTypeMethod(_expression); if (!tblIdentifier.ContainsKey(identifier) && !tblArguments.ContainsKey(identifier)) { ExceptionHandler("undeclared variable '" + identifier + "'"); } if (tblIdentifier.ContainsKey(identifier)) { _ILMethod.Emit(OpCodes.Ldloc, tblIdentifier[identifier]); } else { _ILMethod.Emit(OpCodes.Ldloc, tblArguments[identifier]); } } else { ExceptionHandler("can't generate " + _expression.GetType().Name); typeOfExpression = null; } if (typeOfExpression != expressionType) { if (typeOfExpression == typeof(int) && expressionType == typeof(string)) { _ILMethod.Emit(OpCodes.Box, typeof(int)); _ILMethod.Emit(OpCodes.Callvirt, typeof(object).GetMethod("ToString")); } else { ExceptionHandler("can't convert " + typeOfExpression.Name + " to " + expressionType.Name); } } }