aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionDefinitionTests.java
blob: 74980441f82ea478daf5087bb5c593b223b10362 (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
package org.shedlang.compiler.shid;

import org.junit.jupiter.api.Test;
import org.shedlang.compiler.ast.typed.Typed;
import org.shedlang.compiler.ast.typed.TypedFunctionDefinitionNode;
import org.shedlang.compiler.shid.instructions.ShidDefineFunction;
import org.shedlang.compiler.types.Types;

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

public class ShidCodeGenFunctionDefinitionTests {
    @Test
    public void functionIsCompiledToDefineFunctionInstruction() {
        var node = TypedFunctionDefinitionNode.builder()
            .withName("f")
            .addPositionalParam("x", Typed.typeLevelReference("String", Types.STRING))
            .addNamedParam("y", Typed.typeLevelReference("Int", Types.INT))
            .addFreeVariable("z")
            .addStatement(Typed.expressionStatement(Typed.stringLiteral("hello")))
            .build();
        var context = ShidCodeGen.Context.EMPTY
            .addShedVal("f")
            .addShedVal("z");

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

        assertThat(result.instructions(), isSequence(
            instanceOf(
                ShidDefineFunction.class,
                has("name", x -> x.name(), equalTo("f")),
                has("positionalParams", x -> x.positionalParams(), isSequence(
                    isShidDefineFunctionParam("x__0")
                )),
                has("namedParams", x -> x.namedParams(), isSequence(
                    isShidDefineFunctionParam("y__0")
                )),
                has("freeVariables", x -> x.freeVariables(), containsExactly(
                    has("name", x -> x.name(), equalTo("z__0"))
                )),
                has("body", x -> x.body(), isShidBlock(isSequence(
                    isShidPushString("hello"),
                    isShidDiscard(),
                    isShidPushUnit(),
                    isShidReturn()
                )))
            ),
            isShidLocalStore("f__0")
        ));
    }

    @Test
    public void whenLastStatementIsBlockReturnThenThatValueIsReturned() {
        var node = TypedFunctionDefinitionNode.builder()
            .withName("f")
            .addStatement(Typed.blockReturn(Typed.stringLiteral("hello")))
            .build();
        var context = ShidCodeGen.Context.EMPTY
            .addShedVal("f");

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

        assertThat(result.instructions(), isSequence(
            instanceOf(
                ShidDefineFunction.class,
                has("body", x -> x.body(), isShidBlock(isSequence(
                    isShidPushString("hello"),
                    isShidReturn()
                )))
            ),
            isShidLocalStore("f__0")
        ));
    }
}