diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-09-28 15:44:28 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-09-28 15:44:28 +0100 |
| commit | ecb878e11effdfd91ed1c29f18913e7074a527e4 (patch) | |
| tree | fd368fdfa4940544492de9351fcd6d623f338fa1 /src/test/java/org | |
| parent | 12bc0b647eb6276a93c134e35c5babc4efc01d93 (diff) | |
Hoist structs
Diffstat (limited to 'src/test/java/org')
| -rw-r--r-- | src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java | 49 |
1 files changed, 45 insertions, 4 deletions
diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java index b4524e90..3557ac20 100644 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java @@ -3,10 +3,8 @@ package org.shedlang.compiler.stackir; import org.junit.jupiter.api.Test; import org.shedlang.compiler.ast.typed.Typed; import org.shedlang.compiler.ast.typed.TypedFunctionDefinitionNode; -import org.shedlang.compiler.stackir.instructions.DefineFunctionInstruction; -import org.shedlang.compiler.stackir.instructions.FieldLoadInstruction; -import org.shedlang.compiler.stackir.instructions.LocalStoreInstruction; -import org.shedlang.compiler.stackir.instructions.NamespaceImportInstruction; +import org.shedlang.compiler.ast.typed.TypedStructDefinitionNode; +import org.shedlang.compiler.stackir.instructions.*; import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.types.Types; @@ -52,4 +50,47 @@ public class StackCompilerNamespaceTests { isNamespaceFieldStore(namespaceName, "f") )); } + + @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("f", "None"), + List.of("f", "None") + ); + + var stackIrNamespace = StackCompiler.compileNamespace(node); + + assertThat(stackIrNamespace.instructions(), isSequence( + // TODO: test the builtin loading properly + instanceOf(NamespaceImportInstruction.class), + instanceOf(FieldLoadInstruction.class), + instanceOf(LocalStoreInstruction.class), + + instanceOf( + DefineStructInstruction.class, + has("type", x -> x.type(), equalTo(structType)) + ), + isLocalStoreInstruction("None__0"), + instanceOf( + DefineFunctionInstruction.class, + has("name", x -> x.name(), equalTo("f")) + ), + isLocalStoreInstruction("f__0"), + isLocalLoadInstruction("f__0"), + isNamespaceFieldStore(namespaceName, "f"), + isLocalLoadInstruction("None__0"), + isNamespaceFieldStore(namespaceName, "None") + )); + } } |
