@ball-lang/compiler 0.1.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.
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * ball-ts-compile — Ball (binary .ball.pb or proto3 JSON) → TypeScript.
4
+ *
5
+ * Usage:
6
+ * ball-ts-compile <program.ball.json|program.ball.pb> [--out <path>] [--no-preamble]
7
+ *
8
+ * If --out is omitted, writes to stdout.
9
+ * If the input file doesn't match either extension, JSON is assumed.
10
+ */
11
+ import { readFileSync, writeFileSync } from "node:fs";
12
+ import { extname } from "node:path";
13
+ import { compile } from "../dist/index.js";
14
+
15
+ function die(msg) {
16
+ process.stderr.write(`ball-ts-compile: ${msg}\n`);
17
+ process.exit(1);
18
+ }
19
+
20
+ function parseArgs(argv) {
21
+ const out = { input: null, output: null, includePreamble: true };
22
+ for (let i = 0; i < argv.length; i++) {
23
+ const a = argv[i];
24
+ if (a === "--out" && i + 1 < argv.length) { out.output = argv[++i]; }
25
+ else if (a === "--no-preamble") { out.includePreamble = false; }
26
+ else if (a === "-h" || a === "--help") {
27
+ process.stdout.write(
28
+ "Usage: ball-ts-compile <program.ball.json|program.ball.pb> [--out <path>] [--no-preamble]\n"
29
+ );
30
+ process.exit(0);
31
+ }
32
+ else if (!out.input) { out.input = a; }
33
+ else { die(`unexpected argument: ${a}`); }
34
+ }
35
+ if (!out.input) die("missing input file");
36
+ return out;
37
+ }
38
+
39
+ const args = parseArgs(process.argv.slice(2));
40
+ const ext = extname(args.input).toLowerCase();
41
+
42
+ let program;
43
+ if (ext === ".pb") {
44
+ die("binary .ball.pb not yet supported in @ball-lang/compiler (use .ball.json)");
45
+ } else {
46
+ const text = readFileSync(args.input, "utf8");
47
+ try { program = JSON.parse(text); }
48
+ catch (e) { die(`invalid JSON: ${e.message}`); }
49
+ }
50
+
51
+ const ts = compile(program, { includePreamble: args.includePreamble });
52
+ if (args.output) {
53
+ writeFileSync(args.output, ts);
54
+ } else {
55
+ process.stdout.write(ts);
56
+ }
@@ -0,0 +1,70 @@
1
+ import type { Program } from "./types.ts";
2
+ export interface CompileOptions {
3
+ /** Include the runtime preamble at the top of the output. Default true. */
4
+ includePreamble?: boolean;
5
+ /** Output file path hint (affects ts-morph's internal resolution). */
6
+ fileName?: string;
7
+ }
8
+ export declare class BallCompiler {
9
+ private readonly program;
10
+ /** Buffer that _emit* functions write into. */
11
+ private out;
12
+ private depth;
13
+ /** Catch-bound variables currently in scope — subject to bracket access. */
14
+ private readonly catchVars;
15
+ /** Fields of the class currently being emitted (method bodies). */
16
+ private currentClassFields;
17
+ /** Parameters of the currently-emitting method (shadow fields). */
18
+ private currentMethodParams;
19
+ /** Short method names of the current class (for `this.foo()` routing). */
20
+ private currentClassMethodNames;
21
+ /** All function names in the entry module (function vs ctor routing). */
22
+ private allFunctionNames;
23
+ /** typeDefs in the entry module (typeName → definition). */
24
+ private typeDefByName;
25
+ constructor(program: Program);
26
+ /** Compile to TS source. */
27
+ compile(options?: CompileOptions): string;
28
+ private emitFreeFunction;
29
+ private emitClass;
30
+ private buildCtor;
31
+ private buildMethod;
32
+ private buildGetter;
33
+ private buildSetter;
34
+ private captureInto;
35
+ private writeln;
36
+ private get ind();
37
+ private withMethodContext;
38
+ private emitStatementOrExpression;
39
+ private emitBlock;
40
+ private emitStatement;
41
+ private isControlFlow;
42
+ private emitControlFlowStatement;
43
+ private emitIfStmt;
44
+ private emitForStmt;
45
+ private emitForInStmt;
46
+ private emitWhileStmt;
47
+ private emitDoWhileStmt;
48
+ private emitTryStmt;
49
+ private emitAssignStmt;
50
+ private typedCatchCondition;
51
+ private expr;
52
+ private compileLiteral;
53
+ private compileFieldAccess;
54
+ private compileMessageCreation;
55
+ private extractPositionalAndNamed;
56
+ private typeIsUserDefinedClass;
57
+ private compileBlockExpression;
58
+ private compileLambda;
59
+ private compileCall;
60
+ private compileStdCall;
61
+ private emitIsCheck;
62
+ private wrapIfNeeded;
63
+ private compileSwitchExpr;
64
+ private compileThrowValue;
65
+ private enclosingTypeName;
66
+ private dartTypeToTs;
67
+ }
68
+ /** Convenience: compile a Program directly. */
69
+ export declare function compile(program: Program, options?: CompileOptions): string;
70
+ //# sourceMappingURL=compiler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compiler.d.ts","sourceRoot":"","sources":["../src/compiler.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAOV,OAAO,EAIR,MAAM,YAAY,CAAC;AAGpB,MAAM,WAAW,cAAc;IAC7B,2EAA2E;IAC3E,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,sEAAsE;IACtE,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAQD,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAU;IAElC,+CAA+C;IAC/C,OAAO,CAAC,GAAG,CAAM;IACjB,OAAO,CAAC,KAAK,CAAK;IAElB,4EAA4E;IAC5E,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAqB;IAE/C,mEAAmE;IACnE,OAAO,CAAC,kBAAkB,CAA0B;IAEpD,mEAAmE;IACnE,OAAO,CAAC,mBAAmB,CAA0B;IAErD,0EAA0E;IAC1E,OAAO,CAAC,uBAAuB,CAA0B;IAEzD,yEAAyE;IACzE,OAAO,CAAC,gBAAgB,CAA0B;IAElD,4DAA4D;IAC5D,OAAO,CAAC,aAAa,CAA0C;gBAEnD,OAAO,EAAE,OAAO;IAI5B,4BAA4B;IAC5B,OAAO,CAAC,OAAO,GAAE,cAAmB,GAAG,MAAM;IAyF7C,OAAO,CAAC,gBAAgB;IAwBxB,OAAO,CAAC,SAAS;IAqJjB,OAAO,CAAC,SAAS;IAsDjB,OAAO,CAAC,WAAW;IAwBnB,OAAO,CAAC,WAAW;IAiBnB,OAAO,CAAC,WAAW;IAoBnB,OAAO,CAAC,WAAW;IAcnB,OAAO,CAAC,OAAO;IAIf,OAAO,KAAK,GAAG,GAEd;IAED,OAAO,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