@ball-lang/engine 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ # MIT License
2
+
3
+ Copyright (c) 2026 Ball Language Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,113 @@
1
+ [![npm version](https://img.shields.io/npm/v/@ball-lang/engine.svg)](https://www.npmjs.com/package/@ball-lang/engine)
2
+
3
+ # @ball-lang/engine
4
+
5
+ Tree-walking interpreter for the [Ball programming language](https://github.com/ball-lang/ball). Runs Ball programs directly from their proto3 JSON representation in Node.js and browsers -- no protobuf dependency required.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @ball-lang/engine
11
+ ```
12
+
13
+ ## Quick start
14
+
15
+ ```ts
16
+ import { BallEngine } from '@ball-lang/engine';
17
+
18
+ // A minimal Ball program that prints "Hello, World!"
19
+ const program = {
20
+ name: 'hello',
21
+ version: '1.0.0',
22
+ entryModule: 'main',
23
+ entryFunction: 'main',
24
+ modules: [
25
+ {
26
+ name: 'std',
27
+ functions: [
28
+ { name: 'print', isBase: true },
29
+ { name: 'add', isBase: true },
30
+ ],
31
+ },
32
+ {
33
+ name: 'main',
34
+ moduleImports: [{ name: 'std' }],
35
+ functions: [
36
+ {
37
+ name: 'main',
38
+ body: {
39
+ call: {
40
+ module: 'std',
41
+ function: 'print',
42
+ input: {
43
+ messageCreation: {
44
+ fields: [
45
+ {
46
+ name: 'value',
47
+ value: { literal: { stringValue: 'Hello, World!' } },
48
+ },
49
+ ],
50
+ },
51
+ },
52
+ },
53
+ },
54
+ },
55
+ ],
56
+ },
57
+ ],
58
+ };
59
+
60
+ const engine = new BallEngine(program);
61
+ engine.run();
62
+ console.log(engine.getOutput()); // ["Hello, World!"]
63
+ ```
64
+
65
+ You can also pass a JSON string instead of an object:
66
+
67
+ ```ts
68
+ import { readFileSync } from 'node:fs';
69
+
70
+ const json = readFileSync('my_program.ball.json', 'utf-8');
71
+ const engine = new BallEngine(json);
72
+ engine.run();
73
+ ```
74
+
75
+ ## API reference
76
+
77
+ ### `new BallEngine(program, options?)`
78
+
79
+ Creates an engine instance.
80
+
81
+ | Parameter | Type | Description |
82
+ |-----------|------|-------------|
83
+ | `program` | `object \| string` | A Ball program object or its JSON string representation. |
84
+ | `options.stdout` | `(msg: string) => void` | Callback for `std.print` output. Defaults to collecting into an internal array. |
85
+ | `options.stderr` | `(msg: string) => void` | Callback for error output. Defaults to no-op. |
86
+
87
+ ### `engine.run(): string[]`
88
+
89
+ Executes the program starting from the entry function. Returns the collected stdout output array.
90
+
91
+ ### `engine.getOutput(): string[]`
92
+
93
+ Returns the stdout output collected so far (same array returned by `run()`).
94
+
95
+ ## Supported standard library functions
96
+
97
+ The engine implements the Ball `std` module (~70 functions) covering arithmetic, comparison, logic, bitwise ops, string manipulation, math, control flow (`if`, `for`, `while`, `for_in`, `switch`, `try`), collections, and I/O. See the [Ball repository](https://github.com/ball-lang/ball) for the full specification.
98
+
99
+ ## Usage without a build step
100
+
101
+ If you are using Node.js >= 22.6.0, you can import the TypeScript source directly:
102
+
103
+ ```bash
104
+ node --experimental-strip-types your_script.ts
105
+ ```
106
+
107
+ ```ts
108
+ import { BallEngine } from '@ball-lang/engine/src/index.ts';
109
+ ```
110
+
111
+ ## License
112
+
113
+ MIT
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Ball TypeScript Engine — interprets Ball programs directly from JSON.
3
+ *
4
+ * Runs in Node.js and browsers. No protobuf dependency — works with
5
+ * proto3 JSON representation of Ball programs.
6
+ *
7
+ * Usage:
8
+ * import { BallEngine } from '@ball-lang/engine';
9
+ * const engine = new BallEngine(programJson, { stdout: console.log });
10
+ * engine.run();
11
+ */
12
+ interface Program {
13
+ name?: string;
14
+ version?: string;
15
+ modules: Module[];
16
+ entryModule: string;
17
+ entryFunction: string;
18
+ }
19
+ interface Module {
20
+ name: string;
21
+ functions: FunctionDef[];
22
+ moduleImports?: ModuleImport[];
23
+ }
24
+ interface ModuleImport {
25
+ name: string;
26
+ }
27
+ interface FunctionDef {
28
+ name: string;
29
+ isBase?: boolean;
30
+ body?: Expression;
31
+ outputType?: string;
32
+ metadata?: Record<string, any>;
33
+ }
34
+ interface Expression {
35
+ call?: FunctionCall;
36
+ literal?: Literal;
37
+ reference?: {
38
+ name: string;
39
+ };
40
+ fieldAccess?: {
41
+ object: Expression;
42
+ field: string;
43
+ };
44
+ messageCreation?: {
45
+ fields: FieldValuePair[];
46
+ };
47
+ block?: Block;
48
+ lambda?: Lambda;
49
+ }
50
+ interface FunctionCall {
51
+ module?: string;
52
+ function: string;
53
+ input?: Expression;
54
+ }
55
+ interface Literal {
56
+ intValue?: string | number;
57
+ doubleValue?: number;
58
+ stringValue?: string;
59
+ boolValue?: boolean;
60
+ listValue?: {
61
+ elements: Expression[];
62
+ };
63
+ }
64
+ interface FieldValuePair {
65
+ name: string;
66
+ value: Expression;
67
+ }
68
+ interface Block {
69
+ statements: Statement[];
70
+ result?: Expression;
71
+ }
72
+ interface Statement {
73
+ let?: {
74
+ name: string;
75
+ value: Expression;
76
+ metadata?: Record<string, any>;
77
+ };
78
+ expression?: Expression;
79
+ }
80
+ interface Lambda {
81
+ body: Expression;
82
+ metadata?: Record<string, any>;
83
+ }
84
+ export interface BallEngineOptions {
85
+ stdout?: (msg: string) => void;
86
+ stderr?: (msg: string) => void;
87
+ }
88
+ export declare class BallEngine {
89
+ private program;
90
+ private stdout;
91
+ private stderr;
92
+ private functions;
93
+ private currentModule;
94
+ private activeException;
95
+ private output;
96
+ constructor(program: Program | string, options?: BallEngineOptions);
97
+ private buildLookupTables;
98
+ run(): string[];
99
+ getOutput(): string[];
100
+ private evalExpr;
101
+ private evalCall;
102
+ private callFunction;
103
+ private evalLiteral;
104
+ private evalReference;
105
+ private evalFieldAccess;
106
+ private evalMessageCreation;
107
+ private evalBlock;
108
+ private evalLambda;
109
+ private lazyFields;
110
+ private lazyStringField;
111
+ private evalLazyIf;
112
+ private evalLazyWhile;
113
+ private evalLazyDoWhile;
114
+ private evalLazyFor;
115
+ private evalLazyForIn;
116
+ private evalLazySwitch;
117
+ private evalLazyTry;
118
+ private evalShortCircuitAnd;
119
+ private evalShortCircuitOr;
120
+ private evalReturn;
121
+ private evalAssign;
122
+ private applyCompoundOp;
123
+ private evalIncDec;
124
+ private evalLabeled;
125
+ private callBaseFunction;
126
+ private toBool;
127
+ private toNum;
128
+ private numOp;
129
+ private ballToString;
130
+ }
131
+ export {};
132
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAOH,UAAU,OAAO;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,EAAE,CA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