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

import java.util.ArrayList;
import java.util.HashMap;

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

/**
 * Node for the `funcdef` statement of the grammar.
 */
public class FuncdefNode implements Node {

    private final TerminalNode name;
    private final Node paramlist;
    private final Node block;
    private final String funLabel;

    public FuncdefNode(TerminalNode name, Node paramlist, Node block) {
        this.name = name;
        if (paramlist != null) {
            this.paramlist = paramlist;
        } else {
            this.paramlist = new ParamlistNode(new ArrayList<Node>());
        }
        this.block = block;
        this.funLabel = Label.newFun("FUN");
    }

    @Override
    public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting, FunctionType ft) {
        ArrayList<SemanticError> errors = new ArrayList<>();
        int paramNumber = 0;
        if (paramlist != null) {
            paramNumber = ((ParamlistNode) paramlist).getParamNumber();
        }
        Type returnType = this.block.typeCheck();
        FunctionType newFt = new FunctionType(paramNumber, returnType, funLabel);

        String funName = this.name.toString();
        ST.insert(funName, newFt, _nesting, "");

        HashMap<String, STentry> HM = new HashMap<>();

        ST.add(HM);

        ST.insert(funName, newFt, _nesting + 1, "");
        ST.decreaseOffset();

        if (paramlist != null) {
            errors.addAll(paramlist.checkSemantics(ST, _nesting + 1, newFt));
        }

        // Offset is increased for the possible return value
        ST.increaseOffset();

        errors.addAll(block.checkSemantics(ST, _nesting + 1, newFt));

        // return to the outer block
        ST.remove();

        return errors;
    }

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

    /**
     * Taken from slide 56 of CodeGeneration.pdf
     */
    @Override
    public String codeGeneration() {
        String blockS = block.codeGeneration();
        // The "return" which fix the RA is inside the block
        String funS = funLabel + ":\n" +
                "pushr RA\n" +
                blockS;

        Label.addFunDef(funS);
        return "";
    }

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

        prefix += "  ";

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

        str += block.printAST(prefix);

        return str;
    }

    @Override
    public String toPrint(String prefix) {
        String str = "\n" + prefix + "def " + this.name + '(';
        if (paramlist != null) {
            str += paramlist.toPrint("");
        }

        str += "):\n";
        str += block.toPrint(prefix + "\t") + "\n";
        return str;
    }

}