bonescript-compiler 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/algorithm_catalog.d.ts +32 -0
- package/dist/algorithm_catalog.js +323 -0
- package/dist/algorithm_catalog.js.map +1 -0
- package/dist/ast.d.ts +244 -0
- package/dist/ast.js +8 -0
- package/dist/ast.js.map +1 -0
- package/dist/cli.d.ts +4 -0
- package/dist/cli.js +605 -0
- package/dist/cli.js.map +1 -0
- package/dist/emit_batch.d.ts +7 -0
- package/dist/emit_batch.js +133 -0
- package/dist/emit_batch.js.map +1 -0
- package/dist/emit_capability.d.ts +7 -0
- package/dist/emit_capability.js +376 -0
- package/dist/emit_capability.js.map +1 -0
- package/dist/emit_composition.d.ts +22 -0
- package/dist/emit_composition.js +184 -0
- package/dist/emit_composition.js.map +1 -0
- package/dist/emit_deploy.d.ts +9 -0
- package/dist/emit_deploy.js +191 -0
- package/dist/emit_deploy.js.map +1 -0
- package/dist/emit_events.d.ts +14 -0
- package/dist/emit_events.js +305 -0
- package/dist/emit_events.js.map +1 -0
- package/dist/emit_extras.d.ts +12 -0
- package/dist/emit_extras.js +234 -0
- package/dist/emit_extras.js.map +1 -0
- package/dist/emit_full.d.ts +13 -0
- package/dist/emit_full.js +273 -0
- package/dist/emit_full.js.map +1 -0
- package/dist/emit_maintenance.d.ts +16 -0
- package/dist/emit_maintenance.js +442 -0
- package/dist/emit_maintenance.js.map +1 -0
- package/dist/emit_runtime.d.ts +13 -0
- package/dist/emit_runtime.js +691 -0
- package/dist/emit_runtime.js.map +1 -0
- package/dist/emit_sourcemap.d.ts +29 -0
- package/dist/emit_sourcemap.js +123 -0
- package/dist/emit_sourcemap.js.map +1 -0
- package/dist/emit_tests.d.ts +15 -0
- package/dist/emit_tests.js +185 -0
- package/dist/emit_tests.js.map +1 -0
- package/dist/emit_websocket.d.ts +6 -0
- package/dist/emit_websocket.js +223 -0
- package/dist/emit_websocket.js.map +1 -0
- package/dist/emitter.d.ts +25 -0
- package/dist/emitter.js +511 -0
- package/dist/emitter.js.map +1 -0
- package/dist/extension_manager.d.ts +38 -0
- package/dist/extension_manager.js +170 -0
- package/dist/extension_manager.js.map +1 -0
- package/dist/formatter.d.ts +34 -0
- package/dist/formatter.js +317 -0
- package/dist/formatter.js.map +1 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +113 -0
- package/dist/index.js.map +1 -0
- package/dist/ir.d.ts +168 -0
- package/dist/ir.js +10 -0
- package/dist/ir.js.map +1 -0
- package/dist/lexer.d.ts +195 -0
- package/dist/lexer.js +619 -0
- package/dist/lexer.js.map +1 -0
- package/dist/lowering.d.ts +25 -0
- package/dist/lowering.js +500 -0
- package/dist/lowering.js.map +1 -0
- package/dist/module_loader.d.ts +25 -0
- package/dist/module_loader.js +126 -0
- package/dist/module_loader.js.map +1 -0
- package/dist/optimizer.d.ts +26 -0
- package/dist/optimizer.js +158 -0
- package/dist/optimizer.js.map +1 -0
- package/dist/parse_decls.d.ts +13 -0
- package/dist/parse_decls.js +442 -0
- package/dist/parse_decls.js.map +1 -0
- package/dist/parse_decls2.d.ts +13 -0
- package/dist/parse_decls2.js +295 -0
- package/dist/parse_decls2.js.map +1 -0
- package/dist/parse_expr.d.ts +7 -0
- package/dist/parse_expr.js +197 -0
- package/dist/parse_expr.js.map +1 -0
- package/dist/parse_types.d.ts +6 -0
- package/dist/parse_types.js +51 -0
- package/dist/parse_types.js.map +1 -0
- package/dist/parser.d.ts +10 -0
- package/dist/parser.js +62 -0
- package/dist/parser.js.map +1 -0
- package/dist/parser_base.d.ts +19 -0
- package/dist/parser_base.js +50 -0
- package/dist/parser_base.js.map +1 -0
- package/dist/parser_recovery.d.ts +26 -0
- package/dist/parser_recovery.js +140 -0
- package/dist/parser_recovery.js.map +1 -0
- package/dist/scaffold.d.ts +13 -0
- package/dist/scaffold.js +376 -0
- package/dist/scaffold.js.map +1 -0
- package/dist/solver.d.ts +26 -0
- package/dist/solver.js +281 -0
- package/dist/solver.js.map +1 -0
- package/dist/typechecker.d.ts +52 -0
- package/dist/typechecker.js +534 -0
- package/dist/typechecker.js.map +1 -0
- package/dist/types.d.ts +38 -0
- package/dist/types.js +85 -0
- package/dist/types.js.map +1 -0
- package/dist/verifier.d.ts +46 -0
- package/dist/verifier.js +307 -0
- package/dist/verifier.js.map +1 -0
- package/package.json +52 -0
- package/src/algorithm_catalog.ts +345 -0
- package/src/ast.ts +334 -0
- package/src/cli.ts +624 -0
- package/src/emit_batch.ts +140 -0
- package/src/emit_capability.ts +436 -0
- package/src/emit_composition.ts +196 -0
- package/src/emit_deploy.ts +190 -0
- package/src/emit_events.ts +307 -0
- package/src/emit_extras.ts +240 -0
- package/src/emit_full.ts +309 -0
- package/src/emit_maintenance.ts +459 -0
- package/src/emit_runtime.ts +731 -0
- package/src/emit_sourcemap.ts +140 -0
- package/src/emit_tests.ts +205 -0
- package/src/emit_websocket.ts +229 -0
- package/src/emitter.ts +566 -0
- package/src/extension_manager.ts +187 -0
- package/src/formatter.ts +297 -0
- package/src/index.ts +88 -0
- package/src/ir.ts +215 -0
- package/src/lexer.ts +630 -0
- package/src/lowering.ts +556 -0
- package/src/module_loader.ts +114 -0
- package/src/optimizer.ts +196 -0
- package/src/parse_decls.ts +409 -0
- package/src/parse_decls2.ts +244 -0
- package/src/parse_expr.ts +197 -0
- package/src/parse_types.ts +54 -0
- package/src/parser.ts +64 -0
- package/src/parser_base.ts +57 -0
- package/src/parser_recovery.ts +153 -0
- package/src/scaffold.ts +375 -0
- package/src/solver.ts +330 -0
- package/src/typechecker.ts +591 -0
- package/src/types.ts +122 -0
- package/src/verifier.ts +348 -0
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BoneScript Extension Manager
|
|
3
|
+
*
|
|
4
|
+
* Handles the escape hatch system: extension_point declarations.
|
|
5
|
+
*
|
|
6
|
+
* How it works:
|
|
7
|
+
* 1. Compiler emits a stub with sentinel comments around the implementation region.
|
|
8
|
+
* 2. On recompile, the merger reads the existing output file and extracts any
|
|
9
|
+
* user code between the sentinels.
|
|
10
|
+
* 3. The user's code is re-injected into the newly generated file.
|
|
11
|
+
* 4. If stable: true and no implementation exists, compilation fails.
|
|
12
|
+
*
|
|
13
|
+
* Sentinel format (must be unique and parseable):
|
|
14
|
+
* // <bonescript:ext:NAME:begin>
|
|
15
|
+
* ... user code here ...
|
|
16
|
+
* // <bonescript:ext:NAME:end>
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import * as fs from "fs";
|
|
20
|
+
import * as path from "path";
|
|
21
|
+
import * as AST from "./ast";
|
|
22
|
+
|
|
23
|
+
// ─── Sentinel Helpers ────────────────────────────────────────────────────────
|
|
24
|
+
|
|
25
|
+
export function beginSentinel(name: string): string {
|
|
26
|
+
return `// <bonescript:ext:${name}:begin>`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function endSentinel(name: string): string {
|
|
30
|
+
return `// <bonescript:ext:${name}:end>`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function isStubImplementation(code: string): boolean {
|
|
34
|
+
return code.trim() === "" || code.includes("throw new Error(\"Not implemented:");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// ─── Stub Generator ──────────────────────────────────────────────────────────
|
|
38
|
+
|
|
39
|
+
function toTsType(irType: string): string {
|
|
40
|
+
const map: Record<string, string> = {
|
|
41
|
+
string: "string", uint: "number", int: "number", float: "number",
|
|
42
|
+
bool: "boolean", timestamp: "Date", uuid: "string", bytes: "Buffer", json: "unknown",
|
|
43
|
+
};
|
|
44
|
+
if (map[irType]) return map[irType];
|
|
45
|
+
const m = irType.match(/^(list|set)<(.+)>$/);
|
|
46
|
+
if (m) return `${toTsType(m[2])}[]`;
|
|
47
|
+
const om = irType.match(/^optional<(.+)>$/);
|
|
48
|
+
if (om) return `${toTsType(om[1])} | null`;
|
|
49
|
+
return irType;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function serializeType(t: AST.TypeExprNode): string {
|
|
53
|
+
switch (t.kind) {
|
|
54
|
+
case "PrimitiveType": return t.name;
|
|
55
|
+
case "GenericType": return `${t.name}<${t.typeArgs.map(serializeType).join(", ")}>`;
|
|
56
|
+
case "EntityRefType": return t.name;
|
|
57
|
+
case "TupleType": return `(${t.elements.map(serializeType).join(", ")})`;
|
|
58
|
+
case "UnionType": return t.members.map(serializeType).join(" | ");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function emitExtensionPointStub(ext: AST.ExtensionPointDeclNode): string {
|
|
63
|
+
const params = ext.params.map(p => `${p.name}: ${toTsType(serializeType(p.type))}`).join(", ");
|
|
64
|
+
const returnType = ext.returns ? toTsType(serializeType(ext.returns)) : "void";
|
|
65
|
+
const lines: string[] = [];
|
|
66
|
+
|
|
67
|
+
lines.push(`/**`);
|
|
68
|
+
lines.push(` * Extension point: ${ext.name}`);
|
|
69
|
+
lines.push(` * ${ext.stable ? "STABLE — implementation required." : "Optional — stub used if not implemented."}`);
|
|
70
|
+
lines.push(` * Params: ${ext.params.map(p => `${p.name}: ${serializeType(p.type)}`).join(", ") || "none"}`);
|
|
71
|
+
lines.push(` * Returns: ${ext.returns ? serializeType(ext.returns) : "void"}`);
|
|
72
|
+
lines.push(` */`);
|
|
73
|
+
lines.push(`export function ${ext.name}(${params}): ${returnType} {`);
|
|
74
|
+
lines.push(` ${beginSentinel(ext.name)}`);
|
|
75
|
+
lines.push(` throw new Error("Not implemented: ${ext.name}");`);
|
|
76
|
+
lines.push(` ${endSentinel(ext.name)}`);
|
|
77
|
+
lines.push(`}`);
|
|
78
|
+
|
|
79
|
+
return lines.join("\n");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// ─── Extension Merger ────────────────────────────────────────────────────────
|
|
83
|
+
// Reads an existing generated file and extracts user implementations.
|
|
84
|
+
|
|
85
|
+
export interface ExtractedImpl {
|
|
86
|
+
name: string;
|
|
87
|
+
code: string;
|
|
88
|
+
isStub: boolean;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function extractImplementations(existingContent: string): Map<string, ExtractedImpl> {
|
|
92
|
+
const result = new Map<string, ExtractedImpl>();
|
|
93
|
+
const beginPattern = /\/\/ <bonescript:ext:([^:]+):begin>/g;
|
|
94
|
+
let match: RegExpExecArray | null;
|
|
95
|
+
|
|
96
|
+
while ((match = beginPattern.exec(existingContent)) !== null) {
|
|
97
|
+
const name = match[1];
|
|
98
|
+
const beginIdx = match.index + match[0].length;
|
|
99
|
+
const endMarker = endSentinel(name);
|
|
100
|
+
const endIdx = existingContent.indexOf(endMarker, beginIdx);
|
|
101
|
+
|
|
102
|
+
if (endIdx === -1) continue;
|
|
103
|
+
|
|
104
|
+
const code = existingContent.slice(beginIdx, endIdx).trim();
|
|
105
|
+
result.set(name, {
|
|
106
|
+
name,
|
|
107
|
+
code,
|
|
108
|
+
isStub: isStubImplementation(code),
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return result;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function mergeImplementations(
|
|
116
|
+
newContent: string,
|
|
117
|
+
existingImpls: Map<string, ExtractedImpl>
|
|
118
|
+
): string {
|
|
119
|
+
let result = newContent;
|
|
120
|
+
|
|
121
|
+
for (const [name, impl] of existingImpls) {
|
|
122
|
+
if (impl.isStub) continue; // don't restore stubs
|
|
123
|
+
|
|
124
|
+
const begin = beginSentinel(name);
|
|
125
|
+
const end = endSentinel(name);
|
|
126
|
+
const beginIdx = result.indexOf(begin);
|
|
127
|
+
const endIdx = result.indexOf(end, beginIdx);
|
|
128
|
+
|
|
129
|
+
if (beginIdx === -1 || endIdx === -1) continue;
|
|
130
|
+
|
|
131
|
+
const before = result.slice(0, beginIdx + begin.length);
|
|
132
|
+
const after = result.slice(endIdx);
|
|
133
|
+
result = `${before}\n ${impl.code}\n ${after}`;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ─── Validation ──────────────────────────────────────────────────────────────
|
|
140
|
+
|
|
141
|
+
export interface ExtensionValidationError {
|
|
142
|
+
name: string;
|
|
143
|
+
message: string;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function validateExtensions(
|
|
147
|
+
extensions: AST.ExtensionPointDeclNode[],
|
|
148
|
+
existingImpls: Map<string, ExtractedImpl>
|
|
149
|
+
): ExtensionValidationError[] {
|
|
150
|
+
const errors: ExtensionValidationError[] = [];
|
|
151
|
+
|
|
152
|
+
for (const ext of extensions) {
|
|
153
|
+
if (!ext.stable) continue;
|
|
154
|
+
|
|
155
|
+
const impl = existingImpls.get(ext.name);
|
|
156
|
+
if (!impl || impl.isStub) {
|
|
157
|
+
errors.push({
|
|
158
|
+
name: ext.name,
|
|
159
|
+
message: `Extension point '${ext.name}' is marked stable: true but has no implementation. ` +
|
|
160
|
+
`Add your implementation between the sentinel comments in the generated file.`,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return errors;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ─── File-Level Merge ────────────────────────────────────────────────────────
|
|
169
|
+
// Called by the emitter when writing output files.
|
|
170
|
+
|
|
171
|
+
export function mergeWithExisting(
|
|
172
|
+
newContent: string,
|
|
173
|
+
outputPath: string,
|
|
174
|
+
extensions: AST.ExtensionPointDeclNode[]
|
|
175
|
+
): { content: string; validationErrors: ExtensionValidationError[] } {
|
|
176
|
+
let existingImpls = new Map<string, ExtractedImpl>();
|
|
177
|
+
|
|
178
|
+
if (fs.existsSync(outputPath)) {
|
|
179
|
+
const existing = fs.readFileSync(outputPath, "utf-8");
|
|
180
|
+
existingImpls = extractImplementations(existing);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const validationErrors = validateExtensions(extensions, existingImpls);
|
|
184
|
+
const content = mergeImplementations(newContent, existingImpls);
|
|
185
|
+
|
|
186
|
+
return { content, validationErrors };
|
|
187
|
+
}
|
package/src/formatter.ts
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BoneScript Formatter — `bone fmt`
|
|
3
|
+
* Canonicalizes whitespace and formatting in .bone source files.
|
|
4
|
+
*
|
|
5
|
+
* Rules:
|
|
6
|
+
* - 2-space indentation
|
|
7
|
+
* - One declaration per logical block, separated by one blank line
|
|
8
|
+
* - Field lists: one per line if more than 2, otherwise inline
|
|
9
|
+
* - Constraints: one per line
|
|
10
|
+
* - Trailing newline at end of file
|
|
11
|
+
* - LF line endings
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import * as AST from "./ast";
|
|
15
|
+
|
|
16
|
+
export class Formatter {
|
|
17
|
+
private out: string[] = [];
|
|
18
|
+
private indent = 0;
|
|
19
|
+
|
|
20
|
+
format(program: AST.ProgramNode): string {
|
|
21
|
+
this.out = [];
|
|
22
|
+
this.indent = 0;
|
|
23
|
+
|
|
24
|
+
for (let i = 0; i < program.systems.length; i++) {
|
|
25
|
+
this.formatSystem(program.systems[i]);
|
|
26
|
+
if (i < program.systems.length - 1) this.line("");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return this.out.join("\n") + "\n";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
private line(s: string) {
|
|
33
|
+
this.out.push(" ".repeat(this.indent) + s);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private blank() {
|
|
37
|
+
this.out.push("");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private formatSystem(sys: AST.SystemDeclNode) {
|
|
41
|
+
this.line(`system ${sys.name} {`);
|
|
42
|
+
this.indent++;
|
|
43
|
+
|
|
44
|
+
if (sys.domain) {
|
|
45
|
+
this.line(`domain: ${sys.domain}`);
|
|
46
|
+
this.blank();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
for (let i = 0; i < sys.declarations.length; i++) {
|
|
50
|
+
this.formatDeclaration(sys.declarations[i]);
|
|
51
|
+
if (i < sys.declarations.length - 1) this.blank();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
this.indent--;
|
|
55
|
+
this.line(`}`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private formatDeclaration(decl: AST.DeclarationNode) {
|
|
59
|
+
switch (decl.kind) {
|
|
60
|
+
case "EntityDecl": this.formatEntity(decl); break;
|
|
61
|
+
case "CapabilityDecl": this.formatCapability(decl); break;
|
|
62
|
+
case "ChannelDecl": this.formatChannel(decl); break;
|
|
63
|
+
case "StoreDecl": this.formatStore(decl); break;
|
|
64
|
+
case "EventDecl": this.formatEvent(decl); break;
|
|
65
|
+
case "ConstraintDecl": this.formatConstraint(decl); break;
|
|
66
|
+
case "PolicyDecl": this.formatPolicy(decl); break;
|
|
67
|
+
case "FlowDecl": this.formatFlow(decl); break;
|
|
68
|
+
case "ImportDecl": this.formatImport(decl); break;
|
|
69
|
+
case "ExtensionPointDecl": this.formatExtensionPoint(decl); break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
private formatEntity(e: AST.EntityDeclNode) {
|
|
74
|
+
this.line(`entity ${e.name} {`);
|
|
75
|
+
this.indent++;
|
|
76
|
+
|
|
77
|
+
if (e.owns.length > 0) {
|
|
78
|
+
if (e.owns.length <= 2) {
|
|
79
|
+
const fields = e.owns.map(f => `${f.name}: ${this.formatType(f.type)}`).join(", ");
|
|
80
|
+
this.line(`owns: [${fields}]`);
|
|
81
|
+
} else {
|
|
82
|
+
this.line(`owns: [`);
|
|
83
|
+
this.indent++;
|
|
84
|
+
for (let i = 0; i < e.owns.length; i++) {
|
|
85
|
+
const f = e.owns[i];
|
|
86
|
+
const comma = i < e.owns.length - 1 ? "," : "";
|
|
87
|
+
this.line(`${f.name}: ${this.formatType(f.type)}${comma}`);
|
|
88
|
+
}
|
|
89
|
+
this.indent--;
|
|
90
|
+
this.line(`]`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (e.constraints.length > 0) {
|
|
95
|
+
this.line(`constraints: [`);
|
|
96
|
+
this.indent++;
|
|
97
|
+
for (let i = 0; i < e.constraints.length; i++) {
|
|
98
|
+
const comma = i < e.constraints.length - 1 ? "," : "";
|
|
99
|
+
this.line(`${this.formatExpr(e.constraints[i])}${comma}`);
|
|
100
|
+
}
|
|
101
|
+
this.indent--;
|
|
102
|
+
this.line(`]`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (e.states) {
|
|
106
|
+
const path = e.states.nodes.map(n => n.name).join(" -> ");
|
|
107
|
+
this.line(`states: ${path}`);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (e.auth) this.line(`auth: ${e.auth}`);
|
|
111
|
+
|
|
112
|
+
for (const idx of e.indexes) {
|
|
113
|
+
this.line(`index: [${idx.join(", ")}]`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
this.indent--;
|
|
117
|
+
this.line(`}`);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private formatCapability(c: AST.CapabilityDeclNode) {
|
|
121
|
+
const params = c.params.map(p => `${p.name}: ${this.formatType(p.type)}`).join(", ");
|
|
122
|
+
this.line(`capability ${c.name}(${params}) {`);
|
|
123
|
+
this.indent++;
|
|
124
|
+
|
|
125
|
+
if (c.requires.length > 0) {
|
|
126
|
+
this.line(`requires: [`);
|
|
127
|
+
this.indent++;
|
|
128
|
+
for (let i = 0; i < c.requires.length; i++) {
|
|
129
|
+
const comma = i < c.requires.length - 1 ? "," : "";
|
|
130
|
+
this.line(`${this.formatExpr(c.requires[i])}${comma}`);
|
|
131
|
+
}
|
|
132
|
+
this.indent--;
|
|
133
|
+
this.line(`]`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (c.effects.length > 0) {
|
|
137
|
+
this.line(`effects: [`);
|
|
138
|
+
this.indent++;
|
|
139
|
+
for (let i = 0; i < c.effects.length; i++) {
|
|
140
|
+
const eff = c.effects[i];
|
|
141
|
+
const comma = i < c.effects.length - 1 ? "," : "";
|
|
142
|
+
this.line(`${eff.target.path.join(".")} ${eff.op} ${this.formatExpr(eff.value)}${comma}`);
|
|
143
|
+
}
|
|
144
|
+
this.indent--;
|
|
145
|
+
this.line(`]`);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (c.emits.length > 0) {
|
|
149
|
+
const emits = c.emits.map(e => {
|
|
150
|
+
const args = e.args.map(a => this.formatExpr(a)).join(", ");
|
|
151
|
+
return args ? `${e.eventName}(${args})` : e.eventName;
|
|
152
|
+
}).join(", ");
|
|
153
|
+
this.line(`emits: ${emits}`);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (c.sync) this.line(`sync: ${c.sync}`);
|
|
157
|
+
if (c.timeout) this.line(`timeout: ${c.timeout}`);
|
|
158
|
+
if (c.idempotent !== null) this.line(`idempotent: ${c.idempotent}`);
|
|
159
|
+
|
|
160
|
+
this.indent--;
|
|
161
|
+
this.line(`}`);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private formatChannel(c: AST.ChannelDeclNode) {
|
|
165
|
+
this.line(`channel ${c.name} {`);
|
|
166
|
+
this.indent++;
|
|
167
|
+
if (c.transport) this.line(`transport: ${c.transport}`);
|
|
168
|
+
if (c.ordering) this.line(`ordering: ${c.ordering}`);
|
|
169
|
+
if (c.participants) this.line(`participants: ${this.formatType(c.participants)}`);
|
|
170
|
+
if (c.persistence) this.line(`persistence: ${c.persistence}`);
|
|
171
|
+
if (c.maxSize !== null) this.line(`max_size: ${c.maxSize}`);
|
|
172
|
+
if (c.filter) this.line(`filter: ${this.formatExpr(c.filter)}`);
|
|
173
|
+
this.indent--;
|
|
174
|
+
this.line(`}`);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private formatStore(s: AST.StoreDeclNode) {
|
|
178
|
+
this.line(`store ${s.name} {`);
|
|
179
|
+
this.indent++;
|
|
180
|
+
if (s.engine) this.line(`engine: ${s.engine}`);
|
|
181
|
+
if (s.schema.length > 0) {
|
|
182
|
+
this.line(`schema: {`);
|
|
183
|
+
this.indent++;
|
|
184
|
+
for (let i = 0; i < s.schema.length; i++) {
|
|
185
|
+
const f = s.schema[i];
|
|
186
|
+
const comma = i < s.schema.length - 1 ? "," : "";
|
|
187
|
+
this.line(`${f.name}: ${this.formatType(f.type)}${comma}`);
|
|
188
|
+
}
|
|
189
|
+
this.indent--;
|
|
190
|
+
this.line(`}`);
|
|
191
|
+
}
|
|
192
|
+
if (s.retention) this.line(`retention: ${s.retention}`);
|
|
193
|
+
if (s.partition) this.line(`partition: ${s.partition}`);
|
|
194
|
+
if (s.replicas !== null) this.line(`replicas: ${s.replicas}`);
|
|
195
|
+
this.indent--;
|
|
196
|
+
this.line(`}`);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
private formatEvent(e: AST.EventDeclNode) {
|
|
200
|
+
this.line(`event ${e.name} {`);
|
|
201
|
+
this.indent++;
|
|
202
|
+
if (e.payload.length > 0) {
|
|
203
|
+
this.line(`payload: {`);
|
|
204
|
+
this.indent++;
|
|
205
|
+
for (let i = 0; i < e.payload.length; i++) {
|
|
206
|
+
const f = e.payload[i];
|
|
207
|
+
const comma = i < e.payload.length - 1 ? "," : "";
|
|
208
|
+
this.line(`${f.name}: ${this.formatType(f.type)}${comma}`);
|
|
209
|
+
}
|
|
210
|
+
this.indent--;
|
|
211
|
+
this.line(`}`);
|
|
212
|
+
}
|
|
213
|
+
if (e.delivery) this.line(`delivery: ${e.delivery}`);
|
|
214
|
+
if (e.ttl) this.line(`ttl: ${e.ttl}`);
|
|
215
|
+
this.indent--;
|
|
216
|
+
this.line(`}`);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private formatConstraint(c: AST.ConstraintDeclNode) {
|
|
220
|
+
this.line(`constraint ${c.name}: ${this.formatExpr(c.expr)}`);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private formatPolicy(p: AST.PolicyDeclNode) {
|
|
224
|
+
this.line(`policy ${p.name} {`);
|
|
225
|
+
this.indent++;
|
|
226
|
+
if (p.rateLimit) this.line(`rate_limit: ${p.rateLimit.count} per ${p.rateLimit.per}`);
|
|
227
|
+
if (p.access.length > 0) this.line(`access: [${p.access.join(", ")}]`);
|
|
228
|
+
if (p.audit !== null) this.line(`audit: ${p.audit}`);
|
|
229
|
+
if (p.encryption) this.line(`encryption: ${p.encryption}`);
|
|
230
|
+
this.indent--;
|
|
231
|
+
this.line(`}`);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private formatFlow(f: AST.FlowDeclNode) {
|
|
235
|
+
this.line(`flow ${f.name} {`);
|
|
236
|
+
this.indent++;
|
|
237
|
+
for (let i = 0; i < f.steps.length; i++) {
|
|
238
|
+
const step = f.steps[i];
|
|
239
|
+
const args = step.action.args.map(a => this.formatExpr(a)).join(", ");
|
|
240
|
+
this.line(`step ${step.name}: ${step.action.name}(${args})`);
|
|
241
|
+
if (step.compensate) {
|
|
242
|
+
const cargs = step.compensate.args.map(a => this.formatExpr(a)).join(", ");
|
|
243
|
+
this.indent++;
|
|
244
|
+
this.line(`compensate: ${step.compensate.name}(${cargs})`);
|
|
245
|
+
this.indent--;
|
|
246
|
+
}
|
|
247
|
+
if (i < f.steps.length - 1) this.blank();
|
|
248
|
+
}
|
|
249
|
+
this.indent--;
|
|
250
|
+
this.line(`}`);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
private formatImport(i: AST.ImportDeclNode) {
|
|
254
|
+
this.line(`import ${i.name} from "${i.from}"`);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
private formatType(t: AST.TypeExprNode): string {
|
|
258
|
+
switch (t.kind) {
|
|
259
|
+
case "PrimitiveType": return t.name;
|
|
260
|
+
case "GenericType": return `${t.name}<${t.typeArgs.map(a => this.formatType(a)).join(", ")}>`;
|
|
261
|
+
case "EntityRefType": return t.name;
|
|
262
|
+
case "TupleType": return `(${t.elements.map(e => this.formatType(e)).join(", ")})`;
|
|
263
|
+
case "UnionType": return t.members.map(m => this.formatType(m)).join(" | ");
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private formatExpr(e: AST.ExprNode): string {
|
|
268
|
+
switch (e.kind) {
|
|
269
|
+
case "Literal":
|
|
270
|
+
if (e.type === "string") return `"${e.value}"`;
|
|
271
|
+
if (e.type === "list") return `[${(e.value as AST.ExprNode[]).map(v => this.formatExpr(v)).join(", ")}]`;
|
|
272
|
+
if (e.type === "none") return "none";
|
|
273
|
+
return String(e.value);
|
|
274
|
+
case "FieldRef":
|
|
275
|
+
return e.path.join(".");
|
|
276
|
+
case "BinaryExpr":
|
|
277
|
+
if (e.op === "..") return `${this.formatExpr(e.left)}..${this.formatExpr(e.right)}`;
|
|
278
|
+
return `${this.formatExpr(e.left)} ${e.op} ${this.formatExpr(e.right)}`;
|
|
279
|
+
case "UnaryExpr":
|
|
280
|
+
return `${e.op} ${this.formatExpr(e.operand)}`;
|
|
281
|
+
case "CallExpr":
|
|
282
|
+
return `${e.name}(${e.args.map(a => this.formatExpr(a)).join(", ")})`;
|
|
283
|
+
case "TernaryExpr":
|
|
284
|
+
return `${this.formatExpr(e.condition)} ? ${this.formatExpr(e.consequent)} : ${this.formatExpr(e.alternate)}`;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
private formatExtensionPoint(e: AST.ExtensionPointDeclNode) {
|
|
289
|
+
const params = e.params.map(p => `${p.name}: ${this.formatType(p.type)}`).join(", ");
|
|
290
|
+
this.line(`extension_point ${e.name}(${params}) {`);
|
|
291
|
+
this.indent++;
|
|
292
|
+
if (e.returns) this.line(`returns: ${this.formatType(e.returns)}`);
|
|
293
|
+
this.line(`stable: ${e.stable}`);
|
|
294
|
+
this.indent--;
|
|
295
|
+
this.line(`}`);
|
|
296
|
+
}
|
|
297
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BoneScript Compiler — Public API
|
|
3
|
+
* Import this module to use the compiler programmatically.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Core pipeline
|
|
7
|
+
export { Lexer, LexerError, TokenKind } from "./lexer";
|
|
8
|
+
export type { Token, SourceLocation } from "./lexer";
|
|
9
|
+
export { Parser, ParseError } from "./parser";
|
|
10
|
+
export { RecoveringParser } from "./parser_recovery";
|
|
11
|
+
export type { RecoveredParseResult } from "./parser_recovery";
|
|
12
|
+
export { TypeChecker } from "./typechecker";
|
|
13
|
+
export type { TypeError } from "./typechecker";
|
|
14
|
+
export { Lowering } from "./lowering";
|
|
15
|
+
export { ConstraintSolver } from "./solver";
|
|
16
|
+
export type { SolverResult } from "./solver";
|
|
17
|
+
export { FullEmitter } from "./emit_full";
|
|
18
|
+
export type { EmittedFile } from "./emitter";
|
|
19
|
+
export { Verifier } from "./verifier";
|
|
20
|
+
export type { VerifyResult, VerifyIssue } from "./verifier";
|
|
21
|
+
export { optimize } from "./optimizer";
|
|
22
|
+
export type { OptimizationResult } from "./optimizer";
|
|
23
|
+
export { ModuleLoader } from "./module_loader";
|
|
24
|
+
export type { LoadResult } from "./module_loader";
|
|
25
|
+
export { Formatter } from "./formatter";
|
|
26
|
+
export { scaffold } from "./scaffold";
|
|
27
|
+
export type { ScaffoldDomain } from "./scaffold";
|
|
28
|
+
|
|
29
|
+
// AST types
|
|
30
|
+
export * as AST from "./ast";
|
|
31
|
+
|
|
32
|
+
// IR types
|
|
33
|
+
export * as IR from "./ir";
|
|
34
|
+
|
|
35
|
+
// Algorithm catalog
|
|
36
|
+
export { lookupAlgorithm, listAlgorithms, listByCategory } from "./algorithm_catalog";
|
|
37
|
+
|
|
38
|
+
// Extension system
|
|
39
|
+
export { mergeWithExisting, extractImplementations, validateExtensions } from "./extension_manager";
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Convenience function: compile a .bone source string to files.
|
|
43
|
+
*/
|
|
44
|
+
export async function compile(source: string, sourceFile: string = "program.bone"): Promise<{
|
|
45
|
+
files: { path: string; content: string; language: string; source_module: string }[];
|
|
46
|
+
errors: string[];
|
|
47
|
+
warnings: string[];
|
|
48
|
+
}> {
|
|
49
|
+
const { createHash } = await import("crypto");
|
|
50
|
+
const { Lexer: L } = await import("./lexer");
|
|
51
|
+
const { Parser: P } = await import("./parser");
|
|
52
|
+
const { TypeChecker: TC } = await import("./typechecker");
|
|
53
|
+
const { Lowering: Lo } = await import("./lowering");
|
|
54
|
+
const { ConstraintSolver: CS } = await import("./solver");
|
|
55
|
+
const { FullEmitter: FE } = await import("./emit_full");
|
|
56
|
+
const { optimize: opt } = await import("./optimizer");
|
|
57
|
+
|
|
58
|
+
const errors: string[] = [];
|
|
59
|
+
const warnings: string[] = [];
|
|
60
|
+
|
|
61
|
+
const tokens = new L(source).tokenize();
|
|
62
|
+
const ast = new P(tokens).parse();
|
|
63
|
+
|
|
64
|
+
const typeErrors = new TC().check(ast);
|
|
65
|
+
for (const err of typeErrors) {
|
|
66
|
+
errors.push(`${err.code} at ${err.loc.line}:${err.loc.column}: ${err.message}`);
|
|
67
|
+
}
|
|
68
|
+
if (errors.length > 0) return { files: [], errors, warnings };
|
|
69
|
+
|
|
70
|
+
const hash = createHash("sha256").update(source).digest("hex").slice(0, 16);
|
|
71
|
+
const irSystems = new Lo().lower(ast, hash);
|
|
72
|
+
|
|
73
|
+
for (let i = 0; i < irSystems.length; i++) {
|
|
74
|
+
const result = opt(irSystems[i]);
|
|
75
|
+
irSystems[i] = result.system;
|
|
76
|
+
const solveResult = new CS().solve(irSystems[i]);
|
|
77
|
+
irSystems[i].resolution = solveResult.resolution;
|
|
78
|
+
for (const w of solveResult.warnings) warnings.push(w);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const files: { path: string; content: string; language: string; source_module: string }[] = [];
|
|
82
|
+
const emitter = new FE();
|
|
83
|
+
for (const sys of irSystems) {
|
|
84
|
+
files.push(...emitter.emit(sys));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return { files, errors, warnings };
|
|
88
|
+
}
|