示例#1
0
 public AnnotatedForStatement(VariableSymbol variable, AnnotatedExpression lowerBound, AnnotatedExpression upperBound, AnnotatedStatement body)
 {
     Variable   = variable;
     LowerBound = lowerBound;
     UpperBound = upperBound;
     Body       = body;
 }
示例#2
0
        private object EvaluateExpression(AnnotatedExpression root)
        {
            if (root is AnnotatedTypeCastExpression t)
            {
                var left = EvaluateExpression(t.Left);
                return(PerformTypeCast(left, t.Right));
            }
            if (root is AnnotatedLiteralExpression n)
            {
                return(n.Value);
            }
            if (root is AnnotatedVariableExpression v)
            {
                return(_symbolTable[v.Symbol]);
            }
            if (root is AnnotatedAssignmentExpression a)
            {
                var value = EvaluateExpression(a.Expression);
                _symbolTable[a.Symbol] = value;
                return(value);
            }
            if (root is AnnotatedUnaryExpression u)
            {
                object operand = EvaluateExpression(u.Operand);
                switch (u.Operator.Kind)
                {
                case AnnotatedUnaryOperatorKind.Identity: return((int)operand);

                case AnnotatedUnaryOperatorKind.Negation: return(-(int)operand);

                case AnnotatedUnaryOperatorKind.LogicalNegation: return(!(bool)operand);

                default: throw new Exception($"Unexpected Unary expression {u.Operator.Kind}");
                }
            }
            if (root is AnnotatedBinaryExpression b)
            {
                object left  = EvaluateExpression(b.Left);
                object right = EvaluateExpression(b.Right);

                switch (b.Operator.Kind)
                {
                case AnnotatedBinaryOperatorKind.Addition: return((int)left + (int)right);

                case AnnotatedBinaryOperatorKind.Subtraction: return((int)left - (int)right);

                case AnnotatedBinaryOperatorKind.Multiplication: return((int)left * (int)right);

                case AnnotatedBinaryOperatorKind.Division: {
                    if ((int)right == 0)
                    {
                        throw new Exception("ERROR: Division by Zero");
                    }
                    return((int)left / (int)right);
                }

                case AnnotatedBinaryOperatorKind.LogicalAnd: return((bool)left && (bool)right);

                case AnnotatedBinaryOperatorKind.LogicalOr: return((bool)left || (bool)right);

                case AnnotatedBinaryOperatorKind.Xor: return((int)left ^ (int)right);

                case AnnotatedBinaryOperatorKind.And: return((int)left & (int)right);

                case AnnotatedBinaryOperatorKind.Or: return((int)left | (int)right);

                case AnnotatedBinaryOperatorKind.Equality: return(Equals(left, right));

                case AnnotatedBinaryOperatorKind.Inequality:
                    return(!Equals(left, right));

                case AnnotatedBinaryOperatorKind.GreaterThan: return((int)left > (int)right);

                case AnnotatedBinaryOperatorKind.GreaterThanEqual: return((int)left >= (int)right);

                case AnnotatedBinaryOperatorKind.LessThan: return((int)left < (int)right);

                case AnnotatedBinaryOperatorKind.LessThanEqual: return((int)left <= (int)right);

                default: throw new Exception($"Unexpected binary operator {b.Operator.Kind}");
                }
            }
            throw new Exception($"Unexpected node {root.Kind}");
        }
示例#3
0
 public AnnotatedWhileStatement(AnnotatedExpression condition, AnnotatedStatement body)
 {
     Condition = condition;
     Body      = body;
 }
示例#4
0
 public AnnotateVariableDeclaration(VariableSymbol variable, AnnotatedExpression initializer)
 {
     Variable    = variable;
     Initializer = initializer;
 }
示例#5
0
 public AnnotatedIfStatement(AnnotatedExpression condition, AnnotatedStatement thenStatement, AnnotatedStatement elseStatement)
 {
     Condition     = condition;
     ThenStatement = thenStatement;
     ElseStatement = elseStatement;
 }