cobolx-2 1.2.3
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/.github/pull_request_template.md +9 -0
- package/.github/workflows/ci.yml +18 -0
- package/.vscode/launch.json +19 -0
- package/.vscode/tasks.json +14 -0
- package/CHANGELOG.md +16 -0
- package/CONTRIBUTING.md +23 -0
- package/CargoX.lock +2 -0
- package/LICENSE +21 -0
- package/README.md +164 -0
- package/api-demo/CargoX.lock +1 -0
- package/api-demo/README.md +3 -0
- package/api-demo/benchmarks/arith.cbx +6 -0
- package/api-demo/cobolx.toml +6 -0
- package/api-demo/docs-output/index.html +1 -0
- package/api-demo/generated/LEGACYDEMO.cbx +5 -0
- package/api-demo/generated/client-types.ts +2 -0
- package/api-demo/generated/deploy.json +12 -0
- package/api-demo/generated/flowchart.mmd +4 -0
- package/api-demo/migrations/20260411215238_init.sql +1 -0
- package/api-demo/src/main.cbx +5 -0
- package/api-demo/tests/smoke.cbx +6 -0
- package/benchmarks/arithmetic.cbx +6 -0
- package/cargox/package.json +11 -0
- package/cargox/src/index.d.ts +4 -0
- package/cargox/src/index.js +5 -0
- package/cargox/src/index.js.map +1 -0
- package/cargox/src/index.ts +4 -0
- package/cargox/src/lockfile/index.d.ts +1 -0
- package/cargox/src/lockfile/index.js +9 -0
- package/cargox/src/lockfile/index.js.map +1 -0
- package/cargox/src/lockfile/index.ts +9 -0
- package/cargox/src/manifest.d.ts +10 -0
- package/cargox/src/manifest.js +49 -0
- package/cargox/src/manifest.js.map +1 -0
- package/cargox/src/manifest.ts +58 -0
- package/cargox/src/registry/index.d.ts +4 -0
- package/cargox/src/registry/index.js +13 -0
- package/cargox/src/registry/index.js.map +1 -0
- package/cargox/src/registry/index.ts +13 -0
- package/cargox/src/resolver/index.d.ts +5 -0
- package/cargox/src/resolver/index.js +4 -0
- package/cargox/src/resolver/index.js.map +1 -0
- package/cargox/src/resolver/index.ts +8 -0
- package/cargox/tsconfig.json +7 -0
- package/cli/cobolx-cli/package.json +23 -0
- package/cli/cobolx-cli/src/commands/add.ts +18 -0
- package/cli/cobolx-cli/src/commands/bench.ts +47 -0
- package/cli/cobolx-cli/src/commands/build.ts +74 -0
- package/cli/cobolx-cli/src/commands/check.ts +24 -0
- package/cli/cobolx-cli/src/commands/debug.ts +15 -0
- package/cli/cobolx-cli/src/commands/debug_rewind.ts +16 -0
- package/cli/cobolx-cli/src/commands/deploy.ts +18 -0
- package/cli/cobolx-cli/src/commands/dev.ts +24 -0
- package/cli/cobolx-cli/src/commands/doc.ts +19 -0
- package/cli/cobolx-cli/src/commands/fmt.ts +14 -0
- package/cli/cobolx-cli/src/commands/fuzz.ts +24 -0
- package/cli/cobolx-cli/src/commands/generate.ts +38 -0
- package/cli/cobolx-cli/src/commands/install.ts +25 -0
- package/cli/cobolx-cli/src/commands/legacy.ts +17 -0
- package/cli/cobolx-cli/src/commands/lint.ts +17 -0
- package/cli/cobolx-cli/src/commands/migrate.ts +27 -0
- package/cli/cobolx-cli/src/commands/new.ts +20 -0
- package/cli/cobolx-cli/src/commands/profile.ts +8 -0
- package/cli/cobolx-cli/src/commands/publish.ts +16 -0
- package/cli/cobolx-cli/src/commands/repl.ts +30 -0
- package/cli/cobolx-cli/src/commands/run.ts +22 -0
- package/cli/cobolx-cli/src/commands/task.ts +27 -0
- package/cli/cobolx-cli/src/commands/test.ts +44 -0
- package/cli/cobolx-cli/src/commands/update.ts +30 -0
- package/cli/cobolx-cli/src/commands/visualize.ts +25 -0
- package/cli/cobolx-cli/src/index.ts +101 -0
- package/cli/cobolx-cli/src/project.ts +74 -0
- package/cli/cobolx-cli/tsconfig.json +7 -0
- package/cobolx.toml +7 -0
- package/compiler/package.json +14 -0
- package/compiler/src/ast/types.d.ts +87 -0
- package/compiler/src/ast/types.js +2 -0
- package/compiler/src/ast/types.js.map +1 -0
- package/compiler/src/ast/types.ts +329 -0
- package/compiler/src/backend/custom.d.ts +8 -0
- package/compiler/src/backend/custom.js +12 -0
- package/compiler/src/backend/custom.js.map +1 -0
- package/compiler/src/backend/custom.ts +21 -0
- package/compiler/src/borrow_checker/checker.d.ts +3 -0
- package/compiler/src/borrow_checker/checker.js +82 -0
- package/compiler/src/borrow_checker/checker.js.map +1 -0
- package/compiler/src/borrow_checker/checker.ts +100 -0
- package/compiler/src/codegen/javascript.d.ts +2 -0
- package/compiler/src/codegen/javascript.js +89 -0
- package/compiler/src/codegen/javascript.js.map +1 -0
- package/compiler/src/codegen/javascript.ts +175 -0
- package/compiler/src/const_eval/evaluator.ts +58 -0
- package/compiler/src/diagnostics.d.ts +11 -0
- package/compiler/src/diagnostics.js +14 -0
- package/compiler/src/diagnostics.js.map +1 -0
- package/compiler/src/diagnostics.ts +20 -0
- package/compiler/src/hir/lower.d.ts +7 -0
- package/compiler/src/hir/lower.js +44 -0
- package/compiler/src/hir/lower.js.map +1 -0
- package/compiler/src/hir/lower.ts +60 -0
- package/compiler/src/hir/types.d.ts +21 -0
- package/compiler/src/hir/types.js +2 -0
- package/compiler/src/hir/types.js.map +1 -0
- package/compiler/src/hir/types.ts +26 -0
- package/compiler/src/index.d.ts +22 -0
- package/compiler/src/index.js +84 -0
- package/compiler/src/index.js.map +1 -0
- package/compiler/src/index.ts +122 -0
- package/compiler/src/lexer/lexer.d.ts +21 -0
- package/compiler/src/lexer/lexer.js +207 -0
- package/compiler/src/lexer/lexer.js.map +1 -0
- package/compiler/src/lexer/lexer.ts +274 -0
- package/compiler/src/lexer/tokens.d.ts +8 -0
- package/compiler/src/lexer/tokens.js +18 -0
- package/compiler/src/lexer/tokens.js.map +1 -0
- package/compiler/src/lexer/tokens.ts +126 -0
- package/compiler/src/macros/expand.ts +75 -0
- package/compiler/src/main.ts +4 -0
- package/compiler/src/mir/lower.d.ts +3 -0
- package/compiler/src/mir/lower.js +10 -0
- package/compiler/src/mir/lower.js.map +1 -0
- package/compiler/src/mir/lower.ts +12 -0
- package/compiler/src/mir/types.d.ts +13 -0
- package/compiler/src/mir/types.js +2 -0
- package/compiler/src/mir/types.js.map +1 -0
- package/compiler/src/mir/types.ts +16 -0
- package/compiler/src/optimizer/constantFold.d.ts +2 -0
- package/compiler/src/optimizer/constantFold.js +61 -0
- package/compiler/src/optimizer/constantFold.js.map +1 -0
- package/compiler/src/optimizer/constantFold.ts +109 -0
- package/compiler/src/parser/parser.d.ts +33 -0
- package/compiler/src/parser/parser.js +323 -0
- package/compiler/src/parser/parser.js.map +1 -0
- package/compiler/src/parser/parser.ts +710 -0
- package/compiler/src/plugins/api.ts +8 -0
- package/compiler/src/plugins/loader.ts +21 -0
- package/compiler/src/semantic/analyzer.d.ts +12 -0
- package/compiler/src/semantic/analyzer.js +144 -0
- package/compiler/src/semantic/analyzer.js.map +1 -0
- package/compiler/src/semantic/analyzer.ts +277 -0
- package/compiler/src/type_system/checker.d.ts +7 -0
- package/compiler/src/type_system/checker.js +84 -0
- package/compiler/src/type_system/checker.js.map +1 -0
- package/compiler/src/type_system/checker.ts +108 -0
- package/compiler/tsconfig.json +7 -0
- package/debugger/package.json +11 -0
- package/debugger/src/index.d.ts +1 -0
- package/debugger/src/index.js +9 -0
- package/debugger/src/index.js.map +1 -0
- package/debugger/src/index.ts +9 -0
- package/debugger/tsconfig.json +7 -0
- package/docs/CHANGELOG.md +11 -0
- package/docs/CONTRIBUTING.md +15 -0
- package/docs/LICENSE +21 -0
- package/docs/architecture.md +29 -0
- package/docs/cli.md +58 -0
- package/docs/language-spec.md +49 -0
- package/docs/packages.md +19 -0
- package/docs/platform-systems.md +31 -0
- package/docs/release-validation.md +22 -0
- package/docs/runtime.md +10 -0
- package/docs/tooling.md +17 -0
- package/docs/vscode-extension.md +40 -0
- package/enterprise-demo/CargoX.lock +2 -0
- package/enterprise-demo/README.md +3 -0
- package/enterprise-demo/benchmarks/arith.cbx +6 -0
- package/enterprise-demo/cobolx.toml +7 -0
- package/enterprise-demo/src/main.cbx +5 -0
- package/enterprise-demo/tests/smoke.cbx +6 -0
- package/examples/README.md +20 -0
- package/examples/actors-and-flags.md +8 -0
- package/examples/api-server/README.md +9 -0
- package/examples/api-server/cobolx.toml +6 -0
- package/examples/api-server/src/main.cbx +8 -0
- package/examples/debug-replay.md +7 -0
- package/examples/debugging-demo/README.md +3 -0
- package/examples/debugging-demo/cobolx.toml +6 -0
- package/examples/debugging-demo/src/main.cbx +7 -0
- package/examples/distributed-service.md +12 -0
- package/examples/distributed-system/README.md +3 -0
- package/examples/distributed-system/cobolx.toml +6 -0
- package/examples/distributed-system/generated/deploy.json +12 -0
- package/examples/distributed-system/src/main.cbx +6 -0
- package/examples/event-driven.md +5 -0
- package/examples/event-system/README.md +3 -0
- package/examples/event-system/cobolx.toml +6 -0
- package/examples/event-system/src/main.cbx +11 -0
- package/examples/functions.cbx +10 -0
- package/examples/functions.mjs +12 -0
- package/examples/hello.cbx +5 -0
- package/examples/hello.mjs +7 -0
- package/examples/legacy-sample.cob +5 -0
- package/examples/parallel-processing/README.md +3 -0
- package/examples/parallel-processing/cobolx.toml +6 -0
- package/examples/parallel-processing/src/main.cbx +6 -0
- package/examples/parallel-processing.md +8 -0
- package/examples/platform-features.cbx +32 -0
- package/examples/platform-features.mjs +35 -0
- package/examples/service.cbx +10 -0
- package/examples/workflow-engine/README.md +3 -0
- package/examples/workflow-engine/cobolx.toml +6 -0
- package/examples/workflow-engine/generated/flowchart.mmd +8 -0
- package/examples/workflow-engine/src/main.cbx +13 -0
- package/examples/workflow-engine.md +5 -0
- package/formatter/package.json +14 -0
- package/formatter/src/index.d.ts +1 -0
- package/formatter/src/index.js +59 -0
- package/formatter/src/index.js.map +1 -0
- package/formatter/src/index.ts +103 -0
- package/formatter/tsconfig.json +7 -0
- package/generated/LEGACYDEMO.cbx +5 -0
- package/install.ps1 +4 -0
- package/install.sh +5 -0
- package/linter/package.json +14 -0
- package/linter/src/index.d.ts +2 -0
- package/linter/src/index.js +18 -0
- package/linter/src/index.js.map +1 -0
- package/linter/src/index.ts +19 -0
- package/linter/tsconfig.json +7 -0
- package/lsp/server/package.json +16 -0
- package/lsp/server/src/index.ts +168 -0
- package/lsp/server/tsconfig.json +7 -0
- package/package.json +30 -0
- package/profiler/package.json +11 -0
- package/profiler/src/index.d.ts +5 -0
- package/profiler/src/index.js +11 -0
- package/profiler/src/index.js.map +1 -0
- package/profiler/src/index.ts +11 -0
- package/profiler/tsconfig.json +7 -0
- package/release.json +10 -0
- package/runtime/package.json +14 -0
- package/runtime/src/actors/index.ts +27 -0
- package/runtime/src/async/futures.ts +11 -0
- package/runtime/src/code_as_data/index.ts +5 -0
- package/runtime/src/config/index.ts +17 -0
- package/runtime/src/distributed/index.ts +13 -0
- package/runtime/src/events/index.ts +21 -0
- package/runtime/src/feature_flags/index.ts +9 -0
- package/runtime/src/gc_or_arc/index.ts +1 -0
- package/runtime/src/healing/index.ts +26 -0
- package/runtime/src/index.ts +20 -0
- package/runtime/src/intents/index.ts +9 -0
- package/runtime/src/iterators/index.ts +23 -0
- package/runtime/src/memory/arc.ts +22 -0
- package/runtime/src/observability/index.ts +23 -0
- package/runtime/src/repl/context.ts +7 -0
- package/runtime/src/scheduler/taskScheduler.ts +11 -0
- package/runtime/src/security/capabilities.ts +15 -0
- package/runtime/src/security/secrets.ts +13 -0
- package/runtime/src/state/versioned.ts +31 -0
- package/runtime/src/std_hooks/audit.ts +23 -0
- package/runtime/src/time_travel/index.ts +54 -0
- package/runtime/src/workflows/index.ts +35 -0
- package/runtime/tsconfig.json +7 -0
- package/sample-payroll/README.md +3 -0
- package/sample-payroll/cobolx.toml +3 -0
- package/sample-payroll/src/main.cbx +5 -0
- package/stdlib/business/index.js +15 -0
- package/stdlib/core/README.md +8 -0
- package/stdlib/core/runtime.js +191 -0
- package/stdlib/crypto/index.js +5 -0
- package/stdlib/datetime/index.js +3 -0
- package/stdlib/fs/index.js +9 -0
- package/stdlib/http/index.js +7 -0
- package/stdlib/io/index.js +1 -0
- package/stdlib/json/index.js +7 -0
- package/stdlib/net/index.js +4 -0
- package/tests/macros.cbx +10 -0
- package/tests/pattern_match.cbx +16 -0
- package/tests/smoke.cbx +7 -0
- package/tests/validate-release.mjs +54 -0
- package/tsconfig.base.json +24 -0
- package/vscode-extension/LICENSE +21 -0
- package/vscode-extension/README.md +47 -0
- package/vscode-extension/cobolx-1.2.0.vsix +0 -0
- package/vscode-extension/icon.png +0 -0
- package/vscode-extension/icon.svg +15 -0
- package/vscode-extension/language-configuration.json +17 -0
- package/vscode-extension/package.json +148 -0
- package/vscode-extension/snippets/cobolx.code-snippets +49 -0
- package/vscode-extension/src/extension.ts +283 -0
- package/vscode-extension/syntaxes/cobolx.tmLanguage.json +75 -0
- package/vscode-extension/tests/test.cbl +3 -0
- package/vscode-extension/tsconfig.json +7 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { generateJavaScript } from "../codegen/javascript.js";
|
|
2
|
+
function instructionCount(program) {
|
|
3
|
+
return program.body.length + program.functions.reduce((total, fn) => total + fn.instructions.length, 0);
|
|
4
|
+
}
|
|
5
|
+
export function emitCustomBackend(program, mir, outputFilePath, stdlibDir) {
|
|
6
|
+
return {
|
|
7
|
+
target: "js-custom-backend",
|
|
8
|
+
code: generateJavaScript(program, outputFilePath, stdlibDir),
|
|
9
|
+
instructionCount: instructionCount(mir)
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=custom.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"custom.js","sourceRoot":"","sources":["custom.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAS9D,SAAS,gBAAgB,CAAC,OAAmB;IAC3C,OAAO,OAAO,CAAC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE,CAAC,KAAK,GAAG,EAAE,CAAC,YAAY,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;AAC1G,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,OAAoB,EAAE,GAAe,EAAE,cAAsB,EAAE,SAAiB;IAChH,OAAO;QACL,MAAM,EAAE,mBAAmB;QAC3B,IAAI,EAAE,kBAAkB,CAAC,OAAO,EAAE,cAAc,EAAE,SAAS,CAAC;QAC5D,gBAAgB,EAAE,gBAAgB,CAAC,GAAG,CAAC;KACxC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { MIRInstruction, MIRProgram } from "../mir/types.js";
|
|
2
|
+
import { generateJavaScript } from "../codegen/javascript.js";
|
|
3
|
+
import type { ProgramNode } from "../ast/types.js";
|
|
4
|
+
|
|
5
|
+
export interface BackendOutput {
|
|
6
|
+
target: "js-custom-backend";
|
|
7
|
+
code: string;
|
|
8
|
+
instructionCount: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function instructionCount(program: MIRProgram): number {
|
|
12
|
+
return program.body.length + program.functions.reduce((total, fn) => total + fn.instructions.length, 0);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function emitCustomBackend(program: ProgramNode, mir: MIRProgram, outputFilePath: string, stdlibDir: string): BackendOutput {
|
|
16
|
+
return {
|
|
17
|
+
target: "js-custom-backend",
|
|
18
|
+
code: generateJavaScript(program, outputFilePath, stdlibDir),
|
|
19
|
+
instructionCount: instructionCount(mir)
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
function collectCalls(expression, calls) {
|
|
2
|
+
switch (expression.kind) {
|
|
3
|
+
case "CallExpression":
|
|
4
|
+
calls.push(expression);
|
|
5
|
+
for (const arg of expression.args) {
|
|
6
|
+
collectCalls(arg, calls);
|
|
7
|
+
}
|
|
8
|
+
break;
|
|
9
|
+
case "BinaryExpression":
|
|
10
|
+
collectCalls(expression.left, calls);
|
|
11
|
+
collectCalls(expression.right, calls);
|
|
12
|
+
break;
|
|
13
|
+
case "UnaryExpression":
|
|
14
|
+
collectCalls(expression.operand, calls);
|
|
15
|
+
break;
|
|
16
|
+
default:
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function collectStatementCalls(statement, calls) {
|
|
21
|
+
switch (statement.kind) {
|
|
22
|
+
case "SetStatement":
|
|
23
|
+
case "DisplayStatement":
|
|
24
|
+
case "ReturnStatement":
|
|
25
|
+
case "ExpressionStatement":
|
|
26
|
+
collectCalls(statement.expression, calls);
|
|
27
|
+
break;
|
|
28
|
+
case "InputStatement":
|
|
29
|
+
if (statement.prompt) {
|
|
30
|
+
collectCalls(statement.prompt, calls);
|
|
31
|
+
}
|
|
32
|
+
break;
|
|
33
|
+
case "IfStatement":
|
|
34
|
+
collectCalls(statement.condition, calls);
|
|
35
|
+
for (const child of statement.thenBranch) {
|
|
36
|
+
collectStatementCalls(child, calls);
|
|
37
|
+
}
|
|
38
|
+
for (const child of statement.elseBranch) {
|
|
39
|
+
collectStatementCalls(child, calls);
|
|
40
|
+
}
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export function runBorrowChecker(hir) {
|
|
45
|
+
const diagnostics = [];
|
|
46
|
+
const mutationMap = new Map(hir.functions.map((fn) => [fn.name, fn.mutatedParams]));
|
|
47
|
+
const calls = [];
|
|
48
|
+
for (const statement of hir.program.body) {
|
|
49
|
+
collectStatementCalls(statement, calls);
|
|
50
|
+
}
|
|
51
|
+
for (const fn of hir.program.functions) {
|
|
52
|
+
for (const statement of fn.body) {
|
|
53
|
+
collectStatementCalls(statement, calls);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
for (const call of calls) {
|
|
57
|
+
const mutatedParams = mutationMap.get(call.callee) ?? [];
|
|
58
|
+
if (mutatedParams.length < 2) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const identifiers = call.args.map((arg) => (arg.kind === "Identifier" ? arg.name : undefined));
|
|
62
|
+
const seen = new Map();
|
|
63
|
+
for (let index = 0; index < call.args.length; index += 1) {
|
|
64
|
+
const identifier = identifiers[index];
|
|
65
|
+
if (!identifier) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (seen.has(identifier)) {
|
|
69
|
+
diagnostics.push({
|
|
70
|
+
message: `Borrow checker: '${identifier}' is passed multiple times to mutating function '${call.callee}'`,
|
|
71
|
+
severity: "error",
|
|
72
|
+
range: call.range
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
seen.set(identifier, index);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return diagnostics;
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=checker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checker.js","sourceRoot":"","sources":["checker.ts"],"names":[],"mappings":"AAIA,SAAS,YAAY,CAAC,UAA0B,EAAE,KAA2B;IAC3E,QAAQ,UAAU,CAAC,IAAI,EAAE,CAAC;QACxB,KAAK,gBAAgB;YACnB,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YACvB,KAAK,MAAM,GAAG,IAAI,UAAU,CAAC,IAAI,EAAE,CAAC;gBAClC,YAAY,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YAC3B,CAAC;YACD,MAAM;QACR,KAAK,kBAAkB;YACrB,YAAY,CAAC,UAAU,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YACrC,YAAY,CAAC,UAAU,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;YACtC,MAAM;QACR,KAAK,iBAAiB;YACpB,YAAY,CAAC,UAAU,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YACxC,MAAM;QACR;YACE,MAAM;IACV,CAAC;AACH,CAAC;AAED,SAAS,qBAAqB,CAAC,SAAwB,EAAE,KAA2B;IAClF,QAAQ,SAAS,CAAC,IAAI,EAAE,CAAC;QACvB,KAAK,cAAc,CAAC;QACpB,KAAK,kBAAkB,CAAC;QACxB,KAAK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AAC;IACH,CAAC;IAED,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,aAAa,GAAG,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;QACzD,IAAI,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC7B,SAAS;QACX,CAAC;QACD,MAAM,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,KAAK,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC;QAC/F,MAAM,IAAI,GAAG,IAAI,GAAG,EAAkB,CAAC;QACvC,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,IAAI,CAAC,EAAE,CAAC;YACzD,MAAM,UAAU,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC;YACtC,IAAI,CAAC,UAAU,EAAE,CAAC;gBAChB,SAAS;YACX,CAAC;YACD,IAAI,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC;gBACzB,WAAW,CAAC,IAAI,CAAC;oBACf,OAAO,EAAE,oBAAoB,UAAU,oDAAoD,IAAI,CAAC,MAAM,GAAG;oBACzG,QAAQ,EAAE,OAAO;oBACjB,KAAK,EAAE,IAAI,CAAC,KAAK;iBAClB,CAAC,CAAC;YACL,CAAC;iBAAM,CAAC;gBACN,IAAI,CAAC,GAAG,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;YAC9B,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,WAAW,CAAC;AACrB,CAAC"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type { CallExpressionNode, ExpressionNode, StatementNode } from "../ast/types.js";
|
|
2
|
+
import type { Diagnostic } from "../diagnostics.js";
|
|
3
|
+
import type { HIRProgram } from "../hir/types.js";
|
|
4
|
+
|
|
5
|
+
function collectCalls(expression: ExpressionNode, calls: CallExpressionNode[]): void {
|
|
6
|
+
switch (expression.kind) {
|
|
7
|
+
case "CallExpression":
|
|
8
|
+
calls.push(expression);
|
|
9
|
+
collectCalls(expression.callee, calls);
|
|
10
|
+
for (const arg of expression.args) collectCalls(arg, calls);
|
|
11
|
+
break;
|
|
12
|
+
case "BinaryExpression":
|
|
13
|
+
collectCalls(expression.left, calls);
|
|
14
|
+
collectCalls(expression.right, calls);
|
|
15
|
+
break;
|
|
16
|
+
case "UnaryExpression":
|
|
17
|
+
collectCalls(expression.operand, calls);
|
|
18
|
+
break;
|
|
19
|
+
case "TryExpression":
|
|
20
|
+
collectCalls(expression.expression, calls);
|
|
21
|
+
break;
|
|
22
|
+
case "MemberExpression":
|
|
23
|
+
collectCalls(expression.object, calls);
|
|
24
|
+
break;
|
|
25
|
+
case "MacroInvocation":
|
|
26
|
+
for (const arg of expression.args) collectCalls(arg, calls);
|
|
27
|
+
break;
|
|
28
|
+
case "EnumConstructorExpression":
|
|
29
|
+
for (const field of expression.fields) collectCalls(field, calls);
|
|
30
|
+
break;
|
|
31
|
+
case "ArrayLiteral":
|
|
32
|
+
for (const item of expression.items) collectCalls(item, calls);
|
|
33
|
+
break;
|
|
34
|
+
default:
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function collectStatementCalls(statement: StatementNode, calls: CallExpressionNode[]): void {
|
|
40
|
+
switch (statement.kind) {
|
|
41
|
+
case "LetStatement":
|
|
42
|
+
collectCalls(statement.expression, calls);
|
|
43
|
+
break;
|
|
44
|
+
case "SetStatement":
|
|
45
|
+
case "DisplayStatement":
|
|
46
|
+
case "ReturnStatement":
|
|
47
|
+
case "ExpressionStatement":
|
|
48
|
+
case "AssertStatement":
|
|
49
|
+
case "SpawnStatement":
|
|
50
|
+
collectCalls(statement.expression, calls);
|
|
51
|
+
break;
|
|
52
|
+
case "InputStatement":
|
|
53
|
+
if (statement.prompt) collectCalls(statement.prompt, calls);
|
|
54
|
+
break;
|
|
55
|
+
case "IfStatement":
|
|
56
|
+
collectCalls(statement.condition, calls);
|
|
57
|
+
for (const child of statement.thenBranch) collectStatementCalls(child, calls);
|
|
58
|
+
for (const child of statement.elseBranch) collectStatementCalls(child, calls);
|
|
59
|
+
break;
|
|
60
|
+
case "MatchStatement":
|
|
61
|
+
collectCalls(statement.expression, calls);
|
|
62
|
+
for (const arm of statement.arms) for (const child of arm.body) collectStatementCalls(child, calls);
|
|
63
|
+
break;
|
|
64
|
+
case "UnsafeBlock":
|
|
65
|
+
case "BlockStatement":
|
|
66
|
+
for (const child of statement.body) collectStatementCalls(child, calls);
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function runBorrowChecker(hir: HIRProgram): Diagnostic[] {
|
|
72
|
+
const diagnostics: Diagnostic[] = [];
|
|
73
|
+
const mutationMap = new Map(hir.functions.map((fn) => [fn.name, fn.mutatedParams]));
|
|
74
|
+
const calls: CallExpressionNode[] = [];
|
|
75
|
+
|
|
76
|
+
for (const statement of hir.program.body) collectStatementCalls(statement, calls);
|
|
77
|
+
for (const fn of hir.program.functions) for (const statement of fn.body) collectStatementCalls(statement, calls);
|
|
78
|
+
|
|
79
|
+
for (const call of calls) {
|
|
80
|
+
if (call.callee.kind !== "Identifier") continue;
|
|
81
|
+
const calleeName = call.callee.name;
|
|
82
|
+
const mutatedParams = mutationMap.get(calleeName) ?? [];
|
|
83
|
+
if (mutatedParams.length < 2) continue;
|
|
84
|
+
const identifiers = call.args.map((arg) => (arg.kind === "Identifier" ? arg.name : undefined));
|
|
85
|
+
const seen = new Set<string>();
|
|
86
|
+
for (const identifier of identifiers) {
|
|
87
|
+
if (!identifier) continue;
|
|
88
|
+
if (seen.has(identifier)) {
|
|
89
|
+
diagnostics.push({
|
|
90
|
+
message: `Borrow checker: '${identifier}' is passed multiple times to mutating function '${calleeName}'`,
|
|
91
|
+
severity: "error",
|
|
92
|
+
range: call.range
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
seen.add(identifier);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return diagnostics;
|
|
100
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
class EmitScope {
|
|
3
|
+
parent;
|
|
4
|
+
declarations = new Set();
|
|
5
|
+
constructor(parent) {
|
|
6
|
+
this.parent = parent;
|
|
7
|
+
}
|
|
8
|
+
has(name) {
|
|
9
|
+
if (this.declarations.has(name)) {
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
return this.parent?.has(name) ?? false;
|
|
13
|
+
}
|
|
14
|
+
declare(name) {
|
|
15
|
+
this.declarations.add(name);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function emitExpression(expression) {
|
|
19
|
+
switch (expression.kind) {
|
|
20
|
+
case "NumberLiteral":
|
|
21
|
+
return String(expression.value);
|
|
22
|
+
case "StringLiteral":
|
|
23
|
+
return JSON.stringify(expression.value);
|
|
24
|
+
case "BooleanLiteral":
|
|
25
|
+
return expression.value ? "true" : "false";
|
|
26
|
+
case "Identifier":
|
|
27
|
+
return expression.name;
|
|
28
|
+
case "UnaryExpression":
|
|
29
|
+
return `${expression.operator}${emitExpression(expression.operand)}`;
|
|
30
|
+
case "BinaryExpression":
|
|
31
|
+
return `(${emitExpression(expression.left)} ${expression.operator} ${emitExpression(expression.right)})`;
|
|
32
|
+
case "CallExpression":
|
|
33
|
+
return `${expression.callee}(${expression.args.map(emitExpression).join(", ")})`;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function emitStatement(statement, indent, scope) {
|
|
37
|
+
switch (statement.kind) {
|
|
38
|
+
case "SetStatement":
|
|
39
|
+
if (scope.has(statement.name)) {
|
|
40
|
+
return `${indent}${statement.name} = ${emitExpression(statement.expression)};`;
|
|
41
|
+
}
|
|
42
|
+
scope.declare(statement.name);
|
|
43
|
+
return `${indent}let ${statement.name} = ${emitExpression(statement.expression)};`;
|
|
44
|
+
case "DisplayStatement":
|
|
45
|
+
return `${indent}display(${emitExpression(statement.expression)});`;
|
|
46
|
+
case "InputStatement":
|
|
47
|
+
if (scope.has(statement.name)) {
|
|
48
|
+
return `${indent}${statement.name} = input(${statement.prompt ? emitExpression(statement.prompt) : "\"\""});`;
|
|
49
|
+
}
|
|
50
|
+
scope.declare(statement.name);
|
|
51
|
+
return `${indent}let ${statement.name} = input(${statement.prompt ? emitExpression(statement.prompt) : "\"\""});`;
|
|
52
|
+
case "IfStatement": {
|
|
53
|
+
const thenScope = new EmitScope(scope);
|
|
54
|
+
const elseScope = new EmitScope(scope);
|
|
55
|
+
const thenBranch = statement.thenBranch.map((child) => emitStatement(child, `${indent} `, thenScope)).join("\n");
|
|
56
|
+
const elseBranch = statement.elseBranch.map((child) => emitStatement(child, `${indent} `, elseScope)).join("\n");
|
|
57
|
+
const elseSection = elseBranch ? `\n${indent}else {\n${elseBranch}\n${indent}}` : "";
|
|
58
|
+
return `${indent}if (${emitExpression(statement.condition)}) {\n${thenBranch}\n${indent}}${elseSection}`;
|
|
59
|
+
}
|
|
60
|
+
case "ReturnStatement":
|
|
61
|
+
return `${indent}return ${emitExpression(statement.expression)};`;
|
|
62
|
+
case "ExpressionStatement":
|
|
63
|
+
return `${indent}${emitExpression(statement.expression)};`;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function emitFunction(fn) {
|
|
67
|
+
const scope = new EmitScope();
|
|
68
|
+
for (const param of fn.params) {
|
|
69
|
+
scope.declare(param);
|
|
70
|
+
}
|
|
71
|
+
const body = fn.body.map((statement) => emitStatement(statement, " ", scope)).join("\n");
|
|
72
|
+
return `function ${fn.name}(${fn.params.join(", ")}) {\n${body}\n}`;
|
|
73
|
+
}
|
|
74
|
+
export function generateJavaScript(program, outputFilePath, stdlibDir) {
|
|
75
|
+
const runtimeImport = path.relative(path.dirname(outputFilePath), path.join(stdlibDir, "runtime.js")).replace(/\\/g, "/");
|
|
76
|
+
const runtimePath = runtimeImport.startsWith(".") ? runtimeImport : `./${runtimeImport}`;
|
|
77
|
+
const functionOutput = program.functions.map(emitFunction).join("\n\n");
|
|
78
|
+
const bodyScope = new EmitScope();
|
|
79
|
+
const bodyOutput = program.body.map((statement) => emitStatement(statement, " ", bodyScope)).join("\n");
|
|
80
|
+
return `import { display, input, math, strings } from "${runtimePath}";
|
|
81
|
+
|
|
82
|
+
${functionOutput ? `${functionOutput}\n\n` : ""}function main() {
|
|
83
|
+
${bodyOutput}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
main();
|
|
87
|
+
`;
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=javascript.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"javascript.js","sourceRoot":"","sources":["javascript.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAQ7B,MAAM,SAAS;IAGgB;IAFZ,YAAY,GAAG,IAAI,GAAG,EAAU,CAAC;IAElD,YAA6B,MAAkB;QAAlB,WAAM,GAAN,MAAM,CAAY;IAAG,CAAC;IAEnD,GAAG,CAAC,IAAY;QACd,IAAI,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YAChC,OAAO,IAAI,CAAC;QACd,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC;IACzC,CAAC;IAED,OAAO,CAAC,IAAY;QAClB,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC9B,CAAC;CACF;AAED,SAAS,cAAc,CAAC,UAA0B;IAChD,QAAQ,UAAU,CAAC,IAAI,EAAE,CAAC;QACxB,KAAK,eAAe;YAClB,OAAO,MAAM,CAAC,UAAU,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GAAG,OAAO,CAAC,SAAS,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACxE,MAAM,SAAS,GAAG,IAAI,SAAS,EAAE,CAAC;IAClC,MAAM,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,aAAa,CAAC,SAAS,EAAE,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAEzG,OAAO,kDAAkD,WAAW;;EAEpE,cAAc,CAAC,CAAC,CAAC,GAAG,cAAc,MAAM,CAAC,CAAC,CAAC,EAAE;EAC7C,UAAU;;;;CAIX,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import type {
|
|
3
|
+
ConstDeclarationNode,
|
|
4
|
+
ExpressionNode,
|
|
5
|
+
FunctionDeclarationNode,
|
|
6
|
+
MatchArmNode,
|
|
7
|
+
PatternNode,
|
|
8
|
+
ProgramNode,
|
|
9
|
+
StatementNode
|
|
10
|
+
} from "../ast/types.js";
|
|
11
|
+
|
|
12
|
+
class EmitScope {
|
|
13
|
+
private readonly declarations = new Set<string>();
|
|
14
|
+
constructor(private readonly parent?: EmitScope) {}
|
|
15
|
+
has(name: string): boolean {
|
|
16
|
+
if (this.declarations.has(name)) return true;
|
|
17
|
+
return this.parent?.has(name) ?? false;
|
|
18
|
+
}
|
|
19
|
+
declare(name: string): void {
|
|
20
|
+
this.declarations.add(name);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function emitPattern(pattern: PatternNode, valueRef: string): { condition: string; bindings: string[] } {
|
|
25
|
+
switch (pattern.kind) {
|
|
26
|
+
case "WildcardPattern":
|
|
27
|
+
return { condition: "true", bindings: [] };
|
|
28
|
+
case "IdentifierPattern":
|
|
29
|
+
return { condition: "true", bindings: [`const ${pattern.name} = ${valueRef};`] };
|
|
30
|
+
case "LiteralPattern":
|
|
31
|
+
return { condition: `${valueRef} === ${emitExpression(pattern.expression)}`, bindings: [] };
|
|
32
|
+
case "VariantPattern":
|
|
33
|
+
return {
|
|
34
|
+
condition: `${valueRef}?.tag === ${JSON.stringify(pattern.variantName)}`,
|
|
35
|
+
bindings: pattern.bindings.map((binding, index) => `const ${binding} = ${valueRef}.values[${index}];`)
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function emitExpression(expression: ExpressionNode): string {
|
|
41
|
+
switch (expression.kind) {
|
|
42
|
+
case "NumberLiteral":
|
|
43
|
+
return String(expression.value);
|
|
44
|
+
case "StringLiteral":
|
|
45
|
+
return JSON.stringify(expression.value);
|
|
46
|
+
case "BooleanLiteral":
|
|
47
|
+
return expression.value ? "true" : "false";
|
|
48
|
+
case "Identifier":
|
|
49
|
+
return expression.name;
|
|
50
|
+
case "ArrayLiteral":
|
|
51
|
+
return `[${expression.items.map(emitExpression).join(", ")}]`;
|
|
52
|
+
case "UnaryExpression":
|
|
53
|
+
return `${expression.operator}${emitExpression(expression.operand)}`;
|
|
54
|
+
case "BinaryExpression":
|
|
55
|
+
return `(${emitExpression(expression.left)} ${expression.operator} ${emitExpression(expression.right)})`;
|
|
56
|
+
case "CallExpression":
|
|
57
|
+
return `${emitExpression(expression.callee)}(${expression.args.map(emitExpression).join(", ")})`;
|
|
58
|
+
case "MacroInvocation":
|
|
59
|
+
return `${expression.name}(${expression.args.map(emitExpression).join(", ")})`;
|
|
60
|
+
case "TryExpression":
|
|
61
|
+
return `__propagate(${emitExpression(expression.expression)})`;
|
|
62
|
+
case "MemberExpression":
|
|
63
|
+
return `${emitExpression(expression.object)}.${expression.property}`;
|
|
64
|
+
case "EnumConstructorExpression":
|
|
65
|
+
return `__variant(${JSON.stringify(expression.variantName)}, [${expression.fields.map(emitExpression).join(", ")}])`;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function emitStatement(statement: StatementNode, indent: string, scope: EmitScope): string {
|
|
70
|
+
switch (statement.kind) {
|
|
71
|
+
case "LetStatement": {
|
|
72
|
+
const name = statement.binding.name;
|
|
73
|
+
if (!scope.has(name)) scope.declare(name);
|
|
74
|
+
return `${indent}${statement.isMutable ? "let" : "const"} ${name} = ${emitExpression(statement.expression)};\n${indent}__debug.set(${JSON.stringify(name)}, ${name});`;
|
|
75
|
+
}
|
|
76
|
+
case "SetStatement":
|
|
77
|
+
if (scope.has(statement.name)) {
|
|
78
|
+
return `${indent}${statement.name} = ${emitExpression(statement.expression)};\n${indent}__debug.set(${JSON.stringify(statement.name)}, ${statement.name});`;
|
|
79
|
+
}
|
|
80
|
+
scope.declare(statement.name);
|
|
81
|
+
return `${indent}let ${statement.name} = ${emitExpression(statement.expression)};\n${indent}__debug.set(${JSON.stringify(statement.name)}, ${statement.name});`;
|
|
82
|
+
case "DisplayStatement":
|
|
83
|
+
return `${indent}__debug.record(${JSON.stringify("display")});\n${indent}display(${emitExpression(statement.expression)});`;
|
|
84
|
+
case "InputStatement":
|
|
85
|
+
if (scope.has(statement.name)) {
|
|
86
|
+
return `${indent}${statement.name} = input(${statement.prompt ? emitExpression(statement.prompt) : "\"\""});\n${indent}__debug.set(${JSON.stringify(statement.name)}, ${statement.name});`;
|
|
87
|
+
}
|
|
88
|
+
scope.declare(statement.name);
|
|
89
|
+
return `${indent}let ${statement.name} = input(${statement.prompt ? emitExpression(statement.prompt) : "\"\""});\n${indent}__debug.set(${JSON.stringify(statement.name)}, ${statement.name});`;
|
|
90
|
+
case "IfStatement": {
|
|
91
|
+
const thenScope = new EmitScope(scope);
|
|
92
|
+
const elseScope = new EmitScope(scope);
|
|
93
|
+
const thenBranch = statement.thenBranch.map((child) => emitStatement(child, `${indent} `, thenScope)).join("\n");
|
|
94
|
+
const elseBranch = statement.elseBranch.map((child) => emitStatement(child, `${indent} `, elseScope)).join("\n");
|
|
95
|
+
const elseSection = elseBranch ? `\n${indent}else {\n${elseBranch}\n${indent}}` : "";
|
|
96
|
+
return `${indent}if (${emitExpression(statement.condition)}) {\n${thenBranch}\n${indent}}${elseSection}`;
|
|
97
|
+
}
|
|
98
|
+
case "MatchStatement":
|
|
99
|
+
return emitMatchStatement(statement.arms, emitExpression(statement.expression), indent, scope);
|
|
100
|
+
case "UnsafeBlock":
|
|
101
|
+
return `${indent}{\n${indent} __debug.record(${JSON.stringify("unsafe:enter")});\n${indent} // unsafe block\n${statement.body.map((child) => emitStatement(child, `${indent} `, new EmitScope(scope))).join("\n")}\n${indent} __debug.record(${JSON.stringify("unsafe:exit")});\n${indent}}`;
|
|
102
|
+
case "AssertStatement":
|
|
103
|
+
return `${indent}if (!(${emitExpression(statement.expression)})) throw new Error("Assertion failed");\n${indent}__debug.record(${JSON.stringify("assert")});`;
|
|
104
|
+
case "SpawnStatement":
|
|
105
|
+
return `${indent}__debug.record(${JSON.stringify("spawn")});\n${indent}spawn(() => ${emitExpression(statement.expression)});`;
|
|
106
|
+
case "BlockStatement":
|
|
107
|
+
return `${indent}{\n${statement.body.map((child) => emitStatement(child, `${indent} `, new EmitScope(scope))).join("\n")}\n${indent}}`;
|
|
108
|
+
case "ReturnStatement":
|
|
109
|
+
return `${indent}__debug.record(${JSON.stringify("return")});\n${indent}return ${emitExpression(statement.expression)};`;
|
|
110
|
+
case "ExpressionStatement":
|
|
111
|
+
return `${indent}${emitExpression(statement.expression)};\n${indent}__debug.record(${JSON.stringify("expression")});`;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function emitMatchStatement(arms: MatchArmNode[], valueExpression: string, indent: string, scope: EmitScope): string {
|
|
116
|
+
const ref = "__matchValue";
|
|
117
|
+
const lines = [`${indent}{`, `${indent} const ${ref} = ${valueExpression};`];
|
|
118
|
+
arms.forEach((arm, index) => {
|
|
119
|
+
const pattern = emitPattern(arm.pattern, ref);
|
|
120
|
+
lines.push(`${indent} ${index === 0 ? "if" : "else if"} (${pattern.condition}) {`);
|
|
121
|
+
for (const binding of pattern.bindings) lines.push(`${indent} ${binding}`);
|
|
122
|
+
const armScope = new EmitScope(scope);
|
|
123
|
+
lines.push(...arm.body.map((child) => emitStatement(child, `${indent} `, armScope)));
|
|
124
|
+
lines.push(`${indent} }`);
|
|
125
|
+
});
|
|
126
|
+
lines.push(`${indent}}`);
|
|
127
|
+
return lines.join("\n");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function emitFunction(fn: FunctionDeclarationNode): string {
|
|
131
|
+
const scope = new EmitScope();
|
|
132
|
+
for (const param of fn.signature.params) scope.declare(param.name);
|
|
133
|
+
const asyncKeyword = fn.signature.isAsync ? "async " : "";
|
|
134
|
+
const body = fn.body.map((statement) => emitStatement(statement, " ", scope)).join("\n");
|
|
135
|
+
return `${asyncKeyword}function ${fn.signature.name}(${fn.signature.params.map((param) => param.name).join(", ")}) {\n${body}\n}`;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function emitConst(constant: ConstDeclarationNode): string {
|
|
139
|
+
return `const ${constant.name} = ${emitExpression(constant.expression)};`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function generateJavaScript(program: ProgramNode, outputFilePath: string, stdlibDir: string): string {
|
|
143
|
+
const runtimeImport = path.relative(path.dirname(outputFilePath), path.join(stdlibDir, "runtime.js")).replace(/\\/g, "/");
|
|
144
|
+
const runtimePath = runtimeImport.startsWith(".") ? runtimeImport : `./${runtimeImport}`;
|
|
145
|
+
const exports = program.exports.map((item) => item.name);
|
|
146
|
+
const bodyScope = new EmitScope();
|
|
147
|
+
const macroFunctions = program.macros.map((macro) =>
|
|
148
|
+
`function ${macro.name}(${macro.params.join(", ")}) {\n${macro.body.map((statement) => emitStatement(statement, " ", new EmitScope())).join("\n")}\n}`
|
|
149
|
+
);
|
|
150
|
+
const enumHelpers = program.enums.flatMap((enumDecl) =>
|
|
151
|
+
enumDecl.variants.map((variant) => `const ${variant.name} = (...values) => __variant(${JSON.stringify(variant.name)}, values);`)
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
return `import { display, input, math, strings, Result, Option, iter, spawn, channel, createDebuggerContext } from "${runtimePath}";
|
|
155
|
+
${program.imports.map((item) => `import * as ${item.alias ?? item.importedName} from ${JSON.stringify(item.modulePath.replace(/\\.cbx$/i, ".mjs"))};`).join("\n")}
|
|
156
|
+
|
|
157
|
+
function __variant(tag, values) { return { tag, values }; }
|
|
158
|
+
function __propagate(value) { if (value && value.tag === "ERR") throw new Error(String(value.values?.[0] ?? "propagated error")); return value?.tag === "OK" ? value.values[0] : value; }
|
|
159
|
+
const __debug = createDebuggerContext();
|
|
160
|
+
|
|
161
|
+
${program.consts.map(emitConst).join("\n")}
|
|
162
|
+
${enumHelpers.join("\n")}
|
|
163
|
+
${macroFunctions.join("\n\n")}
|
|
164
|
+
${program.functions.map(emitFunction).join("\n\n")}
|
|
165
|
+
|
|
166
|
+
function main() {
|
|
167
|
+
__debug.record("main:start");
|
|
168
|
+
${program.body.map((statement) => emitStatement(statement, " ", bodyScope)).join("\n")}
|
|
169
|
+
__debug.record("main:end");
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
main();
|
|
173
|
+
${exports.length > 0 ? `\nexport { ${exports.join(", ")} };` : ""}
|
|
174
|
+
`;
|
|
175
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { ConstDeclarationNode, ExpressionNode, ProgramNode } from "../ast/types.js";
|
|
2
|
+
|
|
3
|
+
function evaluateExpression(expression: ExpressionNode, values: Map<string, number | string | boolean>): number | string | boolean | undefined {
|
|
4
|
+
switch (expression.kind) {
|
|
5
|
+
case "NumberLiteral":
|
|
6
|
+
return expression.value;
|
|
7
|
+
case "StringLiteral":
|
|
8
|
+
return expression.value;
|
|
9
|
+
case "BooleanLiteral":
|
|
10
|
+
return expression.value;
|
|
11
|
+
case "Identifier":
|
|
12
|
+
return values.get(expression.name);
|
|
13
|
+
case "UnaryExpression": {
|
|
14
|
+
const value = evaluateExpression(expression.operand, values);
|
|
15
|
+
if (typeof value === "number" && expression.operator === "-") return -value;
|
|
16
|
+
if (typeof value === "boolean" && expression.operator === "!") return !value;
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
case "BinaryExpression": {
|
|
20
|
+
const left = evaluateExpression(expression.left, values);
|
|
21
|
+
const right = evaluateExpression(expression.right, values);
|
|
22
|
+
if (typeof left === "number" && typeof right === "number") {
|
|
23
|
+
switch (expression.operator) {
|
|
24
|
+
case "+":
|
|
25
|
+
return left + right;
|
|
26
|
+
case "-":
|
|
27
|
+
return left - right;
|
|
28
|
+
case "*":
|
|
29
|
+
return left * right;
|
|
30
|
+
case "/":
|
|
31
|
+
return left / right;
|
|
32
|
+
case "%":
|
|
33
|
+
return left % right;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (expression.operator === "==" && left !== undefined && right !== undefined) return left === right;
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
default:
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function literalNode(constant: ConstDeclarationNode, value: number | string | boolean): ExpressionNode {
|
|
45
|
+
if (typeof value === "number") return { kind: "NumberLiteral", value, range: constant.range };
|
|
46
|
+
if (typeof value === "boolean") return { kind: "BooleanLiteral", value, range: constant.range };
|
|
47
|
+
return { kind: "StringLiteral", value, range: constant.range };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function evaluateConstDeclarations(program: ProgramNode): ProgramNode {
|
|
51
|
+
const values = new Map<string, number | string | boolean>();
|
|
52
|
+
const consts = program.consts.map((constant) => {
|
|
53
|
+
const evaluated = evaluateExpression(constant.expression, values);
|
|
54
|
+
if (evaluated !== undefined) values.set(constant.name, evaluated);
|
|
55
|
+
return evaluated === undefined ? constant : { ...constant, expression: literalNode(constant, evaluated) };
|
|
56
|
+
});
|
|
57
|
+
return { ...program, consts };
|
|
58
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { SourceRange } from "./ast/types.js";
|
|
2
|
+
export interface Diagnostic {
|
|
3
|
+
message: string;
|
|
4
|
+
range: SourceRange;
|
|
5
|
+
severity: "error" | "warning";
|
|
6
|
+
}
|
|
7
|
+
export declare class CobolxError extends Error {
|
|
8
|
+
readonly diagnostic: Diagnostic;
|
|
9
|
+
constructor(diagnostic: Diagnostic);
|
|
10
|
+
}
|
|
11
|
+
export declare function formatDiagnostic(filePath: string, diagnostic: Diagnostic): string;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export class CobolxError extends Error {
|
|
2
|
+
diagnostic;
|
|
3
|
+
constructor(diagnostic) {
|
|
4
|
+
super(diagnostic.message);
|
|
5
|
+
this.diagnostic = diagnostic;
|
|
6
|
+
this.name = "CobolxError";
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function formatDiagnostic(filePath, diagnostic) {
|
|
10
|
+
const { line, column } = diagnostic.range.start;
|
|
11
|
+
const label = diagnostic.severity === "warning" ? "Warning" : "Error";
|
|
12
|
+
return `${label}: ${diagnostic.message} at ${filePath}:${line}:${column}`;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=diagnostics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diagnostics.js","sourceRoot":"","sources":["diagnostics.ts"],"names":[],"mappings":"AAQA,MAAM,OAAO,WAAY,SAAQ,KAAK;IACR;IAA5B,YAA4B,UAAsB;QAChD,KAAK,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QADA,eAAU,GAAV,UAAU,CAAY;QAEhD,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;IAC5B,CAAC;CACF;AAED,MAAM,UAAU,gBAAgB,CAAC,QAAgB,EAAE,UAAsB;IACvE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC;IAChD,MAAM,KAAK,GAAG,UAAU,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC;IACtE,OAAO,GAAG,KAAK,KAAK,UAAU,CAAC,OAAO,OAAO,QAAQ,IAAI,IAAI,IAAI,MAAM,EAAE,CAAC;AAC5E,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { SourceRange } from "./ast/types.js";
|
|
2
|
+
|
|
3
|
+
export interface Diagnostic {
|
|
4
|
+
message: string;
|
|
5
|
+
range: SourceRange;
|
|
6
|
+
severity: "error" | "warning";
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class CobolxError extends Error {
|
|
10
|
+
constructor(public readonly diagnostic: Diagnostic) {
|
|
11
|
+
super(diagnostic.message);
|
|
12
|
+
this.name = "CobolxError";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function formatDiagnostic(filePath: string, diagnostic: Diagnostic): string {
|
|
17
|
+
const { line, column } = diagnostic.range.start;
|
|
18
|
+
const label = diagnostic.severity === "warning" ? "Warning" : "Error";
|
|
19
|
+
return `${label}: ${diagnostic.message} at ${filePath}:${line}:${column}`;
|
|
20
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ProgramNode } from "../ast/types.js";
|
|
2
|
+
import type { Diagnostic } from "../diagnostics.js";
|
|
3
|
+
import type { HIRProgram } from "./types.js";
|
|
4
|
+
export declare function lowerToHIR(program: ProgramNode): {
|
|
5
|
+
hir: HIRProgram;
|
|
6
|
+
diagnostics: Diagnostic[];
|
|
7
|
+
};
|