summaryrefslogtreecommitdiff
path: root/src/ast/nodes/TrailerNode.java
blob: d70e962f8c495ef067c5386b49fe5e26b39ab786 (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
package ast.nodes;

import java.util.ArrayList;

import semanticanalysis.SemanticError;
import semanticanalysis.SymbolTable;
import ast.types.*;
import org.antlr.v4.runtime.tree.TerminalNode;

/**
 * Node for the `trailer` statement of the grammar.
 */
public class TrailerNode implements Node {

    private final Node arglist;
    private final ArrayList<Node> exprs;
    private final TerminalNode methodCall;
    private final boolean isParenthesis;
    private final boolean isEmpty;

    public TrailerNode(Node arglist, ArrayList<Node> exprs, TerminalNode methodCall, boolean isParenthesis) {
        this.arglist = arglist;
        this.exprs = exprs;
        this.methodCall = methodCall;
        this.isParenthesis = isParenthesis;

        this.isEmpty = (this.arglist == null && this.exprs.isEmpty() && this.methodCall == null);
    }

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

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

        for (var expr : exprs) {
            errors.addAll(expr.checkSemantics(ST, _nesting, ft));
        }

        return errors;
    }

    public int getArgumentNumber() {
        if (arglist == null) {
            return 0;
        }

        return ((ArglistNode) arglist).getArgumentNumber();
    }

    public boolean isParenthesis() {
        return this.isParenthesis;
    }

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

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

    @Override
    public String toPrint(String prefix) {
        String str = prefix + "TrailerNode\n";

        prefix += "  ";

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

        for (var expr : exprs) {
            str += expr.toPrint(prefix);
        }

        if (methodCall != null) {
            str += prefix + "Method(" + methodCall + ")\n";
        }

        if (isEmpty) {
            str += prefix + "()\n";
        }

        return str;
    }

}