aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioFunctions.java
blob: 105ca4e973b6194026a328996c435f21cc6195f6 (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
package org.shedlang.compiler.shidtopatio;

import org.shedlang.compiler.ast.typed.FreeVariables;
import org.shedlang.compiler.patio.operands.PatioLocalRef;
import org.shedlang.compiler.patio.types.PatioDataLayout;
import org.shedlang.compiler.patio.types.PatioDataLayouts;
import org.shedlang.compiler.shid.instructions.ShidParam;
import org.shedlang.compiler.sources.Source;
import org.shedlang.compiler.types.NamespaceName;

import java.util.List;
import java.util.function.Function;

public class ShidToPatioFunctions {
    private ShidToPatioFunctions() {
    }

    private static final PatioDataLayout ADDITIONAL_DATA_LAYOUT = PatioDataLayouts.struct(List.of());

    private static ShidToPatioClosures.ClosureRuntimeType functionRuntimeType(
        FreeVariables freeVariables,
        ShidToPatioContext context
    ) {
        return ShidToPatioClosures.closureRuntimeType(
            ADDITIONAL_DATA_LAYOUT,
            freeVariables,
            context
        );
    }

    public static ShidToPatioContext create(
        PatioLocalRef destination,
        String name,
        List<ShidParam> positionalParams,
        List<ShidParam> namedParams,
        FreeVariables freeVariables,
        Function<ShidToPatioContext, ShidToPatioContext> generateBody,
        Source source,
        ShidToPatioContext context
    ) {
        var functionRuntimeType = functionRuntimeType(freeVariables, context);

        return ShidToPatioClosures.create(
            destination,
            ShidToPatioCompilationUnitElementSpace.FUNCTION,
            name,
            positionalParams,
            namedParams,
            functionRuntimeType,
            (closurePointer, storeContext) -> storeContext,
            freeVariables,
            (closurePointer, bodyContext) -> generateBody.apply(bodyContext),
            source,
            context
        );
    }

    public static ShidToPatioContext createNative(
        PatioLocalRef destination,
        NamespaceName namespaceName,
        String name,
        List<ShidParam> positionalParams,
        List<ShidParam> namedParams,
        Source source,
        ShidToPatioContext context
    ) {
        var functionRuntimeType = functionRuntimeType(FreeVariables.empty(), context);

        return ShidToPatioClosures.createNative(
            destination,
            namespaceName,
            name,
            positionalParams,
            namedParams,
            functionRuntimeType,
            source,
            context
        );
    }
}