public void Evaluate() { Expression(); switch (TokenType) { case RPNTokenType.Less: case RPNTokenType.Greater: case RPNTokenType.LessOrEqual: case RPNTokenType.GreateOrEqual: case RPNTokenType.NotEqual: case RPNTokenType.Equal: RPNOperator aOperator = new RPNOperator(Owner, RPNObjectType.Operator); aOperator.OperatorType = TokenType; aOperator.OperatorStr = Token; NextToken(); Evaluate(); Owner.RPNList.Add(aOperator); break; case RPNTokenType.EndParentheses: break; case RPNTokenType.EndParam: return; case RPNTokenType.Eof: return; } }
private void Expression() { Term(); switch (TokenType) { case RPNTokenType.Plus: case RPNTokenType.Minus: case RPNTokenType.Or: RPNOperator aOperator = new RPNOperator(Owner, RPNObjectType.Operator); aOperator.OperatorType = TokenType; aOperator.OperatorStr = Token; NextToken(); Expression(); Owner.RPNList.Add(aOperator); break; } }
private void Term() { Factor(); switch (TokenType) { case RPNTokenType.Mulitiply: case RPNTokenType.Divide: case RPNTokenType.Div: case RPNTokenType.Mod: case RPNTokenType.And: RPNOperator aOperator = new RPNOperator(Owner, RPNObjectType.Operator); aOperator.OperatorType = TokenType; aOperator.OperatorStr = Token; NextToken(); Term(); Owner.RPNList.Add(aOperator); break; } }
private void Factor() { RPNOperand operand; RPNOperator aOperator; switch (TokenType) { case RPNTokenType.Plus: case RPNTokenType.Minus: aOperator = new RPNOperator(Owner, RPNObjectType.Operator); switch (TokenType) { case RPNTokenType.Plus: (aOperator as RPNOperator).OperatorType = RPNTokenType.JustPlus; break; case RPNTokenType.Minus: (aOperator as RPNOperator).OperatorType = RPNTokenType.JustMinus; break; } aOperator.OperatorStr = Token; NextToken(); Term(); Owner.RPNList.Add(aOperator); break; case RPNTokenType.StartParentheses: NextToken(); Evaluate(); CheckToken(")"); NextToken(); break; case RPNTokenType.SquareBracket: RPNDataField dataField = new RPNDataField(Owner, RPNObjectType.DataField); dataField.Field = Token; Owner.RPNList.Add(dataField); NextToken(); break; case RPNTokenType.Ident: RPNFunction func; if (Owner.Environment != null) { func = Owner.Environment.FindFunction(Owner, Token.ToUpper()); } else { func = null; } if (func != null) { FunctionAttribute funcAttrib = (FunctionAttribute)Attribute.GetCustomAttribute(func.GetType(), typeof(FunctionAttribute)); if (funcAttrib.ParamTypes != null && funcAttrib.ParamTypes.Length > 0) { NextToken(); for (int i = 0; i < funcAttrib.ParamTypes.Length; i++) { if (i == 0) { CheckToken("("); } else { CheckToken(";"); } NextToken(); Evaluate(); } CheckToken(")"); } Owner.RPNList.Add(func); } else { RPNVariable var = Owner.FindVariable(Token.ToUpper()); if (var != null) { Owner.RPNList.Add(var); } else { throw new Exception("Unknown ident >" + Token + "< in expression >" + Owner.expression + "<."); } } NextToken(); break; case RPNTokenType.Number: operand = new RPNOperand(Owner, RPNObjectType.Operand); float number = float.Parse(Token); operand.Value = (object)number; Owner.RPNList.Add(operand); NextToken(); break; case RPNTokenType.String: operand = new RPNOperand(Owner, RPNObjectType.Operand); operand.Value = Token; Owner.RPNList.Add(operand); NextToken(); break; case RPNTokenType.DateTime: operand = new RPNOperand(Owner, RPNObjectType.Operand); try { operand.Value = Convert.ToDateTime(Token); } catch { throw new Exception(Token + " is not a valid date."); } Owner.RPNList.Add(operand); NextToken(); break; case RPNTokenType.Boolean: operand = new RPNOperand(Owner, RPNObjectType.Operand); operand.Value = Convert.ToBoolean(Token); Owner.RPNList.Add(operand); NextToken(); break; case RPNTokenType.Not: aOperator = new RPNOperator(Owner, RPNObjectType.Operator); aOperator.OperatorType = TokenType; aOperator.OperatorStr = Token; NextToken(); Factor(); Owner.RPNList.Add(aOperator); break; default: UnexpectedToken(); break; } }