Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .claude/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Before considering any feature complete, run the full self-hosting chain:
2. `npm run verify:quick` — same but skips Stage 2 (day-to-day dev)

Or manually:
1. `npm test` — all tests pass (auto-uses native compiler if `.build/chadc` exists)
1. `npm test` — all tests pass (auto-uses native compiler if `.build/chad` exists)
2. `bash scripts/self-hosting.sh` — full 3-stage self-hosting
3. `bash scripts/self-hosting.sh --quick` — skip Stage 2

Expand Down Expand Up @@ -82,7 +82,7 @@ Run tests: `npm test` or `npm run test:full` (via `node scripts/test.js`)
Run tests + self-hosting: `npm run verify` (or `npm run verify:quick` to skip Stage 2)
Build: `npm run build` (TypeScript → dist/)

Tests auto-detect `.build/chadc` and use it instead of `node dist/chadc-node.js` (~10x faster per compile).
Tests auto-detect `.build/chad` and use it instead of `node dist/chad-node.js` (~10x faster per compile).
`compiler.test.ts` runs at concurrency 32; `smoke.test.ts` at concurrency 8.

## Useful Patterns
Expand Down
97 changes: 16 additions & 81 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,18 @@ jobs:
clang -c -O2 -fPIC -I $TS_SRC -I $TS_INCLUDE $TS_SRC/scanner.c -o build/tree-sitter-typescript-scanner.o
clang -c -O2 -fPIC -I vendor/tree-sitter/lib/include c_bridges/treesitter-bridge.c -o build/treesitter-bridge.o

- name: Build native compiler for tests
run: node dist/chadc-node.js src/chadc-native.ts -o .build/chadc

- name: Smoke test native compiler
run: |
.build/chadc examples/hello.ts -o /tmp/hello
/tmp/hello

- name: Run tests
run: npm test

- name: Rebuild native chadc (static release)
run: node dist/chadc-node.js src/chadc-native.ts -o .build/chadc --static --target-cpu=x86-64

- name: Build native chad (static release)
run: node dist/chadc-node.js src/chad-native.ts -o .build/chad --static --target-cpu=x86-64
- name: Build native chad (static)
run: node dist/chad-node.js build src/chad-native.ts -o .build/chad --static --target-cpu=x86-64

- name: Verify static binary
run: |
file .build/chad
ldd .build/chad 2>&1 && echo "WARNING: not fully static" || echo "OK: static binary"

- name: Smoke test native chadc
run: |
.build/chadc examples/hello.ts -o /tmp/hello
/tmp/hello

- name: Smoke test native chad
run: |
.build/chad build examples/hello.ts -o /tmp/hello2
/tmp/hello2
.build/chad build examples/hello.ts -o /tmp/hello
/tmp/hello

- name: Run self-hosting tests
run: node --import tsx --test tests/self-hosting.test.ts
Expand All @@ -102,7 +83,6 @@ jobs:
run: |
mkdir -p release/lib
cp .build/chad release/
cp .build/chadc release/
cp build/tree-sitter-typescript-parser.o release/lib/
cp build/tree-sitter-typescript-scanner.o release/lib/
cp build/treesitter-bridge.o release/lib/
Expand All @@ -113,7 +93,7 @@ jobs:
cp vendor/libwebsockets/build/lib/libwebsockets.a release/lib/
cp c_bridges/lws-bridge.o release/lib/
cp c_bridges/regex-bridge.o release/lib/
tar -czf chadscript-linux-musl-x64.tar.gz -C release chad chadc lib
tar -czf chadscript-linux-musl-x64.tar.gz -C release chad lib

- name: Upload artifact
uses: actions/upload-artifact@v4
Expand Down Expand Up @@ -180,32 +160,13 @@ jobs:
clang -c -O2 -fPIC -I $TS_SRC -I $TS_INCLUDE $TS_SRC/scanner.c -o build/tree-sitter-typescript-scanner.o
clang -c -O2 -fPIC -I vendor/tree-sitter/lib/include c_bridges/treesitter-bridge.c -o build/treesitter-bridge.o

- name: Build native compiler for tests
run: node dist/chadc-node.js src/chadc-native.ts -o .build/chadc

- name: Smoke test native compiler
run: |
.build/chadc examples/hello.ts -o /tmp/hello
/tmp/hello

- name: Run tests
run: npm test

- name: Rebuild native chadc (release)
run: node dist/chadc-node.js src/chadc-native.ts -o .build/chadc --target-cpu=x86-64

- name: Build native chad (release)
run: node dist/chadc-node.js src/chad-native.ts -o .build/chad --target-cpu=x86-64

- name: Smoke test native chadc
run: |
.build/chadc examples/hello.ts -o /tmp/hello
/tmp/hello
- name: Build native chad
run: node dist/chad-node.js build src/chad-native.ts -o .build/chad --target-cpu=x86-64

- name: Smoke test native chad
run: |
.build/chad build examples/hello.ts -o /tmp/hello2
/tmp/hello2
.build/chad build examples/hello.ts -o /tmp/hello
/tmp/hello

- name: Run self-hosting tests
run: node --import tsx --test tests/self-hosting.test.ts
Expand All @@ -215,7 +176,6 @@ jobs:
run: |
mkdir -p release/lib
cp .build/chad release/
cp .build/chadc release/
cp build/tree-sitter-typescript-parser.o release/lib/
cp build/tree-sitter-typescript-scanner.o release/lib/
cp build/treesitter-bridge.o release/lib/
Expand All @@ -226,7 +186,7 @@ jobs:
cp vendor/libwebsockets/build/lib/libwebsockets.a release/lib/
cp c_bridges/lws-bridge.o release/lib/
cp c_bridges/regex-bridge.o release/lib/
tar -czf chadscript-linux-x64.tar.gz -C release chad chadc lib
tar -czf chadscript-linux-x64.tar.gz -C release chad lib

- name: Upload artifact
uses: actions/upload-artifact@v4
Expand Down Expand Up @@ -284,40 +244,17 @@ jobs:
clang -c -O2 -fPIC -I $TS_SRC -I $TS_INCLUDE $TS_SRC/scanner.c -o build/tree-sitter-typescript-scanner.o
clang -c -O2 -fPIC -I vendor/tree-sitter/lib/include c_bridges/treesitter-bridge.c -o build/treesitter-bridge.o

- name: Build native compiler for tests
run: node dist/chadc-node.js src/chadc-native.ts -o .build/chadc

- name: Sign native compiler
run: codesign --sign - --force .build/chadc

- name: Smoke test native compiler
run: |
.build/chadc examples/hello.ts -o /tmp/hello
/tmp/hello

- name: Run tests
run: npm test

- name: Rebuild native chadc (release)
run: node dist/chadc-node.js src/chadc-native.ts -o .build/chadc

- name: Build native chad (release)
run: node dist/chadc-node.js src/chad-native.ts -o .build/chad
- name: Build native chad
run: node dist/chad-node.js build src/chad-native.ts -o .build/chad

- name: Sign macOS binaries
run: |
codesign --sign - --force .build/chad
codesign --sign - --force .build/chadc

- name: Smoke test native chadc
run: |
.build/chadc examples/hello.ts -o /tmp/hello
/tmp/hello

- name: Smoke test native chad
run: |
.build/chad build examples/hello.ts -o /tmp/hello2
/tmp/hello2
.build/chad build examples/hello.ts -o /tmp/hello
/tmp/hello

- name: Run self-hosting tests
run: node --import tsx --test tests/self-hosting.test.ts
Expand All @@ -327,7 +264,6 @@ jobs:
run: |
mkdir -p release/lib
cp .build/chad release/
cp .build/chadc release/
cp build/tree-sitter-typescript-parser.o release/lib/
cp build/tree-sitter-typescript-scanner.o release/lib/
cp build/treesitter-bridge.o release/lib/
Expand All @@ -338,7 +274,7 @@ jobs:
cp vendor/libwebsockets/build/lib/libwebsockets.a release/lib/
cp c_bridges/lws-bridge.o release/lib/
cp c_bridges/regex-bridge.o release/lib/
tar -czf chadscript-macos-arm64.tar.gz -C release chad chadc lib
tar -czf chadscript-macos-arm64.tar.gz -C release chad lib

- name: Upload artifact
uses: actions/upload-artifact@v4
Expand Down Expand Up @@ -383,7 +319,6 @@ jobs:
- name: Verify binary
run: |
file ~/.chadscript/chad.bin
file ~/.chadscript/chadc.bin
ls -la ~/.chadscript/lib/

- name: Smoke test - hello world
Expand All @@ -394,7 +329,7 @@ jobs:
- name: Smoke test - compile to binary
run: |
echo 'process.exit(42);' > /tmp/exit42.ts
chadc /tmp/exit42.ts -o /tmp/exit42
chad build /tmp/exit42.ts -o /tmp/exit42
/tmp/exit42 || [ $? -eq 42 ]

release:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/cross-compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:

- name: Cross-compile hello.ts for macOS arm64
run: |
node dist/chadc-node.js --keep-temps --target macos-arm64 --emit-llvm examples/hello.ts -o .build/hello-macos
node dist/chad-node.js ir --keep-temps --target macos-arm64 examples/hello.ts -o .build/hello-macos
llc -mtriple=aarch64-apple-darwin -filetype=obj .build/hello-macos.ll -o .build/hello-macos.o

- name: Verify object file format
Expand Down Expand Up @@ -87,7 +87,7 @@ jobs:

- name: Cross-compile hello.ts for Linux x64
run: |
node dist/chadc-node.js --keep-temps --target linux-x64 --emit-llvm examples/hello.ts -o .build/hello-linux
node dist/chad-node.js ir --keep-temps --target linux-x64 examples/hello.ts -o .build/hello-linux
llc -mtriple=x86_64-unknown-linux-gnu -filetype=obj .build/hello-linux.ll -o .build/hello-linux.o

- name: Verify object file format
Expand Down
6 changes: 3 additions & 3 deletions BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ npm run verify:quick
# Full: run tests + self-hosting Stage 0-1-2 in parallel
npm run verify

# Tests only (uses native compiler if .build/chadc exists)
# Tests only (uses native compiler if .build/chad exists)
npm test

# Self-hosting only
Expand All @@ -76,9 +76,9 @@ export CHADSCRIPT_TREESITTER_PATH=/path/to/tree-sitter
ChadScript can compile its own compiler to a native binary:

```bash
chadc src/chadc-native.ts -o /tmp/chad-stage0
chad build src/chad-native.ts -o /tmp/chad-stage0

/tmp/chad-stage0 src/chadc-native.ts -o /tmp/chad-stage1
/tmp/chad-stage0 build src/chad-native.ts -o /tmp/chad-stage1
```

The Stage 1 binary is a standalone native compiler that needs no Node.js runtime.
26 changes: 13 additions & 13 deletions benchmarks/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -e

DIR="$(cd "$(dirname "$0")" && pwd)"
REPO="$(dirname "$DIR")"
CHAD="$REPO/.build/chadc"
CHAD="$REPO/.build/chad"
STARTUP_RUNS=50
HTTP_BENCH="$DIR/tools/httpbench"
WS_BENCH="$DIR/tools/wsbench"
Expand Down Expand Up @@ -303,40 +303,40 @@ echo ""

echo "--- Building ---"

$CHAD "$DIR/startup/chadscript.ts" -o /tmp/bench-startup-chad
$CHAD build "$DIR/startup/chadscript.ts" -o /tmp/bench-startup-chad
echo " ChadScript startup built"

$CHAD "$DIR/sqlite/chadscript.ts" -o /tmp/bench-sqlite-chad
$CHAD build "$DIR/sqlite/chadscript.ts" -o /tmp/bench-sqlite-chad
echo " ChadScript SQLite built"

$CHAD "$DIR/matmul/chadscript.ts" -o /tmp/bench-matmul-chad
$CHAD build "$DIR/matmul/chadscript.ts" -o /tmp/bench-matmul-chad
echo " ChadScript Matmul built"

$CHAD "$DIR/montecarlo/chadscript.ts" -o /tmp/bench-montecarlo-chad
$CHAD build "$DIR/montecarlo/chadscript.ts" -o /tmp/bench-montecarlo-chad
echo " ChadScript Monte Carlo built"

$CHAD "$DIR/fibonacci/chadscript.ts" -o /tmp/bench-fibonacci-chad
$CHAD build "$DIR/fibonacci/chadscript.ts" -o /tmp/bench-fibonacci-chad
echo " ChadScript Fibonacci built"

$CHAD "$DIR/sieve/chadscript.ts" -o /tmp/bench-sieve-chad
$CHAD build "$DIR/sieve/chadscript.ts" -o /tmp/bench-sieve-chad
echo " ChadScript Sieve built"

$CHAD "$DIR/sorting/chadscript.ts" -o /tmp/bench-sorting-chad
$CHAD build "$DIR/sorting/chadscript.ts" -o /tmp/bench-sorting-chad
echo " ChadScript Sorting built"

$CHAD "$DIR/nbody/chadscript.ts" -o /tmp/bench-nbody-chad
$CHAD build "$DIR/nbody/chadscript.ts" -o /tmp/bench-nbody-chad
echo " ChadScript N-Body built"

$CHAD "$DIR/stringops/chadscript.ts" -o /tmp/bench-stringops-chad
$CHAD build "$DIR/stringops/chadscript.ts" -o /tmp/bench-stringops-chad
echo " ChadScript String Ops built"

$CHAD "$DIR/fileio/chadscript.ts" -o /tmp/bench-fileio-chad
$CHAD build "$DIR/fileio/chadscript.ts" -o /tmp/bench-fileio-chad
echo " ChadScript File I/O built"

$CHAD "$DIR/binarytrees/chadscript.ts" -o /tmp/bench-binarytrees-chad
$CHAD build "$DIR/binarytrees/chadscript.ts" -o /tmp/bench-binarytrees-chad
echo " ChadScript Binary Trees built"

$CHAD "$DIR/json/chadscript.ts" -o /tmp/bench-json-chad
$CHAD build "$DIR/json/chadscript.ts" -o /tmp/bench-json-chad
echo " ChadScript JSON built"

$CHAD "$DIR/stringsearch/chadscript.ts" -o /tmp/bench-stringsearch-chad
Expand Down
11 changes: 0 additions & 11 deletions docs/getting-started/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,3 @@ chad clean
| `--keep-temps` | Keep intermediate files (`.ll`, `.o`) |
| `-fsanitize=address` | Build with AddressSanitizer (ASAN) |
| `--skip-semantic-analysis` | Skip semantic analysis |

## Direct Compiler

The bare compiler is also available as `chadc`:

```bash
chadc hello.ts # same as chad build hello.ts
chadc hello.ts -o myapp # same as chad build hello.ts -o myapp
```

`chadc` is the compiler binary itself. The `chad` wrapper adds convenience commands like `run`, `ir`, and `clean`.
7 changes: 4 additions & 3 deletions docs/language/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,14 @@ ChadScript is self-hosting — the compiler (~45k lines of TypeScript across ~70

```bash
# Stage 0: Node.js compiles the compiler to a native binary
chadc src/chadc-native.ts -o chad-stage0
# Stage 0: Node.js compiles the compiler to a native binary
chad build src/chad-native.ts -o chad-stage0

# Stage 1: The native binary compiles itself (no Node.js needed)
./chad-stage0 src/chadc-native.ts -o chad-stage1
./chad-stage0 build src/chad-native.ts -o chad-stage1

# Stage 2: Verify correctness (Stage 1 output == Stage 2 output)
./chad-stage1 src/chadc-native.ts -o chad-stage2
./chad-stage1 build src/chad-native.ts -o chad-stage2
```

The Stage 2 binary proves the compiler's output is correct enough to reproduce itself.
Expand Down
11 changes: 1 addition & 10 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ main() {
tar -xzf "$TMPDIR/$TARBALL" -C "$INSTALL_DIR"

mv "$INSTALL_DIR/chad" "$INSTALL_DIR/chad.bin"
mv "$INSTALL_DIR/chadc" "$INSTALL_DIR/chadc.bin"

cat > "$INSTALL_DIR/chad" << 'WRAPPER'
#!/bin/sh
Expand All @@ -95,19 +94,11 @@ exec "$DIR/chad.bin" "$@"
WRAPPER
chmod +x "$INSTALL_DIR/chad"

cat > "$INSTALL_DIR/chadc" << 'WRAPPER'
#!/bin/sh
DIR=$(cd "$(dirname "$(command -v "$0")")" && pwd)
exec "$DIR/chadc.bin" "$@"
WRAPPER
chmod +x "$INSTALL_DIR/chadc"

if [ "$OS_TAG" = "macos" ]; then
xattr -d com.apple.quarantine "$INSTALL_DIR/chad.bin" 2>/dev/null || true
xattr -d com.apple.quarantine "$INSTALL_DIR/chadc.bin" 2>/dev/null || true
fi

success "Installed chad and chadc"
success "Installed chad"

add_to_path

Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
"version": "0.1.0",
"description": "A JavaScript to native code compiler using LLVM",
"type": "module",
"main": "dist/chadc-node.js",
"main": "dist/chad-node.js",
"bin": {
"chad": "./dist/chad-node.js",
"chadc": "./dist/chadc-node.js"
"chad": "./dist/chad-node.js"
},
"scripts": {
"compile": "tsx src/chadc-node.ts",
"compile": "tsx src/chad-node.ts build",
"prebuild": "node scripts/embed-dts.js",
"build": "tsc",
"test": "node scripts/test.js",
Expand Down
Loading
Loading