Example #1
0
        private object EvaluateExpression(SemanticExpression node)
        {
            //Convert the semantic representation into a concrete one
            switch (node)
            {
            case SemanticLiteralExpression n:
                return(n.Value);

            case SemanticVariableExpression v:
                return(_variables[v.Variable]);

            case SemanticAssignmentExpression a:
            {
                var value = EvaluateExpression(a.Expression);
                _variables[a.Variable] = value;
                return(value);
            }

            case SemanticUnaryExpression u:
            {
                var operand = EvaluateExpression(u.Operand);

                return(u.Operator.Kind switch
                    {
                        SemanticUnaryOperatorKind.Identity => (object)(int)operand,
                        SemanticUnaryOperatorKind.Negative => (-1 * (int)operand),
                        SemanticUnaryOperatorKind.LogicalNegation => !(bool)operand,
                        SemanticUnaryOperatorKind.BitwiseNegation => ~(int)operand,
                        _ => throw new Exception($"Unexpected unary operator {u.Operator.Kind}")
                    });
            }
Example #2
0
 public Evaluator(SemanticExpression root, Dictionary <VariableSymbol, object> variables)
 {
     _root      = root;
     _variables = variables;
 }