コード例 #1
0
 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 ManhoodException(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 ManhoodException(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 ManhoodException(parser.Source, _token, "Invalid prefix operator '" + _token + "'.");
     }
 }
コード例 #2
0
        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 ManhoodException(parser.Source, _token, "Left side of swap operation was not a variable.");
                if (right == null) throw new ManhoodException(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 ManhoodException(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 ManhoodException(parser.Source, _token, "Invalid binary operation '" + _token + "'.");
            }
            return func(_left.Evaluate(parser, ii), _right.Evaluate(parser, ii));
        }
コード例 #3
0
 public override double Evaluate(Parser parser, Interpreter ii)
 {
     var name = _left as NameExpression;
     if (name == null)
     {
         throw new ManhoodException(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 ManhoodException(parser.Source, _token, "Invalid postfix operator '" + _token.Value + "'.");
     }
 }
コード例 #4
0
ファイル: Parser.cs プロジェクト: dandrews/Manhood
 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;
 }
コード例 #5
0
ファイル: NumberExpression.cs プロジェクト: dandrews/Manhood
 public override double Evaluate(Parser parser, Interpreter ii)
 {
     return _number;
 }
コード例 #6
0
ファイル: Expression.cs プロジェクト: dandrews/Manhood
 public abstract double Evaluate(Parser parser, Interpreter ii);
コード例 #7
0
ファイル: NameExpression.cs プロジェクト: dandrews/Manhood
 public override double Evaluate(Parser parser, Interpreter ii)
 {
     var d = ii.Engine.Variables.GetVar(_name.Value);
     if (d == null) throw new ManhoodException(parser.Source, _name, "Tried to access undefined variable '" + _name.Value + "'.");
     return d.Value;
 }