🦆Part 5 — Looking Forward
What Phase 1 has
- 75 tests passing on both backends
- Structs, struct params, arrays of structs, local structs, nested structs
- Array parameters of any element type (INT[], FLOAT[], CHAR[], STRUCT[])
- Fixed-point Q16:16 arithmetic on both backends
- SERIALIZE / DESERIALIZE with locked byte layout
- File I/O (open, read, write, seek, tell, size, delete, flush, close)
- Keyboard input (INKEY$, INPUT$(n))
- SELECT CASE, FOR, WHILE, IF / ELSEIF / ELSE
- String concatenation in PRINT
- Hex / binary literals (
0x,0b,&H,&B)
What Phase 2 will add
RND/RANDOMIZE— trivial to implement (LCG on NASM,rand()on C). Already planned.term.dkm— ANSI escape sequence parser in the standard library, not the compiler.resolve_namerefactor — preventive cleanup, not blocking.- FFI — for graphics, sound, networking, GUI/RAD. This is where the “gun on the platform’s shoulder” architecture becomes a feature rather than an implementation detail.
- OO — classes, methods, inheritance. Phase 2 language design.
duckdb3.dkb— the milestone proof-of-concept: a DuckDB-flavored table engine with indexes, queries, and file I/O.- Fullscreen editor — the “does this actually work?” test.
The architectural philosophy
Three principles emerged from building duckBASIC:
1. Platform where possible, own where necessary.
The C backend uses rand(), strcpy(), memcpy(), printf() — all platform-provided. The NASM backend implements fixed_mul (60 lines of asm), _read_str (BIOS INT 21h, AH=0Ah), _print_int (custom decimal printer) — all hand-written, because the platform (bare 16-bit DOS) has nothing.
The “gun on the platform’s shoulder” philosophy is about not reimplementing what you can depend on, and owning what you can’t. Every function in runtime_c.c is a thin wrapper over libc; every function in runtime_com.inc is a hand-written implementation. The distinction is principled, not accidental.
2. Byte layout is a contract.
SERIALIZE / DESERIALIZE works because the byte layout is documented, locked, and tested. It’s the reason a .dkb program can serialize on C and deserialize on NASM. It’s also the reason C’s int (4 bytes) vs NASM’s int (2 bytes) was caught early — as a design decision, not a bug.
3. Bug classes need bug-class fixes, but not always immediately.
The three-path name lookup is a bug class. The right answer is resolve_name. The right timing is when you have a failing test. duckBASIC got both right: it fixed the symptom narrow (with tests 74–78 as proof), and deferred the refactor with a clear trigger condition. That’s what a mature codebase does.
Conclusion
duckBASIC is not a production compiler. It’s not going to replace GCC or LLVM, and it’s not going to be used to write the next PostgreSQL. What it is, though, is a complete implementation of a compiler pipeline with two real backends, a real test suite, and enough features to build real programs (Phase 2’s fullscreen editor is the proof point).
The project demonstrates several things that are worth writing down:
- Two backends is better than one. The C backend made iteration fast; the NASM backend made every bug real.
- Fixed-point math on 8086 is a solved problem if you’re willing to write 200 lines of assembly.
- Byte layout is the hardest part of any serialization feature, and the easiest way to get it wrong is to trust that the platforms agree.
- Cross-backend test suites catch things that single-backend test suites miss. Every bug I’ve described in this series was caught by a cross-backend test.
- Refactoring is a response to pain, not a preventive measure. The right time to unify three name-lookup paths is when a test fails because they diverge — not before.
If you’re building your own compiler, I hope this series gives you some concrete patterns to steal. The test harness (dual backends, DOSBox verification) is the single most valuable design decision in the entire project, because it turns every bug into a reproducible failure rather than a suspected one. That’s worth more than any amount of clever parser design.
The complete Source code will be available to those who are interested
//factorial recursion in DuckBASIC.
FUNCTION factorial(n AS INT) AS INT
{
IF n <= 1
{
RETURN 1
}
RETURN n * factorial(n - 1)
}
PRINT factorial(5)
The bellow image is the look of duckedit, the editor written in DuckBASIC
