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

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

/**
 * Node for the `if` statement of the grammar.
 */
public class IfNode implements Node {

    private final Node guard;
    private final Node thenBranch;
    private final Node elseBranch;

    public IfNode(Node guard, Node thenBranch, Node elseBranch) {
        this.guard = guard;
        this.thenBranch = thenBranch;
        this.elseBranch = elseBranch;
    }

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

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

        return errors;
    }

    @Override
    public Type typeCheck() {
        if (guard.typeCheck() instanceof BoolType) {
            Type thenexp = thenBranch.typeCheck();
            Type elseexp = elseBranch.typeCheck();
            if (thenexp.getClass().equals(elseexp.getClass())) {
                return thenexp;
            } else {
                System.out.println("Type Error: incompatible types in then and else branches.");
                return new ErrorType();
            }
        } else {
            System.out.println("Type Error: non boolean condition in if.");
            return new ErrorType();
        }
    }

    @Override
    public String codeGeneration() {
        String thenLabel = Label.newBasic("then");
        String endLabel = Label.newBasic("end");

        String guardS = guard.codeGeneration();
        String thenS = thenBranch.codeGeneration();
        String elseS = "";
        if (elseBranch != null) {
            elseS = elseBranch.codeGeneration();
        }

        // We're assuming that the guard is boolean or an operation which puts a
        // true (1) or false (0) into A0.
        return guardS +
                "storei T1 1\n" +
                // Check if A0 = true
                "beq A0 T1 " + thenLabel + "\n" +
                elseS +
                "b " + endLabel + "\n" +
                thenLabel + ":\n" +
                thenS +
                endLabel + ":\n";
    }

    @Override
    public String printAST(String prefix) {
        String str = prefix + "If\n" + guard.printAST(prefix + "  ") + thenBranch.printAST(prefix + "  ");

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

        return str;
    }

    @Override
    public String toPrint(String prefix) {
        String str = prefix + "if ";
        str += guard.toPrint("") + ":\n";
        str += thenBranch.toPrint(prefix + "\t") + "\n";
        if (elseBranch != null) {
            str += prefix + "else:\n";
            str += elseBranch.toPrint(prefix + "\t") + "\n";
        }
        return str;
    }

}