@ball-lang/encoder 0.1.0 → 1.3.5
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/README.md +47 -0
- package/dist/encoder.d.ts +19 -2
- package/dist/encoder.d.ts.map +1 -1
- package/dist/encoder.js +340 -87
- package/dist/encoder.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/encoder.ts +357 -92
- package/src/index.ts +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
[](https://www.npmjs.com/package/@ball-lang/encoder)
|
|
2
|
+
|
|
3
|
+
# @ball-lang/encoder
|
|
4
|
+
|
|
5
|
+
Encodes **TypeScript** source into a [Ball](https://github.com/ball-lang/ball) `Program` (the reverse of `@ball-lang/compiler`). Built on the TypeScript Compiler API; every construct — operators, control flow, closures, try/catch, string/list ops — routes through the universal `std` module (there is no `ts_std`). The Dart encoder (`dart/encoder/`, built on the `analyzer` package) is the canonical reference implementation.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @ball-lang/encoder
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { encode } from '@ball-lang/encoder';
|
|
17
|
+
|
|
18
|
+
const program = encode(`
|
|
19
|
+
function main(): void {
|
|
20
|
+
console.log("Hello, World!");
|
|
21
|
+
}
|
|
22
|
+
`);
|
|
23
|
+
|
|
24
|
+
// `program` is a Ball Program object (proto3-JSON-shaped) you can
|
|
25
|
+
// serialize, run through @ball-lang/engine, or compile back to a
|
|
26
|
+
// target language.
|
|
27
|
+
console.log(JSON.stringify(program, null, 2));
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## API
|
|
31
|
+
|
|
32
|
+
- `encode(source: string, options?: EncodeOptions): Program` — encode TypeScript source to a Ball `Program`. With `{ strict: true }` it throws an `EncodeError` on any unhandled construct.
|
|
33
|
+
- `encodeWithWarnings(source, options?): EncodeResult` — same as `encode`, but returns `{ program, warnings }` so you can inspect non-fatal warnings (e.g. unhandled statement/expression kinds).
|
|
34
|
+
- `TsEncoder` — the underlying class.
|
|
35
|
+
- `EncodeError` — thrown in strict mode; carries the accumulated `warnings`.
|
|
36
|
+
|
|
37
|
+
`EncodeOptions`:
|
|
38
|
+
|
|
39
|
+
| Option | Type | Description |
|
|
40
|
+
|--------|------|-------------|
|
|
41
|
+
| `moduleName` | `string` | Name of the generated module. Default `"main"`. |
|
|
42
|
+
| `entryFunction` | `string` | Entry function name. Default `"main"`. |
|
|
43
|
+
| `strict` | `boolean` | Throw `EncodeError` on any unhandled construct. Default `false`. |
|
|
44
|
+
|
|
45
|
+
## License
|
|
46
|
+
|
|
47
|
+
MIT
|
package/dist/encoder.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export interface EncodeOptions {
|
|
|
7
7
|
export declare class TsEncoder {
|
|
8
8
|
private stdFunctions;
|
|
9
9
|
private warnings;
|
|
10
|
+
private strict;
|
|
10
11
|
encode(source: string, options?: EncodeOptions): Program;
|
|
11
12
|
private encodeFunction;
|
|
12
13
|
private encodeBody;
|
|
@@ -14,7 +15,8 @@ export declare class TsEncoder {
|
|
|
14
15
|
private encodeStatement;
|
|
15
16
|
private encodeExpr;
|
|
16
17
|
private encodeBinary;
|
|
17
|
-
private
|
|
18
|
+
private isProvablyString;
|
|
19
|
+
private isStringExpr;
|
|
18
20
|
private encodePrefixUnary;
|
|
19
21
|
private encodePostfixUnary;
|
|
20
22
|
private encodeCall;
|
|
@@ -33,10 +35,25 @@ export declare class TsEncoder {
|
|
|
33
35
|
private encodeTypeAlias;
|
|
34
36
|
private encodeEnum;
|
|
35
37
|
private encodeTaggedTemplate;
|
|
38
|
+
/**
|
|
39
|
+
* Map a JS/TS method name to its Ball std function equivalent.
|
|
40
|
+
* Returns null if no mapping exists (falls through to generic method call).
|
|
41
|
+
*/
|
|
42
|
+
private mapMethodToStd;
|
|
36
43
|
private stdCall;
|
|
37
|
-
private
|
|
44
|
+
private buildBaseModules;
|
|
45
|
+
private nullLiteral;
|
|
38
46
|
private warn;
|
|
39
47
|
getWarnings(): string[];
|
|
40
48
|
}
|
|
49
|
+
export declare class EncodeError extends Error {
|
|
50
|
+
readonly warnings: string[];
|
|
51
|
+
constructor(message: string, warnings: string[]);
|
|
52
|
+
}
|
|
53
|
+
export interface EncodeResult {
|
|
54
|
+
program: Program;
|
|
55
|
+
warnings: string[];
|
|
56
|
+
}
|
|
41
57
|
export declare function encode(source: string, options?: EncodeOptions): Program;
|
|
58
|
+
export declare function encodeWithWarnings(source: string, options?: EncodeOptions): EncodeResult;
|
|
42
59
|
//# sourceMappingURL=encoder.d.ts.map
|
package/dist/encoder.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encoder.d.ts","sourceRoot":"","sources":["../src/encoder.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,OAAO,EAGR,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,aAAa;IAC5B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AA2CD,qBAAa,SAAS;IACpB,OAAO,CAAC,YAAY,CAAqB;IACzC,OAAO,CAAC,QAAQ,CAAgB;
|
|
1
|
+
{"version":3,"file":"encoder.d.ts","sourceRoot":"","sources":["../src/encoder.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,OAAO,EAGR,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,aAAa;IAC5B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AA2CD,qBAAa,SAAS;I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