//--------------------------------------------------------------------- public string Evaluate(ProgramScope Scope) { Stack <Token> stack = new Stack <Token>(); foreach (Token token in this) { if (token.Type == TokenType.Numeric) { stack.Push(token); } else if (token.Type == TokenType.Literal) { stack.Push(token); } else if (token.Type == TokenType.Identifier) { stack.Push(token); } else if (token.Type == TokenType.Symbol) { switch (token.Text) { case "^": this.EvaluateBinaryOperation_(Scope, stack, token); break; case "*": this.EvaluateBinaryOperation_(Scope, stack, token); break; case "/": this.EvaluateBinaryOperation_(Scope, stack, token); break; case "%": this.EvaluateBinaryOperation_(Scope, stack, token); break; case "+": this.EvaluateBinaryOperation_(Scope, stack, token); break; case "-": this.EvaluateBinaryOperation_(Scope, stack, token); break; } } } if (stack.Count != 1) { throw new Exception("Internal error; Non-empty stack after expression evaluation."); } return(stack.Pop().Text); }
public override void Execute(ProgramScope Scope) { string result = this.Expression.Evaluate(Scope); if (this.Variable.Type == VariableType.String) { this.Variable.Value = result; } else if (this.Variable.Type == VariableType.Numeric) { double val = 0.0; double.TryParse(result, out val); this.Variable.Value = val.ToString(); } else if (this.Variable.Type == VariableType.Date) { throw new NotImplementedException(); } return; }
public override void Execute(ProgramScope Scope) { throw new NotImplementedException(); }
public abstract void Execute(ProgramScope Scope);
public override void Execute(ProgramScope Scope) { return; }
public ProgramScope(ProgramScope ThisParentScope) { this.ParentScope = ThisParentScope; this.Variables = new VariableList(); this.Statements = new StatementList(); }
//--------------------------------------------------------------------- private void EvaluateBinaryOperation_(ProgramScope Scope, Stack <Token> Stack, Token Token) { if (Stack.Count < 2) { throw new Exception(string.Format("Insufficent number of operands for binary operation '{0}'.", Token.Text)); } Token result = new Token(); result.Type = TokenType.Numeric; Token token2 = Stack.Pop(); if (token2.Type == TokenType.Numeric) { } else if (token2.Type == TokenType.Literal) { result.Type = TokenType.Literal; } else if (token2.Type == TokenType.Identifier) { Variable variable = Scope.FindVariable(token2.Text, true); if (variable.Type == VariableType.String) { token2 = new Token(TokenType.Literal, variable.Value); result.Type = TokenType.Literal; } else if (variable.Type == VariableType.Numeric) { token2 = new Token(TokenType.Numeric, variable.Value); result.Type = TokenType.Numeric; } else if (variable.Type == VariableType.Date) { throw new NotImplementedException(); } } else { throw new Exception(string.Format("Invalid operand '{0}' for this type of operation '{1}'.", token2.Text, Token.Text)); } Token token1 = Stack.Pop(); if (token1.Type == TokenType.Numeric) { } else if (token1.Type == TokenType.Literal) { result.Type = TokenType.Literal; } else if (token1.Type == TokenType.Identifier) { Variable variable = Scope.FindVariable(token1.Text, true); if (variable.Type == VariableType.String) { token1 = new Token(TokenType.Literal, variable.Value); result.Type = TokenType.Literal; } else if (variable.Type == VariableType.Numeric) { token1 = new Token(TokenType.Numeric, variable.Value); result.Type = TokenType.Numeric; } else if (variable.Type == VariableType.Date) { throw new NotImplementedException(); } } else { throw new Exception(string.Format("Invalid operand '{0}' for this type of operation '{1}'.", token1.Text, Token.Text)); } if (result.Type == TokenType.Numeric) { double op1 = 0; double.TryParse(token1.Text, out op1); double op2 = 0; double.TryParse(token2.Text, out op2); double val = 0.0; switch (Token.Text) { case "^": val = Math.Pow(op1, op2); break; case "*": val = (op1 * op2); break; case "/": val = (op1 / op2); break; case "%": val = (op1 % op2); break; case "+": val = (op1 + op2); break; case "-": val = (op1 - op2); break; //case "&": break; //case "|": break; default: throw new Exception(string.Format("Unknown binary operation '{0}'.", Token.Text)); } result.Text = val.ToString(); } else if (result.Type == TokenType.Literal) { string op1 = token1.Text; string op2 = token2.Text; string val = ""; switch (Token.Text) { case "+": val = (op1 + op2); break; default: throw new Exception(string.Format("Unknown literal operation '{0}'.", Token.Text)); } result.Text = val; } else { throw new Exception("Internal error; Unsupported result type."); } // Store result. Stack.Push(result); return; }