Commit Graph
9 Commits
Author SHA1 Message Date
Eremey Valetov 6b63f14b8c Compiler: zero compile errors — all 72 programs compile, 32 pass
Fix last 2 compile errors:
- SWAP with array elements: parse subscripts via emit_to_buf, use
  gwrt_array_elem pointers for both operands
- ENVIRON$/DATE$/TIME$ in expressions: handle 0xFE-prefix tokens in
  both emit_atom (numeric) and emit_str_expr (string) contexts, with
  proper string detection in PRINT

Other fixes:
- RND(n): discard argument via buffer (don't emit inline)
- Float 0.0f: emit "0.0f" not "0f"
- MID$ 3-argument: proper optional length parameter
- FOR scope: static limit/step (no {} blocks)
- RESUME/RESUME NEXT: proper statement handler
- ON TIMER/KEY/COM: skip event traps
- MID$ assignment: skip handler
- ERROR statement: emit gw_error()
- TIMER as expression: emit time() call
- PMAP/ERDEV as expressions: stub values

32/72 tests pass. 0 compile errors. 37 output mismatches. 3 skipped.
2026-03-29 14:04:56 -04:00
Eremey Valetov 0e0052f9cf Compiler: fix 16 of 18 compile errors — 32/72 tests passing
Fixes:
- Include graphics.h/virmem.h in gwrt.h (5 programs: draw_commands,
  get_put, graphics_stubs, peek_gfx, view_window)
- RND(n): discard argument to buffer instead of emitting it inline
- Float constant 0: emit "0.0f" not "0f" (invalid C suffix)
- MID$ function: proper 3-argument emission with optional length
- FOR/NEXT: use static variables instead of block scope for limit/step
  (fixes FOR inside IF/THEN and unmatched brace issues)
- RESUME/RESUME NEXT: handle as statements (not misparse as FOR/NEXT)
- ON TIMER/KEY/COM: skip event trap setup (not misparse as ON n GOTO)
- MID$ assignment: recognize and skip (don't misparse as variable)
- ERROR statement: emit gw_error() call
- ERASE: stub handler

Only 2 compile errors remain: bubble_sort (SWAP with arrays),
misc_stmts (ENVIRON$ variable name clash).

32 of 72 tests pass. New: datetime, luhn, peek_poke, error_handler,
on_timer, timer_stop, monte_carlo, number_guess.
2026-03-29 13:31:17 -04:00
Eremey Valetov c3319a59c7 Compiler: PRINT USING, STRING$, INSTR, combined relationals — 27/72 tests
PRINT USING: embed token bytes in generated C and call gw_print_using()
at runtime. Variable values synced to interpreter table before the call
so gw_eval() can resolve them. Unlocks fibonacci, temp_table, calendar.

STRING$ (single-byte token 0xD4): handle in emit_str_expr and PRINT
string detection. Emits gw_fn_strings() call.

INSTR (single-byte token 0xD6): handle in emit_atom with start position
detection. Emits gw_fn_instr() call.

PRINT USING token skip fix: skip past TOK_USING before embedding bytes
(gw_print_using expects text_ptr after USING, not at it).

27 of 72 tests now pass. New: fibonacci, calendar, temp_table.
2026-03-29 12:17:06 -04:00
Eremey Valetov 3b6f8fdc80 Compiler: fix array names, combined relationals, expression type tracking
Array name emission: use emit_name_str() to avoid null bytes in C string
literals when variable name has only one significant character.

Combined relationals: handle <=, >=, <> token pairs (TOK_LT+TOK_EQ etc.)
in the precedence climber. Previously these emitted "unknown tok 0xe5"
which broke WHILE X <= 10 and similar conditions.

Expression type tracking: peek_expr_type() determines VT_INT/VT_SNG/VT_DBL
from the leading token in PRINT expressions. PRINT now emits the correct
gw_value_t type for proper number formatting (single vs double precision).

23 of 72 tests now pass (up from 19). New: arrays, hailstone, while_wend,
prime_sieve, leibniz.
2026-03-29 11:41:39 -04:00
Eremey Valetov 7ff24ae85e Compiler: add array support, fix number format, OPEN variable clash
Arrays: DIM creates arrays via gwrt_dim() runtime call. Array element
read/write uses gwrt_array_elem() with buffered subscript expressions
(open_memstream) to correctly determine dimension count. Auto-DIM with
default size 10 for undeclared arrays.

Number formatting: PRINT now uses gw_print_value() (same as interpreter)
instead of gwrt_print_sng(), producing correct type-specific output.

OPEN fix: analysis pass skips OPEN statement arguments to avoid
misidentifying OUTPUT/INPUT/APPEND as variable names.

OPEN/CLOSE/INPUT: emit skip stubs (file I/O compilation is Phase 3).

19 of 72 tests now pass (up from 17). New: hanoi, text_adventure.
2026-03-29 10:41:02 -04:00
Eremey Valetov 58c6d8c99d Compiler Phase 2: WHILE/WEND, ON GOTO/GOSUB, MOD/IDIV/POW, statements
Add WHILE/WEND, ON n GOTO/GOSUB, ON ERROR GOTO, DIM (stub), SWAP,
POKE, DEF SEG, RANDOMIZE, COLOR, LOCATE, SCREEN, WIDTH, KEY, WRITE,
OPTION BASE, DEFINT/DEFSNG/DEFDBL/DEFSTR, TRON/TROFF.

Fix MOD/IDIV/POW operators: buffer left operand via open_memstream so
both sides can be properly cast (MOD/IDIV → int16_t, POW → pow()).
Previously MOD on float operands was a C compile error.

Handle all extended statement tokens (0xFE prefix): graphics, sound,
file I/O, view/window, environ, timer — either with real codegen or
graceful skip.

17 of 72 tests now pass (up from 15). 26 compile errors (down from 30).
2026-03-29 10:22:34 -04:00
Eremey Valetov e535250f6c Fix compiler QA round 2: nested FOR/NEXT, remove unused code, clean warnings
- FOR/NEXT: use a FOR stack to match NEXT variables to their corresponding
  FOR labels, fixing nested loops (e.g., FOR I...FOR J...NEXT J...NEXT I)
- Remove unused emit_int_expr function
- Build now zero warnings across all targets
- Compiler passes 15 of 72 test programs (Phase 1 target: ~30)
2026-03-29 10:03:46 -04:00
Eremey Valetov 4dc255118a Fix compiler QA: misleading indentation, DATA escaping, NEXT safety, bounds
- codegen.c: refactor one-liner function cases to proper multi-line blocks,
  eliminating all -Wmisleading-indentation warnings
- codegen.c: escape quotes and backslashes in DATA pool string literals
- codegen.c: guard NEXT against for_label_counter=0 (NEXT without FOR)
- codegen.c: remove unused functions (emit_expr, read_sng, read_dbl)
- analysis.c: add bounds check on data_line_map writes
- Build now produces zero warnings across all three targets
2026-03-29 09:38:15 -04:00
Eremey Valetov d3b57d9f3b Implement ahead-of-time compiler (Phase 1): BASIC to C via token stream
New tool gwbasic-compile translates tokenized .bas programs to C source,
which gcc compiles into native executables linked against libgwrt.a (the
interpreter's runtime modules minus the execution loop).

Pipeline: .bas → gw_crunch() → analysis pass (line table, variable census,
GOTO targets, DATA collection) → C codegen → gcc → native executable.

Phase 1 supports: PRINT, LET, IF/THEN/ELSE, GOTO, GOSUB/RETURN, FOR/NEXT,
END/STOP/SYSTEM, REM, DATA/READ/RESTORE, CLS, arithmetic/relational/logical
operators, core math functions (SIN, COS, SQR, ABS, etc.), string functions
(LEFT$, RIGHT$, MID$, CHR$, ASC, VAL, STR$, LEN, etc.), string concatenation.

All control flow uses goto/labels (no C for/while) so GOTO into loops works.
GOSUB uses a return-label stack with switch dispatch.
2026-03-29 06:59:42 -04:00