Ejemplo n.º 1
0
 public static Object.Object InfixExpression(Object.IntegerObject left, string op, Object.IntegerObject right)
 {
     if (op == "+")
     {
         return(new Object.IntegerObject {
             Value = left.Value + right.Value
         });
     }
     if (op == "-")
     {
         return(new Object.IntegerObject {
             Value = left.Value - right.Value
         });
     }
     if (op == "*")
     {
         return(new Object.IntegerObject {
             Value = left.Value * right.Value
         });
     }
     if (op == "/")
     {
         return(new Object.IntegerObject {
             Value = left.Value / right.Value
         });
     }
     if (op == "^")
     {
         return(new Object.IntegerObject {
             Value = (long)Math.Pow((double)left.Value, (double)right.Value)
         });
     }
     throw new Exception($"Unsupported arithmetic operator {op}");
 }
Ejemplo n.º 2
0
 public static Object.Object EvalPrefixExpression(PrefixExpression expression)
 {
     Object.Object right = Eval(expression.Right);
     Object.IntegerObject rightInteger = right as Object.IntegerObject;
     if(rightInteger == null)
     {
         throw new Exception("Prefix expression right is not Object.IntegerObject");
     }
     return new Object.IntegerObject { Value = -1 * rightInteger.Value };
 }
Ejemplo n.º 3
0
 public static Object.Object EvalInfixExpression(AST.InfixExpression expression)
 {
     Object.Object        left        = Eval(expression.Left);
     Object.IntegerObject leftInteger = left as Object.IntegerObject;
     if (leftInteger == null)
     {
         throw new Exception("Infix expression left branch is not Object.IntegerObject");
     }
     Object.Object        right        = Eval(expression.Right);
     Object.IntegerObject rightInteger = right as Object.IntegerObject;
     if (rightInteger == null)
     {
         throw new Exception("Infix expression right branch is not Object.IntegerObject");
     }
     return(InfixExpression(leftInteger, expression.Operator, rightInteger));
 }