public override double Evaluate(Parser parser, Interpreter ii) { if (_token.Identifier == MathTokenType.Swap) { var left = _left as NameExpression; var right = _right as NameExpression; if (left == null) throw new ProcessusException(parser.Source, _token, "Left side of swap operation was not a variable."); if (right == null) throw new ProcessusException(parser.Source, _token, "Right side of swap operation was not a variable."); double temp = left.Evaluate(parser, ii); double b = right.Evaluate(parser, ii); ii.Engine.Variables.SetVar(left.Name, b); ii.Engine.Variables.SetVar(right.Name, temp); return b; } Func<Parser, Interpreter, NameExpression, Expression, double> assignFunc; if (AssignOperations.TryGetValue(_token.Identifier, out assignFunc)) { var left = _left as NameExpression; if (left == null) throw new ProcessusException(parser.Source, _token, "Left side of assignment was not a variable."); return assignFunc(parser, ii, left, _right); } Func<double, double, double> func; if (!Operations.TryGetValue(_token.Identifier, out func)) { throw new ProcessusException(parser.Source, _token, "Invalid binary operation '" + _token + "'."); } return func(_left.Evaluate(parser, ii), _right.Evaluate(parser, ii)); }
public override double Evaluate(Parser parser, Interpreter ii) { var name = _left as NameExpression; if (name == null) { throw new ProcessusException(parser.Source, _token, "Left side of increment/decrement postfix was not a variable."); } switch (_token.Identifier) { case MathTokenType.Increment: { double d = name.Evaluate(parser, ii); ii.Engine.Variables.SetVar(name.Name, d + 1); return d; } case MathTokenType.Decrement: { double d = name.Evaluate(parser, ii); ii.Engine.Variables.SetVar(name.Name, d - 1); return d; } default: throw new ProcessusException(parser.Source, _token, "Invalid postfix operator '" + _token.Value + "'."); } }
public override double Evaluate(Parser parser, Interpreter ii) { var name = _right as NameExpression; switch (_token.Identifier) { case MathTokenType.Minus: return -_right.Evaluate(parser, ii); case MathTokenType.Increment: { if (name == null) { throw new ProcessusException(parser.Source, _token, "Increment prefix could not find a variable."); } double d = name.Evaluate(parser, ii) + 1; ii.Engine.Variables.SetVar(name.Name, d); return d; } case MathTokenType.Decrement: { if (name == null) { throw new ProcessusException(parser.Source, _token, "Decrement prefix could not find a variable."); } double d = name.Evaluate(parser, ii) - 1; ii.Engine.Variables.SetVar(name.Name, d); return d; } default: throw new ProcessusException(parser.Source, _token, "Invalid prefix operator '" + _token + "'."); } }
public static double Calculate(Interpreter ii, string expression) { double result = 0; foreach (var expr in expression.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) { var p = new Parser(new Lexer(expr.ToStringe())); result = p.ParseExpression().Evaluate(p, ii); } return result; }
public override double Evaluate(Parser parser, Interpreter ii) { return _number; }
public override double Evaluate(Parser parser, Interpreter ii) { var d = ii.Engine.Variables.GetVar(_name.Value); if (d == null) throw new ProcessusException(parser.Source, _name, "Tried to access undefined variable '" + _name.Value + "'."); return d.Value; }
public abstract double Evaluate(Parser parser, Interpreter ii);