Ejemplo n.º 1
0
        public override object Evaluate(IEvaluationContext context = null)
        {
            if (context == null)
            {
                context = Expression.DefaultContext;
            }

            if (_Operator == "=") //Assignment
            {
                if (!(_OperandA is ReferenceExpression))
                {
                    throw new ExpressionException("The left-hand side of an assignment must be a variable.");
                }
                if (_OperandB.ContainsReference(_OperandA.Description))
                {
                    throw new ExpressionException("Cannot assign an expression to a variable that contains a reference to that same variable.  Use := operator to assign current value.");
                }
                context.SetVariable(_OperandA.Description, _OperandB);
                return(_OperandB.Evaluate(context));
            }
            else if (_Operator == ":=") // Assign current value
            {
                if (!(_OperandA is ReferenceExpression))
                {
                    throw new ExpressionException("The left-hand side of an assignment must be a variable.");
                }
                object result = _OperandB.Evaluate(context);
                context.SetVariable(_OperandA.Description, new ValueExpression(result?.ToString(), result));
                return(result);
            }
            else
            {
                dynamic valueA = _OperandA.Evaluate(context);
                dynamic valueB = _OperandB.Evaluate(context);

                //TODO: Check units

                switch (_Operator)
                {
                case "+":
                    return(valueA + valueB);

                case "-":
                    return(valueA - valueB);

                case "*":
                case ".":
                    return(valueA * valueB);    //TODO: Set units

                case "/":
                case "÷":
                    return(valueA / valueB);

                case "==":
                case "↔":
                case "≡":
                    return(valueA == valueB);

                case "!=":
                case "≠":
                    return(valueA != valueB);

                case ">":
                    return(valueA > valueB);

                case ">=":
                case "≥":
                    return(valueA >= valueB);

                case "<":
                    return(valueA < valueB);

                case "<=":
                case "≤":
                    return(valueA <= valueB);

                case "AND":
                case "^":
                case "&":
                    return(valueA & valueB);

                case "OR":
                case "˅":
                    return(valueA || valueB);

                case "NAND":
                case "↑":
                    return(!(valueA & valueB));

                case "NOR":
                case "↓":
                    return(!(valueA | valueB));

                case "XOR":
                case "⊕":
                    return(valueA ^ valueB);

                case "²":
                    return(valueA * valueA * valueB);

                case "³":
                    return(valueA * valueA * valueA * valueB);
                }
            }
            throw new ExpressionException("Invalid syntax.  Expression could not be evaluated.");
        }
Ejemplo n.º 2
0
 public void SetValue(object newValue)
 {
     _evaluationContext.SetVariable(_name, newValue);
 }
Ejemplo n.º 3
0
 public void SetVariable(string name, object value)
 {
     _relatedContext.SetVariable(name, value);
 }