예제 #1
0
        public int Visit(BinaryOperationNode node)
        {
            var left  = node.Left.Accept(this);
            var right = node.Right.Accept(this);

            if ("+".Equals(node.Value))
            {
                return(left + right);
            }
            else
            if ("-".Equals(node.Value))
            {
                return(left - right);
            }
            else
            if ("*".Equals(node.Value))
            {
                return(left * right);
            }
            else
            if ("/".Equals(node.Value))
            {
                return(left / right);
            }

            return(0);
        }
예제 #2
0
        public string Comparator(BinaryOperationNode bnode, Dictionary <string, int> variableIndex)
        {
            string instructions = converter.ConvertToCode(StandardBinaryOperation(bnode, variableIndex));

            string zeroFlag = converter.ConvertToCode("cmp eax, ecx", "lahf", "shr eax, 14", "and eax, 1");
            string signFlag = converter.ConvertToCode("cmp eax, ecx", "lahf", "shr eax, 15", "and eax, 1");
            string comb     = zeroFlag + converter.ConvertToCode("mov edx, eax", "lahf", "shr eax, 14", "and eax, 1");

            string op     = "";
            string negate = converter.ConvertToCode("not eax", "and eax, 1");

            switch (bnode.operation)
            {
            case "==": op = zeroFlag; break;

            case "<": op = signFlag; break;

            case "<=": op = comb; break;

            case "!=": op = zeroFlag + negate;  break;

            case ">=": op = signFlag + negate;  break;

            case ">": op = comb + negate; break;

            default: throw new Exception("Comparator " + bnode.operation + " is not being converted.");
            }

            return(instructions + op);
        }
예제 #3
0
        public AbstractSyntaxTreeNode Expr()
        {
            var result = Term();

            while ("+".Equals(currentToken.Value) || "-".Equals(currentToken.Value))
            {
                var token = currentToken;
                result = new BinaryOperationNode(result, Term(), token);
            }

            return(result);
        }
예제 #4
0
        private AbstractSyntaxTreeNode Term()
        {
            var result = Factor();

            currentToken = lexer.NextToken();

            while ("*".Equals(currentToken.Value) || "/".Equals(currentToken.Value))
            {
                var token = currentToken;
                result = new BinaryOperationNode(result, Factor(), token);

                currentToken = lexer.NextToken();
            }

            return(result);
        }
예제 #5
0
        public string BasicBinaryOperationNode(BinaryOperationNode bnode, Dictionary <string, int> variableIndex)
        {
            string op = null;

            if (comparators.Contains(bnode.operation))
            {
                return(Comparator(bnode, variableIndex));
            }

            switch (bnode.operation)
            {
            case "+": op = "add";   break;

            case "-": op = "sub";   break;

            case "*": op = "imul";  break;
            }

            return(StandardBinaryOperation(bnode, variableIndex) + converter.ConvertToCode(op + " eax, ecx"));
        }
        public type GetType(BinaryOperationNode bon, Environment env)
        {
            type t1 = GetType(bon.left, env);
            type t2 = GetType(bon.right, env);

            if (bon.operation == "==")
            {
                return(new BoolType());
            }

            if (ExpressionMatches(t1, t2))
            {
                if (t1 is FixedType && !(t2 is FixedType))
                {
                    return(t2);
                }

                return(t1);
            }

            throw new Exception("type mismatch" + bon.ToString());
        }
예제 #7
0
 public string StandardBinaryOperation(BinaryOperationNode bnode, Dictionary <string, int> variableIndex)
 {
     return(converter.ConvertToCode(converter.Convert(bnode.right, variableIndex), "push eax"
                                    , converter.Convert(bnode.left, variableIndex), "pop ecx"));
 }
 public string Print(BinaryOperationNode bnode)
 {
     return("(" + Print(bnode.left) + ")"
            + bnode.operation + "(" + Print(bnode.right) + ")");
 }