summaryrefslogtreecommitdiff
path: root/src/ast/nodes/CompoundNode.java
blob: ad3420d19881fb0b3b091cb6db40deba91487021 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package ast.nodes;

import ast.types.*;
import java.util.ArrayList;
import semanticanalysis.SemanticError;
import semanticanalysis.SymbolTable;

/**
 * Node for the `compound_node` statement of the grammar.
 */
public class CompoundNode implements Node {

    private final Node ifNode;
    private final Node funcDef;
    private final Node forStmt;
    private final Node whileStmt;

    public CompoundNode(Node ifNode, Node funcDef, Node forStmt, Node whileStmt) {
        this.ifNode = ifNode;
        this.funcDef = funcDef;
        this.forStmt = forStmt;
        this.whileStmt = whileStmt;
    }


    @Override
    public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting, FunctionType ft) {
        ArrayList<SemanticError> errors = new ArrayList<>();

        if (ifNode != null) {
            errors.addAll(ifNode.checkSemantics(ST, _nesting, ft));
        }

        if (funcDef != null) {
            errors.addAll(funcDef.checkSemantics(ST, _nesting, ft));
        }

        if (forStmt != null) {
            errors.addAll(forStmt.checkSemantics(ST, _nesting, ft));
        }

        if (whileStmt != null) {
            errors.addAll(whileStmt.checkSemantics(ST, _nesting, ft));
        }

        return errors;
    }

    @Override
    public Type typeCheck() {
        return new VoidType();
    }

    @Override
    public String codeGeneration() {
        if (ifNode != null) {
            return ifNode.codeGeneration();
        }

        if (funcDef != null) {
            return funcDef.codeGeneration();
        }

        if (forStmt != null) {
            return forStmt.codeGeneration();
        }

        if (whileStmt != null) {
            return whileStmt.codeGeneration();
        }

        return "Error: everything is null in Compound node\n";
    }

    @Override
    public String printAST(String prefix) {
        String str = prefix + "CompoundNode\n";

        prefix += "  ";

        if (ifNode != null) {
            str += ifNode.printAST(prefix);
        }

        if (funcDef != null) {
            str += funcDef.printAST(prefix);
        }

        if (forStmt != null) {
            str += forStmt.printAST(prefix);
        }

        if (whileStmt != null) {
            str += whileStmt.printAST(prefix);
        }

        return str;
    }

    @Override
    public String toPrint(String prefix) {
        String str = "";

        if (ifNode != null) {
            str += ifNode.toPrint(prefix);
        }

        if (funcDef != null) {
            str += funcDef.toPrint(prefix);
        }

        if (forStmt != null) {
            str += forStmt.toPrint(prefix);
        }

        if (whileStmt != null) {
            str += whileStmt.toPrint(prefix);
        }

        return str;

    }

    public Node getForStmt() {
        return forStmt;
    }

    public Node getFuncDef() {
        return funcDef;
    }

    public Node getIfNode() {
        return ifNode;
    }

    public Node getWhileStmt() {
        return whileStmt;
    }
}