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
|
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.PatioSerializer;
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 PatioTextBackend implements Backend {
public static final BackendFactory FACTORY = new BackendFactory() {
@Override
public String name() {
return "patio-text";
}
@Override
public Optional<Backend> backendForTarget(ShedTarget target) {
if (target == ShedTarget.PATIO_TEXT) {
return Optional.of(PatioTextBackend.INSTANCE);
} else {
return Optional.empty();
}
}
};
public static final PatioTextBackend INSTANCE = new PatioTextBackend();
private PatioTextBackend() {
}
@Override
public String targetName() {
return "patio-text";
}
public void compile(
ProjectConfig config,
ProfileConfig profileConfig,
List<ShidNamespace> namespaces,
TypeInfos typeInfos,
TargetDir targetDir
) throws IOException, InterruptedException {
PatioBackends.compile(
config,
profileConfig,
namespaces,
typeInfos,
new PatioToText(targetDir)
);
}
private static class PatioToText implements PatioBackends.PatioToTarget {
private final TargetDir targetDir;
public PatioToText(TargetDir targetDir) {
this.targetDir = targetDir;
}
@Override
public PatioBackends.PatioToTarget namespace(
NamespaceName namespaceName,
PatioCompilationUnit patioCompilationUnit
) throws IOException, InterruptedException {
serializeToDisk(
targetDir.namespace(namespaceName),
patioCompilationUnit
);
return this;
}
@Override
public void bin(
String binName,
PatioCompilationUnit patioCompilationUnit,
String mainFunctionName,
ProfileConfig profileConfig
) throws IOException, InterruptedException {
serializeToDisk(
targetDir.bin(binName),
patioCompilationUnit
);
}
private void serializeToDisk(
PartialTargetPath partialTargetPath,
PatioCompilationUnit patioCompilationUnit
) throws IOException {
var builder = new StringBuilder();
PatioSerializer.serializeCompilationUnit(patioCompilationUnit, builder);
var compilationUnitString = builder.toString();
var path = partialTargetPath.withExtension(".patio-text");
Files.createDirectories(path.getParent());
Files.writeString(path, compilationUnitString);
}
}
}
|