|
1 | | -import { compile } from "./compiler"; |
| 1 | +import { type CompilerError, compile } from "./compiler"; |
2 | 2 | import { loadWasm } from "./compiler/exports"; |
3 | | -import { tokenize } from "./lexer"; |
4 | | -import { parse } from "./parser"; |
| 3 | +import { type TokenError, tokenize } from "./lexer"; |
| 4 | +import { type ParseError, parse } from "./parser"; |
5 | 5 |
|
6 | | -export type { CompilerErrorType } from "./compiler"; |
7 | | -export { compile } from "./compiler"; |
8 | | -export type { RunFunction } from "./compiler/exports"; |
9 | | -export { loadWasm } from "./compiler/exports"; |
10 | | -export type { TokenErrorType } from "./lexer"; |
11 | | -export { tokenize } from "./lexer"; |
12 | | -export type { AST, ParseErrorType } from "./parser"; |
13 | | -export { parse } from "./parser"; |
| 6 | +export { CompilerError, compile } from "./compiler"; |
| 7 | +export { loadWasm, type RunFunction } from "./compiler/exports"; |
| 8 | +export { TokenError, tokenize } from "./lexer"; |
| 9 | +export { type AST, ParseError, parse } from "./parser"; |
14 | 10 | export * from "./syntax"; |
15 | 11 | export type { Token } from "./tokens"; |
16 | 12 |
|
| 13 | +export type CompileFromSourceResult = |
| 14 | + | { errorType: null; error: null; bytes: Uint8Array; strings: Uint8Array } |
| 15 | + | { errorType: "TokenError"; error: TokenError; bytes: null; strings: null } |
| 16 | + | { errorType: "ParseError"; error: ParseError; bytes: null; strings: null } |
| 17 | + | { |
| 18 | + errorType: "CompilerError"; |
| 19 | + error: CompilerError; |
| 20 | + bytes: null; |
| 21 | + strings: null; |
| 22 | + }; |
| 23 | + |
17 | 24 | /** |
18 | 25 | * Compiles Pinky source code from a string. |
19 | 26 | * This function tokenizes the source code, parses the tokens into an AST, |
20 | 27 | * and then compiles the AST into WebAssembly binary. |
21 | 28 | * |
22 | 29 | * ``` |
23 | 30 | * const source = "x := 42 print(x)"; |
24 | | - * const wasmBytes = compileFromSource(source); |
| 31 | + * const { bytes } = compileFromSource(source); |
25 | 32 | * ``` |
26 | 33 | */ |
27 | | -export const compileFromSource = (source: string) => { |
28 | | - const { tokens } = tokenize(source); |
29 | | - const { ast } = parse(tokens); |
30 | | - return compile(ast); |
| 34 | +export const compileFromSource = (source: string): CompileFromSourceResult => { |
| 35 | + const { tokens, error: tokenError } = tokenize(source); |
| 36 | + if (tokenError) { |
| 37 | + return { |
| 38 | + bytes: null, |
| 39 | + strings: null, |
| 40 | + errorType: "TokenError", |
| 41 | + error: tokenError, |
| 42 | + }; |
| 43 | + } |
| 44 | + |
| 45 | + const { ast, error: parseError } = parse(tokens); |
| 46 | + if (parseError) { |
| 47 | + return { |
| 48 | + bytes: null, |
| 49 | + strings: null, |
| 50 | + errorType: "ParseError", |
| 51 | + error: parseError, |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + const { bytes, meta, error: compilerError } = compile(ast); |
| 56 | + if (compilerError) { |
| 57 | + return { |
| 58 | + bytes: null, |
| 59 | + strings: null, |
| 60 | + errorType: "CompilerError", |
| 61 | + error: compilerError, |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + return { |
| 66 | + bytes, |
| 67 | + strings: meta?.strings ?? null, |
| 68 | + errorType: null, |
| 69 | + error: compilerError, |
| 70 | + }; |
31 | 71 | }; |
32 | 72 |
|
33 | 73 | /** |
|
0 commit comments