aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/patio/PatioFunctionDefinition.java
blob: 366ac6d3ddeadb7335e7d8bf2a9b85f62e947818 (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
package org.shedlang.compiler.patio;

import org.pcollections.PVector;
import org.shedlang.compiler.patio.instructions.PatioInstruction;
import org.shedlang.compiler.patio.types.PatioType;
import org.shedlang.compiler.sources.Source;
import org.shedlang.compiler.sources.Sources;
import org.shedlang.compiler.util.P;

import java.util.List;

public record PatioFunctionDefinition(
    String name,
    PatioVisibility visibility,
    List<PatioLocal> locals,
    List<PatioType> returnTypes,
    List<PatioInstruction> body,
    Source source
) implements PatioCompilationUnitElement {
    public Iterable<PatioLocal> params() {
        return locals.stream()
            .filter(local -> local.localType() == PatioLocalType.PARAM)
            .toList();
    }

    public static Builder builder() {
        return new Builder(
            "f",
            PatioVisibility.PRIVATE,
            P.vector(),
            P.vector(),
            P.vector()
        );
    }

    public record Builder(
        String name,
        PatioVisibility visibility,
        PVector<PatioLocal> locals,
        PVector<PatioType> returnTypes,
        PVector<PatioInstruction> body
    ) {
        public Builder withName(String name) {
            return new Builder(
                name,
                this.visibility,
                this.locals,
                this.returnTypes,
                this.body
            );
        }

        public Builder withVisibility(PatioVisibility visibility) {
            return new Builder(
                this.name,
                visibility,
                this.locals,
                this.returnTypes,
                this.body
            );
        }

        public Builder addLocal(PatioLocal local) {
            return new Builder(
                this.name,
                this.visibility,
                this.locals.plus(local),
                this.returnTypes,
                this.body
            );
        }

        public Builder addBodyInstruction(PatioInstruction instruction) {
            return new Builder(
                this.name,
                this.visibility,
                this.locals,
                this.returnTypes,
                this.body.plus(instruction)
            );
        }

        public Builder addReturnType(PatioType returnType) {
            return new Builder(
                this.name,
                this.visibility,
                this.locals,
                this.returnTypes.plus(returnType),
                this.body
            );
        }

        public PatioFunctionDefinition build() {
            return new PatioFunctionDefinition(
                this.name,
                this.visibility,
                this.locals,
                this.returnTypes,
                this.body,
                Sources.NULL
            );
        }
    }
}