Esempio n. 1
0
 public AssignExpressionNode(AssignOperations operation, WritableNode left, ExpressionNode value, SourceSpan sourceSpan)
     : base(sourceSpan)
 {
     this.operation = operation;
     this.left = left;
     this.value = value;
 }
Esempio n. 2
0
        public override double Evaluate(MathParser parser, VM ii)
        {
            if (_token.ID == RMathToken.Swap)
            {
                var left  = _left as NameExpression;
                var right = _right as NameExpression;
                if (left == null)
                {
                    throw new RantException(parser.Source, _token, "Left side of swap operation was not a variable.");
                }
                if (right == null)
                {
                    throw new RantException(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 <MathParser, VM, NameExpression, Expression, double> assignFunc;

            if (AssignOperations.TryGetValue(_token.ID, out assignFunc))
            {
                var left = _left as NameExpression;
                if (left == null)
                {
                    throw new RantException(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.ID, out func))
            {
                throw new RantException(parser.Source, _token, "Invalid binary operation '" + _token + "'.");
            }
            return(func(_left.Evaluate(parser, ii), _right.Evaluate(parser, ii)));
        }