aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIfTests.java
blob: 8f21f3228524cb189f1f14178779f7cd2b9519ac (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 org.shedlang.compiler.shid;

import org.junit.jupiter.api.Test;
import org.shedlang.compiler.ast.typed.Typed;
import org.shedlang.compiler.types.Types;

import java.util.List;

import static org.shedlang.compiler.shid.instructions.ShidMatchers.*;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.isSequence;

public class ShidCodeGenIfTests {
    @Test
    public void ifWithSingleConditionalBranchIsCompiledToSingleIfInstruction() {
        var node = Typed.if_(
            List.of(
                Typed.conditionalBranch(
                    Typed.reference("c"),
                    Typed.block(List.of(
                        Typed.blockReturn(Typed.intLiteral(42))
                    ))
                )
            ),
            Typed.block(List.of(
                Typed.functionReturn(Typed.stringLiteral("x"))
            )),
            Types.INT
        );
        var context = ShidCodeGen.Context.EMPTY
            .addShedVal("c");

        var result = ShidCodeGen.compileExpression(node, context);

        assertThat(result.instructions(), isSequence(
            isShidLocalLoad("c__0"),
            isShidIf(
                isShidBlock(isSequence(
                    isShidPushInt(42)
                )),
                isShidBlock(isSequence(
                    isShidBlock(isSequence(
                        isShidPushString("x"),
                        isShidReturn()
                    ))
                ))
            )
        ));
    }

    @Test
    public void ifWithMultipleConditionalBranchesIsCompiledToNestedIfInstructions() {
        var node = Typed.if_(
            List.of(
                Typed.conditionalBranch(
                    Typed.reference("c"),
                    Typed.block(List.of(
                        Typed.blockReturn(Typed.intLiteral(42))
                    ))
                ),
                Typed.conditionalBranch(
                    Typed.reference("d"),
                    Typed.block(List.of(
                        Typed.blockReturn(Typed.intLiteral(1))
                    ))
                )
            ),
            Typed.block(List.of(
                Typed.functionReturn(Typed.stringLiteral("x"))
            )),
            Types.INT
        );
        var context = ShidCodeGen.Context.EMPTY
            .addShedVal("c")
            .addShedVal("d");

        var result = ShidCodeGen.compileExpression(node, context);

        assertThat(result.instructions(), isSequence(
            isShidLocalLoad("c__0"),
            isShidIf(
                isShidBlock(isSequence(
                    isShidPushInt(42)
                )),
                isShidBlock(isSequence(
                    isShidLocalLoad("d__0"),
                    isShidIf(
                        isShidBlock(isSequence(
                            isShidPushInt(1)
                        )),
                        isShidBlock(isSequence(
                            isShidBlock(isSequence(
                                isShidPushString("x"),
                                isShidReturn()
                            ))
                        ))
                    )
                ))
            )
        ));
    }
}