Exemple #1
0
        object IVisitor <object> .visit(AssignmentNode node)
        {
            Node left  = node.getLeft();
            Node right = node.getRight();

            left.accept(this);
            right.accept(this);

            checkSameType(left, right);
            return(null);
        }
        private Node stmt()
        {
            Node statement = new StatementNode();

            if (currentToken.type == Token.KW)
            {
                // For loop detected
                if (currentToken.value == "for")
                {
                    Node forLoop = new ForLoopNode();
                    match_keyword("for");

                    // Control node
                    Node control = new ForControlNode();
                    // Condition
                    Node condition = new ForConditionNode();

                    // Control node assignment
                    Node assignment = new AssignmentNode();
                    Node id         = new IdNode(currentToken);

                    assignment.addChild(id);
                    condition.addChild(id);

                    match(Token.ID);
                    match_keyword("in");
                    assignment.addChild(expr());
                    match(Token.RANGE);

                    condition.addChild(expr());

                    match_keyword("do");
                    control.addChild(assignment);
                    control.addChild(condition);
                    forLoop.addChild(control);
                    statement.addChild(forLoop);

                    // Add block
                    Node block = new BlockNode();

                    while (!accept_keyword("end"))
                    {
                        block.addChild(stmt());
                    }
                    forLoop.addChild(block);

                    match_keyword("end");
                    match_keyword("for");
                }
                // Read detected
                else if (currentToken.value == "read")
                {
                    Node read = new ReadNode();
                    statement.addChild(read);
                    match_keyword("read");
                    read.addChild(new IdNode(currentToken));
                    match(Token.ID);
                }
                // Print detected
                else if (currentToken.value == "print")
                {
                    Node print = new PrintNode();
                    statement.addChild(print);
                    match_keyword("print");
                    print.addChild(expr());
                }
                // Assert detected
                else if (currentToken.value == "assert")
                {
                    Node assert = new AssertNode();
                    match_keyword("assert");
                    match(Token.LPAR);
                    assert.addChild(expr());
                    match(Token.RPAR);
                    statement.addChild(assert);
                }
                // Variable declaration detected
                else if (currentToken.value == "var")
                {
                    Node declaration = new DeclarationNode();
                    match_keyword("var");
                    Node id = new IdNode(currentToken);
                    declaration.addChild(id);
                    match(Token.ID);
                    match(Token.COL);

                    if (accept_keyword("int"))
                    {
                        id.type = Token.INT;
                        match_keyword("int");
                    }
                    else if (accept_keyword("string"))
                    {
                        id.type = Token.STRING;
                        match_keyword("string");
                    }
                    else if (accept_keyword("bool"))
                    {
                        id.type = Token.BOOL;
                        match_keyword("bool");
                    }

                    else
                    {
                        new SyntaxError(currentToken, "Syntax Error: Expected type int, string or bool, got '" + currentToken.value + "'");
                        nextStatement();
                        return(null);
                    }

                    statement.addChild(declaration);

                    // Optional assignment
                    if (accept(Token.ASS))
                    {
                        Node assignment = new AssignmentNode();
                        assignment.addChild(id);
                        match(Token.ASS);
                        assignment.addChild(expr());
                        statement.addChild(assignment);
                    }
                }
                else
                {
                    new SyntaxError(currentToken, "Syntax Error: Expected keyword var, for, read, print or assert, got " + currentToken.value);
                    statement = new StatementNode();
                    return(null);
                }
                // End of statement
                match(Token.SCOL);
            }
            // Assignment detected
            else if (currentToken.type == Token.ID)
            {
                Node id = new IdNode(currentToken);
                match(Token.ID);
                Node assignment = new AssignmentNode();
                assignment.addChild(id);
                match(Token.ASS);
                assignment.addChild(expr());
                statement.addChild(assignment);
                match(Token.SCOL);
            }

            return(statement);
        }
 object IVisitor <object> .visit(AssignmentNode node)
 {
     SymbolTable.assign(node.getLeft().value, node.getRight().accept(new Evaluator()).ToString());
     return(null);
 }
Exemple #4
0
 object IVisitor <object> .visit(AssignmentNode node)
 {
     return(1);
 }