示例#1
0
        public void Visit(AddOpNode n)
        {
            var children = n.GetChildren();

            foreach (var node in children)
            {
                node.SymTable = n.SymTable;
                node.Accept(this);
            }

            if (!string.Equals(children[0].ExprType.Type, TypeConstants.IntType) &&
                !string.Equals(children[1].ExprType.Type, TypeConstants.IntType) &&
                !string.Equals(children[0].ExprType.Type, TypeConstants.FloatType) &&
                !string.Equals(children[1].ExprType.Type, TypeConstants.FloatType))
            {
                _errorStream.WriteLine($"Operation must be done with numerical operands! ({n.Token.StartLine}:{n.Token.StartColumn})");
                Console.WriteLine($"Error: Operation must be done with numerical operands! ({n.Token.StartLine}:{n.Token.StartColumn})");
            }

            if (children[0].ExprType.Dims.Count > 0 || children[1].ExprType.Dims.Count > 0)
            {
                _errorStream.WriteLine($"Cannot multiply/divide arrays! ({n.Token.StartLine}:{n.Token.StartColumn})");
                Console.WriteLine($"Error: Cannot multiply/divide arrays! ({n.Token.StartLine}:{n.Token.StartColumn})");
            }
            else if (!string.Equals(children[0].ExprType.Type, children[1].ExprType.Type))
            {
                _errorStream.WriteLine($"Operand types don't match! {children[1].ExprType.Type} <-> {children[0].ExprType.Type} ({n.Token.StartLine}:{n.Token.StartColumn})");
                Console.WriteLine($"Error: Operand types don't match! {children[1].ExprType.Type} <-> {children[0].ExprType.Type} ({n.Token.StartLine}:{n.Token.StartColumn})");
            }

            n.ExprType = children[0].ExprType;
        }
示例#2
0
        private Node SumExpression()
        {
            // term ((PLUS^|MINUS^) term)*
            Node      termExpression = Term();
            TokenType next           = LookAhead(1);

            if (LookAhead(2) != TokenType.STRING_LITERAL)
            {
                while (next == TokenType.PLUS ||
                       next == TokenType.PLUS_PLUS ||
                       next == TokenType.PLUS_ASSIGN ||
                       next == TokenType.MINUS ||
                       next == TokenType.MINUS_MINUS ||
                       next == TokenType.MINUS_ASSIGN)
                {
                    if (next == TokenType.PLUS)
                    {
                        termExpression = new AddOpNode(Match(TokenType.PLUS).Position, termExpression, Term());
                    }
                    else if (next == TokenType.PLUS_PLUS)
                    {
                        Node increment = new IntegerNode(Match(TokenType.PLUS_PLUS).Position, 1);
                        Node addNode   = new AddOpNode(increment.Position, termExpression, increment);
                        termExpression = new AssignNode(termExpression.Position, termExpression, addNode, false);
                    }
                    else if (next == TokenType.PLUS_ASSIGN)
                    {
                        Token plusAssign = Match(TokenType.PLUS_ASSIGN);
                        Node  increment  = Expression();
                        Node  addNode    = new AddOpNode(plusAssign.Position, termExpression, increment);
                        termExpression =
                            new AssignNode(termExpression.Position, termExpression, addNode, false);
                    }
                    else if (next == TokenType.MINUS)
                    {
                        termExpression =
                            new SubtractOpNode(Match(TokenType.MINUS).Position, termExpression, Term());
                    }
                    else if (next == TokenType.MINUS_MINUS)
                    {
                        Node deincrement  = new IntegerNode(Match(TokenType.MINUS_MINUS).Position, 1);
                        Node subtractNode = new SubtractOpNode(deincrement.Position, termExpression, deincrement);
                        termExpression =
                            new AssignNode(termExpression.Position, termExpression, subtractNode, false);
                    }
                    else if (next == TokenType.MINUS_ASSIGN)
                    {
                        Token minusAssign  = Match(TokenType.MINUS_ASSIGN);
                        Node  deincrement  = Expression();
                        Node  subtractNode = new SubtractOpNode(minusAssign.Position, termExpression, deincrement);
                        termExpression =
                            new AssignNode(termExpression.Position, termExpression, subtractNode, false);
                    }

                    next = LookAhead(1);
                }
            }
            return(termExpression);
        }
示例#3
0
 public void Visit(AddOpNode n)
 {
     PrintDOTIDLabel(n, n.Op.ToString());
     PrintDOTParentChild(n);
     foreach (var child in n.GetChildren())
     {
         child.Accept(this);
     }
 }
示例#4
0
        public void Visit(AddOpNode n)
        {
            var children = n.GetChildren();

            foreach (var child in children)
            {
                child.SymTable = n.SymTable;
                child.Accept(this);
            }
        }
        public void Visit(AddOpNode n)
        {
            var table = (FunctionSymbolTableEntry)n.SymTable;

            n.TemporaryVariableName = table.MemoryLayout.AddTemporaryVariable();

            var children = n.GetChildren();

            foreach (var child in children)
            {
                child.Accept(this);
            }
        }
示例#6
0
        public void Visit(AddOpNode n)
        {
            var children     = n.GetChildren();
            var table        = (FunctionSymbolTableEntry)n.SymTable;
            var resultOffset = table.MemoryLayout.GetOffset(n.TemporaryVariableName);

            var operandVarOffsets = new List <int>();

            foreach (var child in children)
            {
                child.Accept(this);
                var offset = table.MemoryLayout.GetOffset(child.TemporaryVariableName);
                operandVarOffsets.Add(offset);
            }

            Instructions instruction;

            switch (n.Op)
            {
            case AddOp.Add:
                instruction = Instructions.Add;
                break;

            case AddOp.Subtract:
                instruction = Instructions.Sub;
                break;

            case AddOp.Or:
                instruction = Instructions.Or;
                break;

            default:
                throw new InvalidOperationException("Unknown op");
            }

            _writer.WriteComment($"AddOp ({n.Op}) at ({n.Token.StartLine} : {n.Token.StartColumn})");

            var destReg = PopRegister();
            var op1Reg  = PopRegister();
            var op2Reg  = PopRegister();

            _writer.WriteInstruction(Instructions.Lw, op1Reg, $"{operandVarOffsets[0]}({FSPReg})");
            _writer.WriteInstruction(Instructions.Lw, op2Reg, $"{operandVarOffsets[1]}({FSPReg})");
            _writer.WriteInstruction(instruction, destReg, op2Reg, op1Reg);
            _writer.WriteInstruction(Instructions.Sw, $"{resultOffset}({FSPReg})", destReg);

            PushRegister(op2Reg);
            PushRegister(op1Reg);
            PushRegister(destReg);
        }