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

import ast.types.*;
import java.util.ArrayList;

import semanticanalysis.STentry;
import semanticanalysis.SemanticError;
import semanticanalysis.SymbolTable;

/**
 * Node for the `assignment` statement of the grammar.
 */
public class AssignmentNode implements Node {

    private final ExprListNode lhr;
    private final Node assign;
    private final ExprListNode rhr;

    // Useful for code gen
    private int offset;
    private boolean alreadyDef;

    public AssignmentNode(Node lhr, Node assign, Node rhr) {
        this.lhr = (ExprListNode) lhr;
        this.assign = assign;
        this.rhr = (ExprListNode) rhr;
        this.alreadyDef = false;
    }

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

        // DO NOT CHECK lhr
        errors.addAll(assign.checkSemantics(ST, _nesting, ft));
        errors.addAll(rhr.checkSemantics(ST, _nesting, ft));

        int lsize = lhr.getSize();

        for (int i = 0; i < lsize; i++) {
            ExprNode leftAtom = (ExprNode) lhr.getElem(i);
            STentry e = ST.lookup(leftAtom.getId());
            if (ft != null) {
                ft.addLocalVar();
            }

            if (e == null) {
                ST.insert(leftAtom.getId(), new AtomType(), _nesting, "");
                e = ST.lookup(leftAtom.getId());
            } else {
                int ns = e.getNesting();
                if (_nesting == ns) {
                    this.alreadyDef = true;
                }
            }
            offset = e.getOffset();
        }
        return errors;
    }

    // TODO: check it out for this type
    @Override
    public Type typeCheck() {
        return rhr.typeCheck();
    }

    @Override
    public String codeGeneration() {
        String rhrString = rhr.codeGeneration();

        String lhrString = "";
        ExprNode leftAtom = (ExprNode) lhr.getElem(0);

        // The code generation for the left atom returns a `store A0 0(T1)` at
        // the end but we do not want that command.
        // So, we'll have a string with the substring + the offset.
        String leftAtomCode = leftAtom.codeGeneration();
        lhrString += leftAtomCode.substring(0, leftAtomCode.length() - 17) + offset;

        // If the variable name is previously defined it'll load the variable
        // from the `0(T1)`, otherwise it'll push the `A0` register at the top
        // of the stack.
        if (!this.alreadyDef) {
            lhrString += "\npushr A0\n";
        } else {
            lhrString += "\nload A0 0(T1)\n";
        }

        return rhrString + lhrString;
    }

    @Override
    public String toPrint(String prefix) {
        return prefix + "Assignment\n" + lhr.toPrint(prefix + "  ") + assign.toPrint(prefix + "  ")
                + rhr.toPrint(prefix + "  ");
    }

}