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

import org.junit.jupiter.api.Test;
import org.shedlang.compiler.ast.typed.TypedNativeFunctionDeclarationNode;
import org.shedlang.compiler.shid.instructions.ShidDeclareNativeFunction;
import org.shedlang.compiler.sources.Sources;
import org.shedlang.compiler.types.FunctionType;
import org.shedlang.compiler.types.NamespaceName;
import org.shedlang.compiler.types.Types;

import java.util.List;

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

public class ShidCodeGenNativeFunctionDeclarationTests {
    @Test
    public void nativeFunctionIsCompiledToDeclareNativeFunctionInstruction() {
        var node = TypedNativeFunctionDeclarationNode.builder()
            .withName("f")
            .withSignature(
                FunctionType.builder()
                    .addPositionalParam(Types.INT)
                    .addPositionalParam(Types.INT)
                    .addNamedParam("n1", Types.INT)
                    .addNamedParam("n2", Types.INT)
                    .build()
            )
            .build();
        var context = ShidCodeGen.Context.EMPTY
            .enterBlock(
                NamespaceName.of("Example", "Project"),
                List.of(),
                Sources.NULL
            )
            .addShedVal("f");

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

        assertThat(result.instructions(), isSequence(
            instanceOf(
                ShidDeclareNativeFunction.class,
                has("namespaceName", x -> x.namespaceName(), equalTo(NamespaceName.of("Example", "Project"))),
                has("name", x -> x.name(), equalTo("f")),
                has("positionalParams", x -> x.positionalParams(), isSequence(
                    isShidDefineFunctionParam("arg0"),
                    isShidDefineFunctionParam("arg1")
                )),
                has("namedParams", x -> x.namedParams(), isSequence(
                    isShidDefineFunctionParam("n1"),
                    isShidDefineFunctionParam("n2")
                ))
            ),
            isShidLocalStore("f__0")
        ));
    }
}