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
|
package org.shedlang.compiler.shid;
import org.junit.jupiter.api.Test;
import org.shedlang.compiler.ast.typed.*;
import org.shedlang.compiler.shid.instructions.*;
import org.shedlang.compiler.types.NamespaceName;
import org.shedlang.compiler.types.Types;
import java.util.List;
import java.util.Optional;
import static org.shedlang.compiler.shid.instructions.ShidMatchers.*;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.*;
public class ShidCodeGenNamespaceTests {
@Test
public void bodyOfNamespaceIsCompiledWithNamesBeingExported() {
var namespaceName = new NamespaceName(List.of("Example"));
var node = Typed.namespace(
namespaceName,
List.of(
TypedFunctionDefinitionNode.builder()
.withName("f")
.addPositionalParam("x", Typed.typeLevelReference("String", Types.STRING))
.addNamedParam("y", Typed.typeLevelReference("Int", Types.INT))
.addStatement(Typed.expressionStatement(Typed.stringLiteral("hello")))
.build()
),
List.of(BoundVariable.val("f")),
List.of("f")
);
var shidNamespace = ShidCodeGen.compileNamespace(node);
assertThat(shidNamespace.body(), isShidBlock(isSequence(
// TODO: test the builtin loading properly
instanceOf(ShidNamespaceImport.class),
instanceOf(ShidFieldLoad.class),
instanceOf(ShidLocalStore.class),
instanceOf(
ShidDefineFunction.class,
has("name", x -> x.name(), equalTo("f"))
),
isShidLocalStore("f__0"),
isShidLocalLoad("f__0"),
isShidNamespaceFieldStore(namespaceName, "f")
)));
}
@Test
public void importsAreLoaded() {
var node = TypedNamespaceNode.builder()
.addImport(Typed.import_(NamespaceName.of("A")))
.addBoundVariable(BoundVariable.val("A"))
.addImport(Typed.import_(NamespaceName.of("B")))
.addBoundVariable(BoundVariable.val("B"))
.build();
var shidNamespace = ShidCodeGen.compileNamespace(node);
assertThat(shidNamespace.body(), isShidBlock(isSequence(
// TODO: test the builtin loading properly
instanceOf(ShidNamespaceImport.class),
instanceOf(ShidFieldLoad.class),
instanceOf(ShidLocalStore.class),
isShidNamespaceImport(NamespaceName.of("A")),
isShidLocalStore("A__0"),
isShidNamespaceImport(NamespaceName.of("B")),
isShidLocalStore("B__0")
)));
}
@Test
public void structsAreHoisted() {
var namespaceName = new NamespaceName(List.of("Example"));
var structType = Types.struct(namespaceName, "None");
var node = Typed.namespace(
namespaceName,
List.of(
TypedFunctionDefinitionNode.builder()
.withName("f")
.build(),
TypedStructDefinitionNode.builder()
.withType(structType)
.build()
),
List.of(BoundVariable.val("f"), BoundVariable.val("None")),
List.of("f", "None")
);
var shidNamespace = ShidCodeGen.compileNamespace(node);
assertThat(shidNamespace.body(), isShidBlock(isSequence(
// TODO: test the builtin loading properly
instanceOf(ShidNamespaceImport.class),
instanceOf(ShidFieldLoad.class),
instanceOf(ShidLocalStore.class),
instanceOf(
ShidDefineStruct.class,
has("type", x -> x.type(), equalTo(structType))
),
isShidLocalStore("None__0"),
instanceOf(
ShidDefineFunction.class,
has("name", x -> x.name(), equalTo("f"))
),
isShidLocalStore("f__0"),
isShidLocalLoad("f__0"),
isShidNamespaceFieldStore(namespaceName, "f"),
isShidLocalLoad("None__0"),
isShidNamespaceFieldStore(namespaceName, "None")
)));
}
@Test
public void structMethodsAreExported() {
var namespaceName = new NamespaceName(List.of("Example", "Project"));
var structType = Types.struct(namespaceName, "User");
var node = Typed.namespace(
namespaceName,
List.of(
TypedStructDefinitionNode.builder()
.withType(structType)
.addMethod(
TypedFunctionDefinitionNode.builder()
.withName("name")
.addPositionalParam("this", Optional.empty())
.build()
)
.build()
),
List.of(BoundVariable.val("User")),
List.of()
);
var shidNamespace = ShidCodeGen.compileNamespace(node);
assertThat(shidNamespace.body(), isShidBlock(isSequence(
instanceOf(ShidNamespaceImport.class),
instanceOf(ShidFieldLoad.class),
instanceOf(ShidLocalStore.class),
instanceOf(
ShidDefineStruct.class,
has("type", x -> x.type(), equalTo(structType))
),
isShidLocalStore("User__0"),
instanceOf(
ShidDefineFunction.class,
has("name", x -> x.name(), equalTo("$methods__Example_Project__User__name"))
),
isShidMethodStore(structType, "name")
)));
}
}
|