|
| 1 | +# Pinky Compiler |
| 2 | + |
| 3 | +A fast, embeddable compiler for the Pinky scripting language. |
| 4 | +Compile Pinky code to WebAssembly and run it anywhere JavaScript runs. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | +- Compile Pinky source to WASM bytes |
| 11 | +- Run Pinky code in the browser or Node.js |
| 12 | +- TypeScript-first API |
| 13 | +- Tiny, dependency-free runtime |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## Install |
| 18 | + |
| 19 | +```sh |
| 20 | +npm install pinky-compiler |
| 21 | +``` |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +```ts |
| 28 | +import { compileFromSource, init } from "pinky-compiler"; |
| 29 | + |
| 30 | +// 1. Compile Pinky source to WASM bytes |
| 31 | +const source = ` |
| 32 | + x := 5 |
| 33 | + println "Hello, Pinky!" |
| 34 | + println x + 10 |
| 35 | +`; |
| 36 | +const { bytes, error } = compileFromSource(source); |
| 37 | + |
| 38 | +if (error) throw error; |
| 39 | + |
| 40 | +// 2. Initialize the WASM runtime (once per app) |
| 41 | +const { run } = await init(); |
| 42 | + |
| 43 | +// 3. Run the compiled program |
| 44 | +const output = run(bytes); |
| 45 | +console.log(output.join("")); // Hello, Pinky!\n15\n |
| 46 | +``` |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +## API |
| 51 | + |
| 52 | +### `compileFromSource(source: string)` |
| 53 | + |
| 54 | +Tokenizes, parses, and compiles Pinky source code to WASM. |
| 55 | + |
| 56 | +- **Returns:** `{ bytes: Uint8Array, error, meta }` |
| 57 | + |
| 58 | +### `init()` |
| 59 | + |
| 60 | +Initializes the WASM runtime and returns an object with a `run` function. |
| 61 | + |
| 62 | +- **Returns:** `Promise<{ run: (bytes: Uint8Array) => string[] }>` |
| 63 | + |
| 64 | +### `run(bytes: Uint8Array)` |
| 65 | + |
| 66 | +Runs the compiled WASM program and returns output as an array of strings. |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +## Advanced Usage |
| 71 | + |
| 72 | +You can also use the lower-level building blocks: |
| 73 | + |
| 74 | +```ts |
| 75 | +import { tokenize, parse, compile, init } from "pinky-compiler"; |
| 76 | + |
| 77 | +const { tokens } = tokenize('println "hi"'); |
| 78 | +const { ast } = parse(tokens); |
| 79 | +const { bytes } = compile(ast); |
| 80 | +const { run } = await init(); |
| 81 | +run(bytes); |
| 82 | +``` |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +## Types |
| 87 | + |
| 88 | +All major types are exported: |
| 89 | + |
| 90 | +```ts |
| 91 | +import type { AST, Token, CompilerErrorType, ParseErrorType, TokenErrorType } from "pinky-compiler"; |
| 92 | +``` |
0 commit comments