Repository navigation
Phase 2: @print macro family — compile-time format expansion - #11
Conversation
…d/rt/ - Delete silver_runtime/ Rust crate entirely - Add/update pure-Silver runtime in std/rt/: - std/rt/casts.ag (numeric cast registry) - std/rt/value.ag (boxed Value constructors) - std/rt/methods.ag (method dispatch, intern table, builtins) - std/rt/runtime.ag (unified Runtime aggregate) - std/sys/entry.ag (_start entry point) - Fix interior pointer bug in MethodRegistry (no stored TypeRegistry*/Heap*) - Add --static-runtime flag and -nostdlib linking in compiler driver - Add tests: rt_casts_test, rt_methods_test, runtime_test, static_link_test - Fix all doc references (AGENTS.md, README.md, runtime-migration.md) - Mark plan/runtime-migration.md as superseded
Generated by ./update-bootstrap.sh after deleting silver_runtime/ and adding pure-Silver std/rt/ modules (casts, methods, runtime, value, entry).
- Extended MacroHandler trait to pass macro name to type_check/codegen - Added parse_format helper and FormatSegment enum in builtin_macros - Added PrintHandler struct registering all 6 macros (@print/@println/@Eprint /@eprintln/@fprint/@Sprint) - Added print_typeck method on TypeChecker for format validation - Added print_codegen on LlvmIrGenerator for BufWriter method-call expansion - Added value_write_method_name to dispatch format placeholders to correct BufWriter methods (write_str/write_i64/write_u64/write_f64/write_bool) - Extended resolve_receiver_type with Literal type inference - @Sprint returns empty string as placeholder (full impl deferred) - Stdlib and test changes: string concat, libc extern removal prep, test rewrites for print macros - Various test fixes and libc extern deletion across stdlib
| Some(ty) => Self::type_to_write_method(&ty.kind), | ||
| None => { | ||
| // Fallback: emit the value and infer type from LLVM type | ||
| let val = self.emit_expression_value(expr).map_err(|e| { |
There was a problem hiding this comment.
Fallback evaluates expressions twice
When resolve_receiver_type returns None, this call emits the placeholder expression to infer its LLVM type. emit_method_call_expression then emits the same expression again as the writer argument. A supported conditional or block expression containing a side-effecting call therefore runs twice, discards the first result, and prints the second. Type inference must not emit the expression unless that emitted value is reused by the generated writer call.
Context Used: You are an expert systems programmer, compiler eng... (source)
| u8* ptr = (u8*)realloc<u8>((u8*)(*self).data, new_cap); | ||
| (*self).data = (i64)ptr; | ||
| (*self).cap = new_cap; |
There was a problem hiding this comment.
Failed growth corrupts writer state
The allocator can return 0 when growth fails, but this path replaces data and records a nonzero capacity without checking the result. The old allocation is lost, and the next write skips initialization because cap != 0 before indexing through the null data pointer. An allocation failure during buffer growth therefore causes a leak followed by a null-pointer write. Commit the new pointer and capacity only after confirming that reallocation succeeded.
Context Used: You are an expert systems programmer, compiler eng... (source)
Phase 2: @print macro family — compile-time format expansion
Summary
Implements Phase 2 of the libc-removal plan: the
@print/@println/@eprint/@eprintln/@fprint/@sprintbuilt-in macro family with compile-time{}placeholder expansion into BufWriter method calls.Changes
Core:
@printmacro familyagc/src/builtin_macros/mod.rs—parse_formathelper,FormatSegmentenum,PrintHandlerstruct (MacroHandler impl), registration for all 6 macrosagc/src/semantic/typeck.rs—print_typeckmethod: validates format string literal, counts placeholders, checks arg count, type-checks value argsagc/src/codegen/llvm_ir.rs—print_codegen: extracts format segments, builds synthetic AST expressions for BufWriter method calls, dispatches placeholder types to correct write method (write_str/write_i64/write_u64/write_f64/write_bool). Also:value_write_method_namehelper,sprint_codegenplaceholder,resolve_receiver_typeLiteral extensionInfra
MacroHandlertrait extended withname: &strparameter on both methods; all existing handlers updatedMacroRegistry::type_check/codegenpassnameto handlersStdlib & tests
"A" "B"→"AB")printfto@printlnTesting
cargo test -p agc: 217/218 pass (1 pre-existing failure indisplay.agtrait parsing)Notes
@sprintreturns empty string (full implementation deferred per plan)--dynamic-runtimeflag required at runtime (static-runtime default is pre-existing)Greptile Summary
This PR adds compile-time formatting macros backed by the pure-Silver I/O path. The main changes are:
BufWritermethod calls.BufWriterimplementation for standard output, files, and formatted strings.Confidence Score: 4/5
The remaining runtime failures should be fixed before merging.
agc/src/codegen/llvm_ir.rs and std/io.ag
Important Files Changed
Reviews (2): Last reviewed commit: "Update bootstrap artifacts after compile..." | Re-trigger Greptile
Context used: