Commit Graph

47 Commits

Author SHA1 Message Date
dimgigov 9c6b516453 feat: restructure repo, borrow checker, expanded stdlib
- Reorganize repository to Rust-style layout:
  compiler/bootstrap/  compiler/selfhost/  compiler/tests/
  library/std/  library/runtime/  tests/  tools/
- Add buxs/ Windows-compatible project root
- Add borrow checker tests and implement:
  - Alias analysis (double mutable borrow detection)
  - Use-after-move detection for own T
- Expand standard library:
  - Std::Os: Args, Env, Cwd, Chdir
  - Std::Time: NowMs, NowUs, SleepMs
  - Std::Process: Run, Output
  - Std::Io: PrintInt64 (fixes 32-bit truncation bug)
- Add examples: os_time.bux, process.bux
- Fix PrintInt to use int64_t in C runtime
2026-06-05 20:08:17 +03:00
dimgigov a45a2ecd49 fix: escape sequences in strings, --version/--help, manifest parser, debug cleanup
- Lexer (Nim + Bux): resolve escape sequences (\n, \t, \r, etc) instead of
  keeping raw source text in token. This was the root cause of \n being
  double-escaped to \\n in generated C code.
- Manifest parser (src_bux/manifest.bux): use bux_strlen instead of broken
  String_SplitCount(s, "") hack for string length; fix quote stripping.
- CLI (src_bux/cli.bux): --version/--help now recognized alongside version/help.
- Remove ~30+ debug Print statements from cli, parser, lexer, sema, main.
- Clean up unneeded Print/PrintInt extern declarations.
- _selfhost/src/: synced from src_bux/.
2026-06-05 18:24:59 +03:00
dimgigov 3e46e67d48 fix: multi-arg calls, uninitialized symbols, param limit; add banner img; update docs
- Fix segfault from uninitialized Symbol structs in scope/hir_lower/sema
- Fix null ReadFile crash in Cli_MergeFileInto (missing stdlib files)
- Extend function params from 6 to 9 (bux_str_format needs 9 args)
- Add ExprList/HirArgList linked lists for >2 call arguments
- Add banner image to README
- Update docs: C backend is now primary (not QBE)
2026-06-05 15:21:15 +03:00
dimgigov 0c1f230286 selfhost: C backend becomes primary backend; QBE path removed from CLI
Major changes:
- Cli_BuildProject now uses CBackend_Generate instead of QBE SSA generation
- Replaced QBE invocation (vendor/qbe/qbe) with direct cc compilation
- Cli_Build (single-file) now compiles C output with cc including runtime.c/io.c
- Added import-based stdlib merging: only imported Std::* modules are merged,
  eliminating unused generic code from Array/Map/Set/Channel in simple programs

C backend fixes:
- Fixed struct typedef redefinition: struct definitions now use 'struct Name {}'
  instead of 'typedef struct {} Name' to avoid conflicting with forward typedefs
- Added enum support: HirEnum struct, enum emission in C backend, variant
  constants emitted as #define Name_Value
- Added char8 typedef
- Skip generic structs/functions (T/K/V type parameters) in C emission

Sema fixes:
- Fixed Sema_IsNumeric: now correctly recognizes uint, float32, float64
- Fixed return type check: allow pointer compatibility (String <-> *T)

Tested: hello, fibonacci, factorial, enums examples build and run via buxc2
2026-06-05 14:42:18 +03:00
dimgigov 5ae85d5bd9 Phase 2: Fix sema stub — add full function body type checking
- Iterate function body statements and call Sema_CheckStmt on each
- Add Sema_CheckBlock helper for child scope creation in if/while/for/loop blocks
- Extend Sema_CheckStmt to handle all statement kinds: skIf, skWhile, skDoWhile,
  skLoop, skFor, skMatch, skBreak, skContinue, skDecl, skReturn
- Extend Sema_CheckExpr with ekAssign, ekField, ekIndex, ekBlock, ekSelf
- Fix ekCall to resolve return type from function declaration
- Fix ekBinary to handle assignment operators (tkAssign, +=, -=, etc.)
- Add Sema_CollectGlobals handling for dkUse (imports), dkConst, dkEnum variants
- Add tkColonColon path parsing in parser (Color::Red)
- Add decl field to Symbol struct for function/type resolution
- Add Sema_ZeroInitSymbol helper to avoid garbage from uninitialized struct fields
- Fix concurrency.bux missing Task_Join import

All examples now pass buxc2 check. make selfhost succeeds.
2026-06-05 14:07:38 +03:00
dimgigov f0f94f30e1 fix(selfhost): generic struct compilation, explicit generic args, method calls, field access
- parser: add parserIsTypeArgListAhead() for Func<Type>(args) and Type<T> { ... }
- hir_lower: method call desugaring with typeName tracking, self keyword,
  ekField typeName propagation, variable type tracking, strip '*' suffix
  from pointer type names in method calls, fix externFuncs buffer overflow
  (64 -> 256 elements) that corrupted structCount
- qbe_backend: QBE_FindStruct, QBE_FieldOffset, QBE_TypeSize helpers;
  hStructInit field-by-field codegen; hFieldPtr with offset + auto-load;
  hAssign/hStore special case for hFieldPtr (generate pointer, not value);
  fix null as *HirNode -> null as *void for extraData comparisons
2026-06-05 13:07:28 +03:00
dimgigov 291de88506 docs: update README, Stdlib, BuildAndTest, PLAN for new std modules
Added documentation for:
- Std::Fs (DirExists, Mkdir, ListDir)
- Std::Mem (Alloc, Free, MemEq, New)
- Std::Set<T> (Set_New, Set_Add, Set_Has)
- String_Find, String_Replace, String_Format

Updated PLAN.md blockers (all resolved) and self-host status.
Updated README.md project structure and feature table.
Updated BuildAndTest.md with new stdlib modules and path syntax.

Also includes:
- CLI path argument fix (build/check/run accept project path)
- Remove dummyFunc from parser
- Fix String_Replace to use String_Concat instead of buggy str_join2
2026-06-02 18:46:03 +03:00
dimgigov ec4e748584 fix(qbe): use extraData instead of child3 for hIf else-branch
Block lowering overwrites child3 for statement chaining.
Else block is stored in extraData by hir_lower (matches c_backend).
Fixes QBE parse error 'last block misses jump' in if-without-else chains.
2026-06-02 18:07:35 +03:00
dimgigov eb8179b6d0 fix(qbe): any-child terminator check in QBE_IsTerminator for hBlock — QBE SSA now fully valid
- QBE_IsTerminator(hBlock) checks ALL children, not just last
- Removed legacy c_backend.bux and nim_backend.bux from _selfhost/src/
- QBE self-hosting QBE phase: PASSES (SSA valid, assembled to .s)
- Remaining: linker errors from missing String_Eq/StringBuilder in runtime
2026-06-02 13:56:12 +03:00
dimgigov 6cb5528a4d fix(qbe): no-else uses @lEnd target, removed bodyTerminated, brace fix 2026-06-02 13:53:52 +03:00
dimgigov 7efa79d46f fix(qbe): QBE_IsTerminator for hIf+hBlock, smart @lEnd, block-break on terminator 2026-06-02 13:48:35 +03:00
dimgigov 3881ba8271 fix(qbe): simplified if handler, unconditional @lElse/@lEnd, implicit ret fix 2026-06-02 13:45:01 +03:00
dimgigov 4c610b8a80 fix(qbe): improved if-else label handling, QBE_IsTerminator for hIf 2026-06-02 13:32:41 +03:00
dimgigov 319dcd9280 fix(qbe): QBE_EnsureLocal for all operands, uppercase hVar→0, char→w 2026-06-02 13:29:38 +03:00
dimgigov 0eadb8ab76 fix(qbe): QBE_EnsureLocal for hFieldPtr base, char types w→w 2026-06-02 13:24:08 +03:00
dimgigov 6fdbc139d0 fix(qbe): QBE_EnsureLocal — add % prefix to bare identifiers in store/call/ret 2026-06-02 13:18:15 +03:00
dimgigov a61cd198a8 fix(qbe): empty val/target fallbacks for hStore, hAssign, alloca-init 2026-06-02 13:11:40 +03:00
dimgigov cae53aeaef fix(qbe): Lcx_ResolveTypeKind — map TypeExpr.kind→Type.kind, fix alloca typeKind, skip empty stores 2026-06-02 13:02:35 +03:00
dimgigov 8d0841b6be fix(qbe): hCast passthrough, logical ops =w→=l, implicit ret, && rVal string bug 2026-06-02 12:50:51 +03:00
dimgigov 70cfa4f721 feat: backtick raw/multi-line strings + QBE backend fixes
- Add backtick raw string literals (Go-style ): no escape processing, multi-line
- C backend: handle backtick strings in emitExpr
- Self-hosted lexer: lexScanBacktickString support
- QBE backend: handle backtick strings in data sections
- QBE: remove broken extern declarations (QBE resolves at link time)
- QBE: fix comparison result types (ceql → w, extend to l)
- QBE: fix SSA parameter name clash (_p_ prefix for params)
- QBE: const inlining via HirLower + QBE emitter
- QBE: proper type mapping for bool/char types
- hir_lower: collect dkConst declarations for const propagation
2026-06-02 11:33:29 +03:00
dimgigov 975cc347d0 fix: selfhost CLI run command, lexer char literal, parser test path, calloc in runtime
- Fix lexer unterminated char error test (was treating 'a as lifetime)
- Fix parser test sample.bux path for root-dir execution
- Add bux_system() to runtime.c and run command to selfhost CLI
- Switch bux_alloc from malloc to calloc for zero-init
- Fix module flattening in Cli_Check/Cli_Compile to scan full decl list
- Make test passes fully, selfhost builds successfully
2026-06-02 00:50:16 +03:00
dimgigov 2b911410cb feat: self-hosting progress - Nim backend, parser fixes, 64 struct fields, 13/14 modules check 2026-06-01 23:42:25 +03:00
dimgigov e8d8b62c9d selfhost: fix parser infinite loop + lexer token limit
- Parser_Parse: add beforePos safeguard to skip unrecognized tokens
  instead of looping forever when parserParseDecl returns null
- Lexer: increase maxTokens 4096 -> 32768; add explicit break on limit
- CLI: null-check ReadFile result before String_Eq
- Nim CLI: add -O2 to cc invocation for faster self-hosted builds
2026-06-01 14:03:54 +03:00
dimgigov 55beb23220 WIP: Bootstrap loop debugging
- Fixed module flattening in self-hosted compiler (Cli_Compile now unwraps module wrappers)
- buxc2 check now finds functions in module-wrapped files (token.bux: funcCount=6, Main.bux: funcCount=2)
- Added debug prints to trace lexer.bux timeout and check x segfault
- Remaining issues: lexer.bux hangs in self-hosted pipeline, check x segfaults in runtime
2026-06-01 12:23:25 +03:00
dimgigov 21f8b2e85a feat: async/await/spawn + fix else-if lowering bug
Self-hosted compiler (buxc2):
- Add async/await/spawn tokens, parsing, HIR, lowering, C emission
- Fix pointer type emission (*T -> T*) in C backend
- Fix sizeof(Type) parsing with parentheses
- Fix import ::{...} infinite loop guard
- Fix int64 emission by avoiding else-if chain workaround
- Add debug-free cli and hir_lower

Bootstrap compiler (Nim):
- Fix critical else-if lowering bug in hir_lower.nim:
  when both elseIfs and else block exist, elseIfs were dropped
  causing all else-if chains to collapse to if+else only

Runtime:
- Fix bux_async_await memory corruption (don't free in run)
- Add bux_remove_from_ready for safe cleanup
- Fix forward declaration and deduplicate bux_now_ms

Docs & examples:
- Update async.bux example with proper int64 params
- Update README, LanguageRef, Stdlib, PLAN for async features
- Mark Phase 7.10 bootstrap loop as completed

All 27 examples pass. buxc2 check/build work on all examples.
2026-06-01 02:57:46 +03:00
dimgigov 194d70173c feat: keyword-as-identifier, hFieldPtr/hSizeOf handling, skip forward decl bodies 2026-05-31 21:35:34 +03:00
dimgigov 27b3db5091 feat: keyword-as-identifier, C struct defs with forward decls, field4-7 in HIR 2026-05-31 21:28:40 +03:00
dimgigov 9020f437d4 fix: parser sets typeName for pointer types, simplify Lcx_LowerParam 2026-05-31 21:18:16 +03:00
dimgigov 646c1cad28 feat: while/loop, array indexing (hIndexPtr/hAssign/hLoad), hAssign for assignment expressions 2026-05-31 21:10:17 +03:00
dimgigov 0ae4424bbc fix: null literal emits 0 in C backend — minimal project builds successfully 2026-05-31 21:05:42 +03:00
dimgigov e2c1a21059 fix: pointer type names in let/cast/params (String*), cast uses actual target type 2026-05-31 21:01:39 +03:00
dimgigov 1495b24a9e fix: C codegen improvements - typedefs, forward decls, runtime decls, null-safe runtime 2026-05-31 20:55:05 +03:00
dimgigov e251200a2f fix: null-safe bux_strcmp/bux_strncmp/bux_strcpy, init alloca typeName — project build no longer crashes 2026-05-31 20:49:54 +03:00
dimgigov 535e446b47 fix: infinite-loop guards in match/struct-init parsers 2026-05-31 20:29:59 +03:00
dimgigov dee9a3614c fix: infinite-loop guard in block parser, retTypeKind/alloca type fixes — 11/14 pass 2026-05-31 20:27:48 +03:00
dimgigov 48ee40e7c5 feat: struct init in all phases, 9/14 modules pass check 2026-05-31 20:21:20 +03:00
dimgigov a6d3fedf83 Phase 7.10: extend struct field support to 64 fields, add field4-7 in parser
Parser (src_bux/parser.bux):
- Increased struct field limit from 8 to 64
- Added field4-field7 slots in struct field assignment
- Safeguard: fields beyond slot 7 are parsed but not stored (counted only)

Known issues:
- buxc2 segfaults on ast.bux (Decl struct has 30+ fields, sizeof mismatch
  between Bux parser's Decl and C compiler's Decl)
- Full bootstrap requires Decl struct in ast.bux to match actual Decl layout

All 18 examples pass, all unit tests pass.
2026-05-31 18:35:04 +03:00
dimgigov 6744a35c03 Phase 7.10: workaround else-if bug in lexSkipWhitespace
Lexer (src_bux/lexer.bux):
- Rewrote lexSkipWhitespace to use nested if/else instead of else-if
  chain (workaround for Nim C backend else-if lowering bug)
- lexPeek: mask with & 255 for UTF-8 signed char bytes

Known: buxc2 project build hangs on ast.bux parsing (infinite loop).
buxc2 single-file build works correctly with else-if.
All 18 examples pass, all unit tests pass.
2026-05-31 18:24:44 +03:00
dimgigov 74117595d2 Phase 7.10: multi-file project build, module braces, else-if fix, UTF-8 lexer
Runtime (stdlib/runtime.c):
- bux_list_dir() — directory listing with extension filter
- bux_run_cc() — invoke C compiler on generated C code
- bux_mkdir_if_needed() — create build directory
- bux_dir_exists() — check if directory exists
- Removed duplicate bux_path_join

Parser (src_bux/parser.bux):
- Module parsing with braces: module X { ... }
- else-if parsing: wraps inner if in synthetic Block
- Infinite-loop safeguards in module body and struct body parsing

Lexer (src_bux/lexer.bux):
- lexPeek: mask with & 255 to handle UTF-8 signed char bytes

CLI (src_bux/cli.bux):
- Cli_BuildProject(): multi-file build from src/ directory
  - Lists .bux files, parses each, merges declarations inline
  - Runs sema + HIR + C codegen on merged module
  - Invokes C compiler to produce binary
- New extern declarations: bux_file_exists, bux_dir_exists, bux_path_join,
  bux_mkdir_if_needed, bux_run_cc, bux_list_dir
- FileExists(), DirExists() wrapper functions
- "project" command dispatch

Known issue: else-if chain not fully emitted by buxc2 C backend
(lexSkipWhitespace missing comment handling in generated C).

All 18 examples pass, all unit tests pass.
2026-05-31 18:14:35 +03:00
dimgigov 35b856bab4 Phase 7.10: buxc2 now compiles multi-statement Bux programs
Parser (src_bux/parser.bux):
- Block stores statements as linked list (firstStmt/lastStmt/nextStmt)
- Block struct: added firstStmt, lastStmt fields
- Stmt struct: added nextStmt field

HIR Lowering (src_bux/hir_lower.bux):
- Lcx_LowerBlock: iterates statement linked list, chains HirNodes via child3
- hIf: else block stored in extraData (child3 reserved for block chaining)

C Backend (src_bux/c_backend.bux):
- hBlock: emits statements via child3 linked list with proper indentation
- hStore: combines alloca + value into single declaration (int x = value;)
- hIf: full if/else emission with proper newlines
- Function decl: proper return types and parameter types (not just "int")
- int main() wrapper: generated when Main function exists
- No duplicate return 0 when body already has return

Tested:
- buxc2 compiles hello world program → runs correctly
- buxc2 compiles multi-statement program (let, if, function calls) → runs correctly
- All 18 examples still pass, all unit tests pass
2026-05-31 17:25:03 +03:00
dimgigov e8084b2840 Phase 7.10: command-line args + buxc2 check/build working
Runtime:
- bux_argc()/bux_argv() — command-line arg access from Bux programs
- g_argc/g_argv globals — set by C main() wrapper

Compiler:
- C backend generates extern g_argc/g_argv + int main(argc,argv) wrapper
- main.bux reads args via bux_argc/bux_argv and passes to Cli_Run

buxc2 verified:
- buxc2 version — reads CLI args correctly
- buxc2 check <file.bux> — lexes, parses, type-checks, generates C
- buxc2 build <file.bux> <output.c> — writes C output

PLAN.md updated with Phase 7.10 status and remaining work.
All 18 examples pass, all unit tests pass.
2026-05-31 16:53:03 +03:00
dimgigov cb256397bd Phase 7.9: Self-hosted compiler buxc2 builds and runs! 🎉
buxc (Nim bootstrap) successfully compiles buxc2 (Bux self-hosted compiler)
into a working 88KB ELF x86-64 binary.

Compiler fixes (Nim):
- duplicate symbol: user funcs shadow stdlib funcs via mergeDecls()
- forward declarations: func without body + definition (both orderings)
- extern func dedup: same extern in multiple files
- discard keyword: new language keyword, lowered to expr stmt or no-op
- parser: keywords as field names + advance-on-error safeguard
- parser: var without initializer (zero-init)
- parser: multi-line || && continuation expressions
- parser: else-if chain newline handling
- C backend: const declarations emitted as #define
- C backend: load(field_ptr) → base.field optimization (fixes lvalue errors)

Source fixes (src_bux/*.bux):
- types.bux: tk* → ty* prefix for type kind constants (avoid token conflict)
- sema.bux: remaining tk* → ty* references, StringMap → *void workaround
- hir_lower.bux: ekReturn removed, Lcx_LowerParam helper, *f dereference
- c_backend.bux: &mod.funcs[i] for pointer passing
- cli.bux: ReadFile/WriteFile → bux_read_file/bux_write_file wrappers
- parser.bux: pathStr.len → String_Len(pathStr)
- types.bux: "*" + x → String_Concat("*", x)

Build system:
- Makefile: added 4 missing examples + selfhost target
- PLAN.md: updated with actual project state, Phase 7.9 marked complete

All 18 examples pass, all unit tests pass.
2026-05-31 16:34:36 +03:00
dimgigov 166954204c perf: sema fast-path for large modules (>50 funcs) — skip body checking 2026-05-31 14:33:32 +03:00
dimgigov 679d406690 fix: c_backend.bux else-if replaced with if chain — all 14 files now parse 2026-05-31 14:28:59 +03:00
dimgigov de6e89e3df Phase 7.9: multi-file build, module braces, struct init multi-line, else-if fix 2026-05-31 14:25:57 +03:00
dimgigov f4de065160 fix: module syntax — changed src_bux/*.bux from module X; to module X { ... } 2026-05-31 14:14:42 +03:00
dimgigov 3c2a6e68b9 perf: Scope lookup O(1) via Table instead of O(n) linear search + module parse fix 2026-05-31 14:10:55 +03:00