aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineFunctionTests.java
blob: c918f782d52595d8e08903a8f1de7e45320bf295 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package org.shedlang.compiler.shidtopatio;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.shedlang.compiler.ast.typed.FreeVariables;
import org.shedlang.compiler.patio.types.PatioTypes;
import org.shedlang.compiler.patio.instructions.Patio;
import org.shedlang.compiler.sources.Sources;
import org.shedlang.compiler.shid.instructions.ShidDefineFunction;
import org.shedlang.compiler.shid.instructions.Shid;
import org.shedlang.compiler.testing.snapshots.SnapshotResolver;
import org.shedlang.compiler.testing.snapshots.Snapshotter;

import java.io.IOException;
import java.util.List;

import static org.shedlang.compiler.shidtopatio.ShidToPatioTesting.serialize;

@ExtendWith({SnapshotResolver.class})
public class ShidToPatioDefineFunctionTests {
    @Test
    public void canDefineMinimalFunction(Snapshotter snapshotter) throws IOException {
        var instruction = new ShidDefineFunction(
            "f",
            List.of(),
            List.of(),
            FreeVariables.empty(),
            Shid.block(List.of(
                Shid.pushUnit(),
                Shid.return_()
            )),
            ShidDefineFunction.Variant.FUNCTION,
            Sources.NULL
        );
        var context = ShidToPatioContext.EMPTY;

        var result = ShidToPatio.generateInstruction(instruction, context);

        snapshotter.assertSnapshot(serialize(result));
    }

    @Test
    public void immutableFreeVariablesAreStoredInAndLoadedFromEnvironment(Snapshotter snapshotter) throws IOException {
        var instruction = new ShidDefineFunction(
            "f",
            List.of(),
            List.of(),
            FreeVariables.builder().plus("a", Sources.NULL).plus("b", Sources.NULL).build(),
            Shid.block(List.of(
                Shid.localLoad("a"),
                Shid.return_()
            )),
            ShidDefineFunction.Variant.FUNCTION,
            Sources.NULL
        );
        var context = ShidToPatioContext.EMPTY
            .defineShidParam("a")
            .defineShidParam("b");

        var result = ShidToPatio.generateInstruction(instruction, context);

        snapshotter.assertSnapshot(serialize(result));
    }

    @Test
    public void canCaptureFreeVariablesThatAreSetAfterDefinition(Snapshotter snapshotter) throws IOException {
        var instruction = new ShidDefineFunction(
            "f",
            List.of(),
            List.of(),
            FreeVariables.builder().plus("a", Sources.NULL).plus("b", Sources.NULL).build(),
            Shid.block(List.of(
                Shid.localLoad("a"),
                Shid.return_()
            )),
            ShidDefineFunction.Variant.FUNCTION,
            Sources.NULL
        );
        var context = ShidToPatioContext.EMPTY
            .declareShidVal("a", ShidToPatioTypes.VALUE_TYPE).context()
            .declareShidVal("b", ShidToPatioTypes.VALUE_TYPE).context();

        context = ShidToPatio.generateInstruction(instruction, context);

        // We make sure there are some other instructions before setting the
        // free variables so we can distinguish the free variable being copied
        // into the closure upon closure creation (i.e. too soon) versus after
        // the free variable is set.
        context = context.addInstruction(Patio.localStore(Patio.localRef("a"), Patio.intConst(42)));
        context = context.initShidLocal("a");
        context = context.addInstruction(Patio.localStore(Patio.localRef("b"), Patio.intConst(47)));
        context = context.initShidLocal("b");

        snapshotter.assertSnapshot(serialize(context));
    }

    @Test
    public void pointersToFreeMutableVariablesAreStoredInAndLoadedFromEnvironment(Snapshotter snapshotter) throws IOException {
        var instruction = new ShidDefineFunction(
            "f",
            List.of(),
            List.of(),
            FreeVariables.builder()
                .plus("a", Sources.NULL)
                .plus("b", Sources.NULL)
                .build(),
            Shid.block(List.of(
                Shid.localLoad("a"),
                Shid.return_()
            )),
            ShidDefineFunction.Variant.FUNCTION,
            Sources.NULL
        );
        var context = ShidToPatioContext.EMPTY
            .declareShidVal("a", PatioTypes.DATA_PTR).context()
            .initShidLocal("a")
            .declareShidVal("b", PatioTypes.DATA_PTR).context()
            .initShidLocal("b");

        var result = ShidToPatio.generateInstruction(instruction, context);

        snapshotter.assertSnapshot(serialize(result));
    }

    @Test
    public void positionalAndNamedParamsAreDefined(Snapshotter snapshotter) throws IOException {
        var instruction = new ShidDefineFunction(
            "f",
            List.of(Shid.param("param1"), Shid.param("param2")),
            // Define named params in non-lexical order
            List.of(Shid.param("paramB"), Shid.param("paramC"), Shid.param("paramA")),
            FreeVariables.builder().plus("a", Sources.NULL).plus("b", Sources.NULL).build(),
            Shid.block(List.of(
                Shid.localLoad("a"),
                Shid.localLoad("param1"),
                Shid.intAdd(),
                Shid.localLoad("paramA"),
                Shid.intAdd(),
                Shid.return_()
            )),
            ShidDefineFunction.Variant.FUNCTION,
            Sources.NULL
        );
        var context = ShidToPatioContext.EMPTY
            .defineShidParam("a")
            .defineShidParam("b");

        var result = ShidToPatio.generateInstruction(instruction, context);

        snapshotter.assertSnapshot(serialize(result));
    }

    @Test
    public void effectHandlerMethodSwapsEffectHandlerInBody(Snapshotter snapshotter) throws IOException {
        var instruction = new ShidDefineFunction(
            "f",
            List.of(),
            List.of(),
            FreeVariables.empty(),
            Shid.block(List.of(
                Shid.pushUnit(),
                Shid.resume()
            )),
            ShidDefineFunction.Variant.EFFECT_HANDLER_METHOD,
            Sources.NULL
        );
        var context = ShidToPatioContext.EMPTY;

        var result = ShidToPatio.generateInstruction(instruction, context);

        snapshotter.assertSnapshot(serialize(result));
    }
}