Skip to content

Add pure-Silver formatting + print/abort ABI (Wave 1 unit 3) - #8

Merged
CierCier merged 6 commits into
mainfrom
std/fmt-pure-silver
Jul 20, 2026
Merged

CierCier merged 6 commits into
mainfrom
std/fmt-pure-silver

Conversation

@CierCier

@CierCier CierCier commented Jul 20, 2026 •

Copy link
Copy Markdown
Owner

Summary

Wave 1 unit 3 of docs/runtime-migration.md.

  • std/fmt.ag — itoa/utoa/ftoa/btoa, UTF-8 validation helpers, buffered stdout/stderr writes over sys_write
  • std/io.ag — print path rewritten onto fmt; exports frozen silver_rt_print_* / silver_rt_abort* ABI
  • tests/fmt_test.ag — 46 cases covering integer/float formatting edge cases and the print surface

File API still uses FILE* temporarily (migrates in unit 9).

Test plan

Greptile Summary

This PR introduces std/fmt.ag — a pure-Silver number-to-text formatting module implementing Grisu2 f64 conversion, integer/bool formatters, UTF-8 validation, and raw write(2) I/O helpers. std/io.ag's println/eprintln are rewritten onto these helpers (bypassing libc stdio), and ffi.rs aligns the Rust silver_rt_print_bytes non-UTF-8 sentinel to include a newline, matching the Silver implementation.

  • std/fmt.ag: Adds itoa/utoa/btoa/ftoa (all buffer-based, no allocation), a correct UTF-8 validator, and the frozen silver_rt_print_* / silver_rt_abort* ABI using sys_write directly. The Grisu2 port faithfully replicates the rapidjson cached-power table and digit-generation loop, including manual unsigned arithmetic workarounds for the current codegen's signed-only lowering.
  • std/io.ag: println/eprintln now call write_stdout/write_stderr from std.fmt, gaining null-pointer safety that the old puts-based path lacked; the return semantics remain compatible (non-negative on success, EOF on error).
  • tests/fmt_test.ag: 46 cases covering boundary values (i64 MIN/MAX, u64 MAX, 5e-324, f64 MAX, NaN, ±inf, -0), UTF-8 edge cases (surrogates, overlongs, truncated sequences), and the print surface as a smoke test.

Confidence Score: 5/5

Safe to merge; no correctness issues found in the formatting, UTF-8 validation, ABI surface, or Grisu2 port.

The Grisu2 cached-power table and digit-generation loop are faithful to the rapidjson reference; integer/bool formatters and the two's-complement MIN handling are correct; UTF-8 validation correctly covers all lead-byte ranges, overlong sequences, surrogates, and truncated inputs. The ffi.rs change aligns the Rust sentinel with the Silver implementation. The one observation (zero-write short-circuit going undetected by the < 0 check in println) is a theoretical edge case that does not affect normal blocking stdout/stderr use.

No files require special attention.

Important Files Changed

Filename Overview
std/fmt.ag New 808-line module: Grisu2 f64 formatter, integer/bool formatters, UTF-8 validator, raw fd writers, and the frozen silver_rt_print_*/abort ABI — all implemented correctly with careful boundary handling and well-documented workarounds for signed-only codegen.
std/io.ag println/eprintln rewritten onto std.fmt write helpers; null-pointer guard added; File API and variadic extern surface unchanged.
tests/fmt_test.ag 46-case test suite for itoa/utoa/btoa/ftoa and the print surface; exercises boundary values and the UTF-8 validator; non-UTF-8 sentinel path is now exercised inline (previously untested).
silver_runtime/src/runtime/ffi.rs One-line fix: silver_rt_print_bytes non-UTF-8 branch changed from print! to println!, aligning the Rust runtime sentinel with the Silver implementation.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Silver source calls print/format] --> B{Which path?}
    B -->|println / eprintln| C[std.io: null check]
    B -->|silver_rt_print_*| D[std.fmt: format into FmtPrintBuf]
    B -->|silver_rt_abort*| E[std.fmt: write_stderr + sys_kill SIGABRT]
    C --> F[std.fmt: cstr_len]
    F --> G[write_stdout / write_stderr]
    D --> H[itoa / utoa / btoa / ftoa]
    H --> I[append newline to buffer]
    I --> G
    G --> J[__fmt_write_fd: retry loop]
    J -->|r > 0| K[accumulate written bytes]
    K --> J
    J -->|written == len| L[sys_write fd 1 or 2]
    J -->|r == EINTR -4| J
    J -->|r < 0 non-EINTR| M[return error]
    subgraph ftoa Grisu2
        H2[extract IEEE-754 fields] --> H3{special?}
        H3 -->|NaN| H4[write NaN]
        H3 -->|Inf| H5[write inf]
        H3 -->|zero| H6[write 0 or -0]
        H3 -->|normal/subnormal| H7[__fmt_grisu2]
        H7 --> H8[cached power lookup]
        H8 --> H9[__fmt_digit_gen + __fmt_grisu_round]
        H9 --> H10[place decimal point]
    end
    H --> H2
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    A[Silver source calls print/format] --> B{Which path?}
    B -->|println / eprintln| C[std.io: null check]
    B -->|silver_rt_print_*| D[std.fmt: format into FmtPrintBuf]
    B -->|silver_rt_abort*| E[std.fmt: write_stderr + sys_kill SIGABRT]
    C --> F[std.fmt: cstr_len]
    F --> G[write_stdout / write_stderr]
    D --> H[itoa / utoa / btoa / ftoa]
    H --> I[append newline to buffer]
    I --> G
    G --> J[__fmt_write_fd: retry loop]
    J -->|r > 0| K[accumulate written bytes]
    K --> J
    J -->|written == len| L[sys_write fd 1 or 2]
    J -->|r == EINTR -4| J
    J -->|r < 0 non-EINTR| M[return error]
    subgraph ftoa Grisu2
        H2[extract IEEE-754 fields] --> H3{special?}
        H3 -->|NaN| H4[write NaN]
        H3 -->|Inf| H5[write inf]
        H3 -->|zero| H6[write 0 or -0]
        H3 -->|normal/subnormal| H7[__fmt_grisu2]
        H7 --> H8[cached power lookup]
        H8 --> H9[__fmt_digit_gen + __fmt_grisu_round]
        H9 --> H10[place decimal point]
    end
    H --> H2
Loading

Reviews (3): Last reviewed commit: "Merge origin/main into std/fmt-pure-silv..." | Re-trigger Greptile

Context used:

  • Context used - You are an expert systems programmer, compiler eng... (source)

New std/fmt.ag provides itoa/utoa/ftoa/btoa and buffered write helpers
over sys_write. std/io.ag print path uses the new formatters and exports
silver_rt_print_{i64,u64,bool,f64,cstr,bytes}, silver_rt_abort, and
silver_rt_abort_cstr matching silver_runtime/src/runtime/ffi.rs.

Co-Authored-By: Claude Fable 5 <[email protected]>
Copilot AI review requested due to automatic review settings July 20, 2026 16:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment thread std/fmt.ag Resolved
Comment thread std/io.ag Resolved
@CierCier
CierCier merged commit af94705 into main Jul 20, 2026
2 checks passed
@CierCier
CierCier deleted the std/fmt-pure-silver branch July 21, 2026 10:16
CierCier added a commit that referenced this pull request Aug 1, 2026
Add pure-Silver formatting + print/abort ABI (Wave 1 unit 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants