
What toke 10 years from terry Davis it toke me agents and days
This repository contains two things that build on each other:
以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。
This repository contains two things that build on each other:
Two demonstrations carry the whole project. First, the ownership thesis — a use-after-move is a diagnostic, not a crash, and the session survives it:
hr> Str a = "who owns me?\n";
hr> Str b = a; // moves: a is invalidated AT RUNTIME
hr> Print(a);
runtime diagnostic: use of a moved value at repl:1:7
hr: statement aborted by a runtime diagnostic (session continues)
hr> Print(b); // the session survived — this is the whole point
who owns me?
Second, the self-hosting thesis. At the in-kernel prompt of the real OS:
Ed("/TOS/Files.HR"); // open the OS's own source EdReplaceLine("Str OsName", "Str OsName() { return "mine"; }"); EdSave(); // write it back #include "/TOS/Files.HR" // recompile, in this session Print(OsName(), "\n"); // -> mine
No reboot, no crash, HeapCheck() = 1 throughout. tools/kernel_ci.py runs exactly that sequence, in QEMU, on every CI run.
Windows (what this repository was developed and verified on). Open a terminal in the project folder — this is a source snapshot, not a git checkout, so there is nothing to clone:
1. host compiler + language tests (110 tests, ~4 min including a full QEMU boot)
powershell -NoProfile -ExecutionPolicy Bypass -File tools\cargo.ps1 test --release
2. try the language
.\target\release\hr.exe demos\tour.hr # guided tour of every language feature .\target\release\hr.exe # interactive shell
3. build and boot the operating system (needs QEMU)
powershell -NoProfile -ExecutionPolicy Bypass -File kernel\build.ps1 & "C:\Program Files\qemu\qemu-system-x86_64.exe" -kernel kernel\kernel.bin -m 256M -smp 1 -serial stdio
4. the full scripted QEMU session, all phase gates, prints KERNEL-CI: PASS
python tools\kernel_ci.py
Linux: identical, with cargo instead of tools/cargo.ps1, kernel/build.sh instead of kernel/build.ps1, and python3.
Use tools/cargo.ps1, not bare cargo, on Windows. It clears future-dated build artefacts and forces a real rebuild when it detects them. This is not paranoia: this machine's clock jumps backwards, which made cargo report Fresh hr-kernel forever while three genuinely different gfx.rs implementations produced byte-identical kernels (see D050).
Need Why Notes
x86-64 CPU The JIT emits x86-64 machine code 32-bit and ARM hosts are not supported
Rust (rustup, stable) The compiler is written in Rust rustup toolchain install stable
x86_64-unknown-none target Only for building the OS kernel rustup target add x86_64-unknown-none
QEMU Only for booting the OS qemu-system-x86_64; on Windows the default install path is detected automatically
Python 3 Only for tools/kernel_ci.py and tools/gen_font8x8.py Windows: python, not the Microsoft Store stub
~2 GB free disk Toolchain + build artefacts
No C compiler is required on Windows. The kernel is assembled and linked entirely by tools that ship with rustc (rust-lld, rustc's integrated assembler), so you do not need as, ld, objcopy, nasm, or Visual Studio's C++ workload — rustup's bundled rust-mingw provides the linker. On Linux the kernel uses the GNU toolchain path (kernel/build.sh), so gcc/binutils are needed there.
tools/setup_env.sh (Linux) checks your machine and reports exactly what is missing — it installs nothing and needs no root. tools/env.sh is sourced by the shell scripts and configures nothing unless the toolchain really lives under /opt. On Windows the equivalents are tools/cargo.ps1 and kernel/build.ps1.
1.1 The idea
Most languages check ownership before the program runs, or not at all. HolyRust checks it while it runs, in a live session where code exists the moment you type it. That inversion is the design thesis, argued in LANG_SPEC §1:
The whole language is defined in docs/LANG_SPEC.md. If a feature is not in that document, it does not exist.
.\target\release\hr.exe # interactive shell (REPL) .\target\release\hr.exe demos\tour.hr # compile and run a file
CLI reference
Command What it does
hr Interactive shell. Prompts hr>, continues on ... while braces are unbalanced, #quit/Ctrl-D to exit.
hr FILE.hr Compile and run a file. Diagnostics are positioned; exit code is non-zero if anything failed or trapped.
hr --dump-tokens FILE Print the token stream (line:col offset..end TOKEN).
hr --dump-ast FILE Print the AST in the stable snapshot format.
hr --dump-code FILE Compile and write the machine-code blob to a temp file, printing its base address and entry offset.
hr --bench Latency harness → the numbers in docs/BENCHMARKS.md.
REPL commands
Command Effect
#help Print the help text
#history Show this session's inputs
#reset Start a fresh session (new globals, functions, classes)
#quit / #exit / Ctrl-D Leave
Every session appends to session.log in the working directory: session headers, each input line, rendered diagnostics, aborts, and per-statement latency ([stmt N] X.XXXms) — the Phase 5 p95 gate reads that log. Guest output and value echoes go to stdout; the banner, prompts and diagnostics go to stderr (D036), so hr file.hr > out.txt captures only your program's output.
hr demos/tour.hr — the output below is the real thing, not an illustration:
$ hr demos/tour.hr Hello from a bare string literal. count=42 ratio=1.5 Digits()=12 Digits(,6)=16 Digits(9)=92 Sum(1,2,3,4)=10 zero small even-ish other acc=10 5!=120 point 3,4 caught: use of a moved value (moved or never initialized) b still works: owned via shared borrow: 5 caught: borrow conflict (rule: one mutable XOR many immutable) after unsafe write: 9 code generated at compile time -> Six()=6
Each section of demos/tour.hr is annotated with the LANG_SPEC section it demonstrates. The highlights:
No main(), and bare strings print (§4). Top-level statements are the program.
Default arguments at any position (§5). Omission uses an empty slot, and only parameters that have a default may be omitted:
I64 Digits(I64 a = 1, I64 b = 2) { return a * 10 + b; } Digits() // 12 Digits(,6) // 16 <- a takes its default Digits(9) // 92 <- b takes its default
Variadics (§5) expose implicit argc/argv:
I64 Sum(...) { I64 total = 0; for (I64 i = 0; i < argc; i++) { total += argv[i]; } return total; }
switch (§6) has no fallthrough (HolyC semantics) and accepts comma lists and inclusive ranges:
switch (v) { case 0: Print("zero\n"); case 1...5: Print("small\n"); case 6, 8, 10: Print("even-ish\n"); default: Print("other\n"); }
Runtime ownership (§7). Scalars are Copy; Str and class are Move:
Str a = "owned\n";
Str b = a; // a is invalidated NOW
try { Print(a); } catch { Print("caught: ", catch_msg, "\n"); }
Print("b still works: ", b); // the session survived
The runtime reports trap codes you can branch on: -10 use-after-move, -11 borrow conflict, -12 write through a shared borrow, -13 invalid pointer.
Borrows (§7/§8) enforce one-mutable-XOR-many-immutable at borrow creation and at writes. unsafe { } (§10) is the documented escape hatch:
I64 n = 5;
I64 *shared = &n; // shared borrow
I64 *excl = &mut n; // traps: borrow conflict while shared is live
unsafe { *shared = 9; } // no checks
Strictness is adjustable (§8). #![ownership(strict|loose|off)] as the first line of a file, or as a statement inside a block, dials move-tracking and borrow checks down for that unit. Default is strict.
#exe { } (§12) runs a block at compile time and splices what it prints back into the program as source text — code that writes code:
#exe { "I64 Six() { return 6; }\n"; } Print(Six(), "\n"); // 6
#include "path" splices another file's source into the unit being compiled. The path is looked up in the session's in-memory filesystem first (which is what lets the OS include its own source), then on disk. Nesting is capped at 8, and a missing file is a positioned C094 error, never a silent no-op.
source text │ ▼ hr_lexer tokens (positions on every token; never panics on garbage) │ ▼ hr_parser AST, error-recovering (one bad line does not hide the next) │ ▼ pipeline #include splice → #exe compile-time execution → re-parse │ ▼ hr_check types, ownership rules, builtin signatures → typed IR │ ▼ hr_codegen own x86-64 emitter (no LLVM) → code blob + relocations │ ▼ hr_runtime mmap/VirtualAlloc → patch → mprotect RX → call (runtime context lives in r15; arena; trap handling; shadow ownership state; the filesystem; the editor buffer) │ ▼ hr_shell the REPL: one Session backs the whole session, so globals, functions and classes persist across lines
Things worth knowing about the implementation:
Demo Shows
demos/tour.hr Every language feature, section by section
demos/hello.hr The minimum: printing and arithmetic
demos/fizzbuzz.hr Loops and conditionals
demos/ownership_demo.hr Move/borrow traps caught live by try/catch
demos/exe_block.hr #exe {} generating a function at compile time
demos/kernel_sim.hr Live hot-patching of a running "kernel" event loop
Part 2 — TempleOS-RS
2.1 The idea
This OS is written in itself. Edit any file, re-include it, the OS changes.
A single-tasking, 640×480×16 operating system. Rust owns exactly what must touch hardware; everything above that is guest code in /TOS/*.HR, compiled by the in-kernel JIT at boot, and editable while the machine runs.
powershell -NoProfile -ExecutionPolicy Bypass -File kernel\build.ps1 & "C:\Program Files\qemu\qemu-system-x86_64.exe" ` -kernel kernel\kernel.bin -m 256M -smp 1 -serial stdio
A QEMU window opens showing the real VGA output. Click into the window to give it keyboard and mouse focus (Ctrl+Alt+G releases the mouse). -serial stdio mirrors the console to your terminal, which is handy because the boot transcript there is what every diagnostic goes to.
Expected boot transcript (this is also how a failure is diagnosed — each of the OS's own source files is named as it is compiled). Real output, not an illustration:
S1:32bit-entry S1.5:paging-on S2:64bit-longmode S2.1:pre-bss-zero S2.2:pre-kmain S3:rust-kmain S4:hooks-set TOS: compiling /TOS/Files.HR TOS: compiling /TOS/Graphics.HR TOS: compiling /TOS/Sound.HR TOS: compiling /TOS/Oracle.HR TOS: compiling /TOS/Editor.HR TOS: compiling /TOS/Flight.HR TOS: compiling /TOS/Shell.HR TOS: /TOS loaded - this OS is written in itself HolyRust kernel shell — the compiler runs IN the kernel heap integrity: 1 [echo-v1] 104 [echo-v1] 105 pumped 2 boot keys mouse: 1 clock: 290ms date: 20261015 kernel: ready — type HolyRust (serial or PS/2 keyboard) 0 hr>
Boot takes 1.8–4.7 s under pure emulation (TCG) against the project's 5 s gate; most of it is the OS compiling its own seven source files with its own JIT. #quit at the prompt halts the machine.
Prefer it headless? That is exactly what the CI does — -display none plus monitor-driven keyboard and mouse, so no window is needed:
python tools\kernel_ci.py # full scripted session; prints KERNEL-CI: PASS
2.3 Using the shell
Anything you type is compiled and executed as you type it. A line that is exactly one bare word is treated as a command word and dispatched by /TOS/Shell.HR; everything else is HolyRust.
Anything with arguments is called as a HolyRust function:
Dir("/TOS/.HR"); // entries matching a glob (one '') Cat("/TOS/Doc/TempleOS-RS.txt"); // print a file FWrite("/Tmp/notes.txt", "hello"); // create or overwrite FRead("/Tmp/notes.txt"); // read it back FDel("/Tmp/notes.txt"); // delete it Count(); // how many entries
Because filesystem commands are just guest code, you can rewrite them live:
I64 Count() { return 4242; } // hot-patch the OS's own function Count(); // 4242
2.4 Flying
flight puts you over a field of wireframe pyramids, in the spirit of TempleOS's AfterEgypt. It is tos/Flight.HR — guest code, compiled at boot, using only the graphics builtins.
The horizon tilts as you bank and drops as you pitch — that is the entire illusion, and it is what the CI asserts on, in pixels. Fly it a frame at a time if you prefer:
FlightInit(); // enter graphics mode and reset FlightStep(); // one frame: read input, integrate, render FlightRun(
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。