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

/**
 * 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;

    public FuncdefNode(TerminalNode name, Node paramlist, Node block) {
        this.name = name;
        this.paramlist = paramlist;
        this.block = block;
    }

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

        ST.insert(this.name.toString(), ft, _nesting, "");

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

        ST.add(HM);

        ST.insert(this.name.toString(), ft, _nesting + 1, "");

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

        // TODO: think to the fucking offset
        // Offset is increased for the possible return value
        ST.increaseoffset();

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

        ST.remove();

        return errors;
    }

    // FIXME: this type must be the same of the return stmt variable
    @Override
    public Type typeCheck() {
        return new VoidType();
    }

    // TODO: code generation for funcdef
    @Override
    public String codeGeneration() {
        return "";
    }

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

        prefix += "  ";

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

        str += block.toPrint(prefix);

        return str;
    }

}