aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/backends/PatioNativeCHeadersBackend.java
blob: fcd1ef4fa5874e52d6b9bc4a97acdd2c7e9d16c0 (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
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
package org.shedlang.compiler.backends;

import org.shedlang.compiler.backends.paths.PartialTargetPath;
import org.shedlang.compiler.backends.paths.TargetDir;
import org.shedlang.compiler.backends.targets.ShedTarget;
import org.shedlang.compiler.config.ProfileConfig;
import org.shedlang.compiler.config.ProjectConfig;
import org.shedlang.compiler.patio.PatioCompilationUnit;
import org.shedlang.compiler.patio.PatioFunctionDeclaration;
import org.shedlang.compiler.shid.ShidNamespace;
import org.shedlang.compiler.typechecker.TypeInfos;
import org.shedlang.compiler.types.NamespaceName;

import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Optional;

public class PatioNativeCHeadersBackend implements Backend {
    public static final BackendFactory FACTORY = new BackendFactory() {
        @Override
        public String name() {
            return "patio-native-c-headers";
        }

        @Override
        public Optional<Backend> backendForTarget(ShedTarget target) {
            if (target == ShedTarget.PATIO_NATIVE_C_HEADERS) {
                return Optional.of(PatioNativeCHeadersBackend.INSTANCE);
            } else {
                return Optional.empty();
            }
        }
    };

    public static final PatioNativeCHeadersBackend INSTANCE = new PatioNativeCHeadersBackend();

    private PatioNativeCHeadersBackend() {
    }

    @Override
    public String targetName() {
        return "patio-native-c-headers";
    }

    public void compile(
        ProjectConfig config,
        ProfileConfig profileConfig,
        List<ShidNamespace> namespaces,
        TypeInfos typeInfos,
        TargetDir targetDir
    ) throws IOException, InterruptedException {
        PatioBackends.compile(
            config,
            profileConfig,
            namespaces,
            typeInfos,
            new PatioToNativeHeaders(targetDir)
        );
    }

    private static class PatioToNativeHeaders implements PatioBackends.PatioToTarget {

        private final TargetDir targetDir;

        public PatioToNativeHeaders(TargetDir targetDir) {
            this.targetDir = targetDir;
        }

        @Override
        public PatioBackends.PatioToTarget namespace(
            NamespaceName namespaceName,
            PatioCompilationUnit patioCompilationUnit
        ) throws IOException, InterruptedException {
            var macroName = String.format(
                "SHED_NAMESPACES__%s__H",
                String.join("__", namespaceName.parts())
            );
            serializeToDisk(
                targetDir.namespace(namespaceName),
                macroName,
                patioCompilationUnit
            );

            return this;
        }

        @Override
        public void bin(
            String binName,
            PatioCompilationUnit patioCompilationUnit,
            String mainFunctionName,
            ProfileConfig profileConfig
        ) throws IOException, InterruptedException {
            var macroName = String.format("SHED_BINS__%s__H", binName);
            serializeToDisk(
                targetDir.bin(binName),
                macroName,
                patioCompilationUnit
            );
        }

        private void serializeToDisk(
            PartialTargetPath partialTargetPath,
            String macroName,
            PatioCompilationUnit patioCompilationUnit
        ) throws IOException {
            var empty = true;

            var builder = new StringBuilder();
            // TODO: path to header file? Use angle brackets?
            builder.append(String.format("#ifndef %s\n", macroName));
            builder.append(String.format("#define %s\n\n", macroName));
            builder.append("#include \"shed/shed.h\"\n\n");
            for (var element : patioCompilationUnit.elements()) {
                if (element instanceof PatioFunctionDeclaration patioFunctionDeclaration) {
                    // TODO: pass this information through rather than having to inspect the string?
                    // TODO: ignore dependency projects?
                    if (patioFunctionDeclaration.name().startsWith("shed__runtime__namespace__")) {
                        empty = false;
                        builder.append("ShedObject ");
                        builder.append(patioFunctionDeclaration.name());
                        builder.append("(ShedClosurePointer closurePointer");
                        for (var param : patioFunctionDeclaration.params()) {
                            builder.append(", ShedObject ");
                            builder.append(param.name());
                        }
                        builder.append(");\n");
                    }
                }
            }
            builder.append("\n#endif\n");

            if (empty) {
                return;
            }

            var compilationUnitString = builder.toString();

            var path = partialTargetPath.withExtension(".h");
            Files.createDirectories(path.getParent());
            Files.writeString(path, compilationUnitString);
        }
    }
}