The Option-C parallel _v vstream API was scaffolding to bring the io stack up alongside the old surface; carrying both permanently is a rule-9 divergence from ref/hare, which has exactly one io surface. Collapse onto that surface (stream = *vtable, ref/hare/io/stream.ha) and rename the _v symbols to their Hare names (io vstream->stream, fmt vfprint->fprint, bufio/memio/log surfaces, log.new). Deletes the 4 lib/*/vstream.ww scaffold files; regenerates w6c/wwdump combined.ww. cstage and wwstage stay byte-identical and combined_ww_fresh holds; all 220 tests pass.
346 lines
11 KiB
C
346 lines
11 KiB
C
/*
|
|
* 780_fmt_vstream_mods_run — project #94 fold-eFinal sentinel. Pins the
|
|
* modifier-formatting machinery (rawleni64 / rawlenstr / rawlenf64 /
|
|
* rawlen / formatraw / formatone) in lib/fmt/fmt.ww: formatfield takes
|
|
* `*mods` and routes through formatone so the {:mods} placeholder
|
|
* honours width / alignment / pad / sign / base / prec. The fold-eFinal
|
|
* FLIP collapsed the dual fmt surface into this one (the former `v*`
|
|
* modifier helpers merged into the bare names).
|
|
*
|
|
* Each row imports fmt + os + io, opens a per-process /tmp output
|
|
* file, calls fdprintf with a {:mods} format string + one arg, and
|
|
* asserts the exact byte content read back.
|
|
*
|
|
* Cstage-only per row — pre-existing wwstage bug #209 (sibling of
|
|
* #190): the wwstage checker bails on fmt.formattable match-arms
|
|
* whenever a downstream probe `import fmt;`s the package. Identical
|
|
* carve-out to 777_fmt_vstream_run (cited there at line 18-33).
|
|
* Byte-id graduates with #209 close.
|
|
*
|
|
* row | format | arg | expect | exit
|
|
* -----------------+------------+--------------+-----------+-----
|
|
* width | "{:5}" | 42i64 | " 42" | 50
|
|
* precision | "{:.3}" | "hello" | "hel" | 51
|
|
* base_hex | "{:x}" | 255i64 | "ff" | 52
|
|
* sign_plus | "{:+}" | 7i64 | "+7" | 53
|
|
* zero_pad | "{:_05}" | 42i64 | "00042" | 54
|
|
*
|
|
* Modifier surface exercised (subset of scanmods at fmt.ww:376-405):
|
|
*
|
|
* width digits 1..9 — RIGHT-align pad to N chars
|
|
* prec '.' + digits — str truncation / int min-digits
|
|
* base 'x' — strconv.base.HEX_LOWER
|
|
* neg '+' — emit '+' on non-negative
|
|
* pad '_<rune>' — pad rune override (default space when ':')
|
|
*
|
|
* Drew NaN/Inf signoff (carry from task #58): f64 arm not exercised
|
|
* here (rows pin i64+str+bool fastpaths); follow-on fold can add f64
|
|
* once Drew's strconv.f64tos shortest-G stays static. The fold-e6
|
|
* implementation honours the sign-peel in vformatraw f64 arm; an
|
|
* end-to-end test lands when a NaN/Inf-aware probe joins the corpus.
|
|
*
|
|
* IMPORT-ORDER WORKAROUND (sibling task #208, pre-existing): every
|
|
* row places `import os;` FIRST for the same reason 776/777 document
|
|
* (wwstage checker ordering-sensitivity on os.tryread/trywrite/
|
|
* tryopen's bare `return r;` over a tagged return type).
|
|
*
|
|
* DEFERRALS / RELATED (NOT addressed by these rows):
|
|
*
|
|
* - #209 (wwstage formattable match-arm bail): blocks STAGE_WW
|
|
* here, same as 777_fmt_vstream_run. Cstage-only until close.
|
|
*
|
|
* BOOTSTRAP-EMBED CHECK: fmt is NOT embedded in any selfhost
|
|
* combined.ww (grep confirmed), so the fold-eFinal fmt collapse does
|
|
* NOT require a Makefile regen (#110); only test paths see the
|
|
* machinery. 990-997 byte-id gates stay green by virtue of fmt being
|
|
* test-only on the bootstrap surface.
|
|
*
|
|
* GATE POLARITY: must stay GREEN. A red here means formatone /
|
|
* formatraw / rawlen miscomputed bytes, formatfield regressed its
|
|
* *mods route, scanmods/modsinit drifted, or the strconv.u64tos base
|
|
* path miscompiled under fdprintf's intrusive io.stream→*fd_ctx
|
|
* dispatch.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/wait.h>
|
|
|
|
static int
|
|
runwait(const char *cmd)
|
|
{
|
|
int rc = system(cmd);
|
|
if (rc == -1) return -1;
|
|
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
|
|
return -1;
|
|
}
|
|
|
|
#define STAGE_CS 1
|
|
#define STAGE_WW 2
|
|
|
|
struct row {
|
|
const char *label;
|
|
const char *src;
|
|
int want_exit;
|
|
int stage_mask;
|
|
int byte_id;
|
|
};
|
|
|
|
static const struct row rows[] = {
|
|
{ "width",
|
|
"package main;\n"
|
|
"import os;\n"
|
|
"import fmt;\n"
|
|
"import io;\n"
|
|
"export fn main() i32 = {\n"
|
|
" let path: str = \"/tmp/wwfmtv_780_a\";\n"
|
|
" let fd: i32 = os.open(path, os.flag.RDWR | os.flag.CREATE | os.flag.TRUNC, 0o644: i32);\n"
|
|
" if (fd < 0) { return 90; };\n"
|
|
" let wr = fmt.fdprintf(fd, \"{:5}\", 42i64);\n"
|
|
" let nw: size = 0;\n"
|
|
" match (wr) {\n"
|
|
" case let n: size => { nw = n; };\n"
|
|
" case io.error => { return 91; };\n"
|
|
" };\n"
|
|
" os.close(fd);\n"
|
|
" let rd: i32 = os.open(path, os.flag.RDONLY, 0i32);\n"
|
|
" if (rd < 0) { return 92; };\n"
|
|
" let buf: [16]u8;\n"
|
|
" let n: i64 = os.read(rd, &buf[0], 16u64);\n"
|
|
" os.close(rd);\n"
|
|
" if (n != 5i64) { return 93; };\n"
|
|
" if (nw != 5: size) { return 94; };\n"
|
|
" if (buf[0] != 32u8 || buf[1] != 32u8 || buf[2] != 32u8 || buf[3] != 52u8 || buf[4] != 50u8) { return 95; };\n"
|
|
" return 50;\n"
|
|
"};\n",
|
|
50,
|
|
STAGE_CS, 0 },
|
|
{ "precision",
|
|
"package main;\n"
|
|
"import os;\n"
|
|
"import fmt;\n"
|
|
"import io;\n"
|
|
"export fn main() i32 = {\n"
|
|
" let path: str = \"/tmp/wwfmtv_780_b\";\n"
|
|
" let fd: i32 = os.open(path, os.flag.RDWR | os.flag.CREATE | os.flag.TRUNC, 0o644: i32);\n"
|
|
" if (fd < 0) { return 90; };\n"
|
|
" let wr = fmt.fdprintf(fd, \"{:.3}\", \"hello\");\n"
|
|
" let nw: size = 0;\n"
|
|
" match (wr) {\n"
|
|
" case let n: size => { nw = n; };\n"
|
|
" case io.error => { return 91; };\n"
|
|
" };\n"
|
|
" os.close(fd);\n"
|
|
" let rd: i32 = os.open(path, os.flag.RDONLY, 0i32);\n"
|
|
" if (rd < 0) { return 92; };\n"
|
|
" let buf: [16]u8;\n"
|
|
" let n: i64 = os.read(rd, &buf[0], 16u64);\n"
|
|
" os.close(rd);\n"
|
|
" if (n != 3i64) { return 93; };\n"
|
|
" if (nw != 3: size) { return 94; };\n"
|
|
" if (buf[0] != 104u8 || buf[1] != 101u8 || buf[2] != 108u8) { return 95; };\n"
|
|
" return 51;\n"
|
|
"};\n",
|
|
51,
|
|
STAGE_CS, 0 },
|
|
{ "base_hex",
|
|
"package main;\n"
|
|
"import os;\n"
|
|
"import fmt;\n"
|
|
"import io;\n"
|
|
"export fn main() i32 = {\n"
|
|
" let path: str = \"/tmp/wwfmtv_780_c\";\n"
|
|
" let fd: i32 = os.open(path, os.flag.RDWR | os.flag.CREATE | os.flag.TRUNC, 0o644: i32);\n"
|
|
" if (fd < 0) { return 90; };\n"
|
|
" let wr = fmt.fdprintf(fd, \"{:x}\", 255i64);\n"
|
|
" let nw: size = 0;\n"
|
|
" match (wr) {\n"
|
|
" case let n: size => { nw = n; };\n"
|
|
" case io.error => { return 91; };\n"
|
|
" };\n"
|
|
" os.close(fd);\n"
|
|
" let rd: i32 = os.open(path, os.flag.RDONLY, 0i32);\n"
|
|
" if (rd < 0) { return 92; };\n"
|
|
" let buf: [16]u8;\n"
|
|
" let n: i64 = os.read(rd, &buf[0], 16u64);\n"
|
|
" os.close(rd);\n"
|
|
" if (n != 2i64) { return 93; };\n"
|
|
" if (nw != 2: size) { return 94; };\n"
|
|
" if (buf[0] != 102u8 || buf[1] != 102u8) { return 95; };\n"
|
|
" return 52;\n"
|
|
"};\n",
|
|
52,
|
|
STAGE_CS, 0 },
|
|
{ "sign_plus",
|
|
"package main;\n"
|
|
"import os;\n"
|
|
"import fmt;\n"
|
|
"import io;\n"
|
|
"export fn main() i32 = {\n"
|
|
" let path: str = \"/tmp/wwfmtv_780_d\";\n"
|
|
" let fd: i32 = os.open(path, os.flag.RDWR | os.flag.CREATE | os.flag.TRUNC, 0o644: i32);\n"
|
|
" if (fd < 0) { return 90; };\n"
|
|
" let wr = fmt.fdprintf(fd, \"{:+}\", 7i64);\n"
|
|
" let nw: size = 0;\n"
|
|
" match (wr) {\n"
|
|
" case let n: size => { nw = n; };\n"
|
|
" case io.error => { return 91; };\n"
|
|
" };\n"
|
|
" os.close(fd);\n"
|
|
" let rd: i32 = os.open(path, os.flag.RDONLY, 0i32);\n"
|
|
" if (rd < 0) { return 92; };\n"
|
|
" let buf: [16]u8;\n"
|
|
" let n: i64 = os.read(rd, &buf[0], 16u64);\n"
|
|
" os.close(rd);\n"
|
|
" if (n != 2i64) { return 93; };\n"
|
|
" if (nw != 2: size) { return 94; };\n"
|
|
" if (buf[0] != 43u8 || buf[1] != 55u8) { return 95; };\n"
|
|
" return 53;\n"
|
|
"};\n",
|
|
53,
|
|
STAGE_CS, 0 },
|
|
{ "zero_pad",
|
|
"package main;\n"
|
|
"import os;\n"
|
|
"import fmt;\n"
|
|
"import io;\n"
|
|
"export fn main() i32 = {\n"
|
|
" let path: str = \"/tmp/wwfmtv_780_e\";\n"
|
|
" let fd: i32 = os.open(path, os.flag.RDWR | os.flag.CREATE | os.flag.TRUNC, 0o644: i32);\n"
|
|
" if (fd < 0) { return 90; };\n"
|
|
" let wr = fmt.fdprintf(fd, \"{:_05}\", 42i64);\n"
|
|
" let nw: size = 0;\n"
|
|
" match (wr) {\n"
|
|
" case let n: size => { nw = n; };\n"
|
|
" case io.error => { return 91; };\n"
|
|
" };\n"
|
|
" os.close(fd);\n"
|
|
" let rd: i32 = os.open(path, os.flag.RDONLY, 0i32);\n"
|
|
" if (rd < 0) { return 92; };\n"
|
|
" let buf: [16]u8;\n"
|
|
" let n: i64 = os.read(rd, &buf[0], 16u64);\n"
|
|
" os.close(rd);\n"
|
|
" if (n != 5i64) { return 93; };\n"
|
|
" if (nw != 5: size) { return 94; };\n"
|
|
" if (buf[0] != 48u8 || buf[1] != 48u8 || buf[2] != 48u8 || buf[3] != 52u8 || buf[4] != 50u8) { return 95; };\n"
|
|
" return 54;\n"
|
|
"};\n",
|
|
54,
|
|
STAGE_CS, 0 },
|
|
};
|
|
|
|
static int
|
|
write_source(const char *path, const char *src)
|
|
{
|
|
FILE *f = fopen(path, "wb");
|
|
if (!f) return -1;
|
|
fputs(src, f);
|
|
fclose(f);
|
|
return 0;
|
|
}
|
|
|
|
/* Per-row tmpdir cleanup. ww_ww writes intermediates next to the
|
|
* source (filed task #15), so each row's build leaves
|
|
* <src>.{combined.ww,s,o} + bare exe alongside. Mirror of 777's. */
|
|
static void
|
|
cleanup_tmp(const char *tmpdir, const char *base)
|
|
{
|
|
char p[640];
|
|
snprintf(p, sizeof p, "%s/%s.ww", tmpdir, base); unlink(p);
|
|
snprintf(p, sizeof p, "%s/%s.s", tmpdir, base); unlink(p);
|
|
snprintf(p, sizeof p, "%s/%s.o", tmpdir, base); unlink(p);
|
|
snprintf(p, sizeof p, "%s/%s.combined.ww", tmpdir, base); unlink(p);
|
|
snprintf(p, sizeof p, "%s/%s", tmpdir, base); unlink(p);
|
|
rmdir(tmpdir);
|
|
}
|
|
|
|
static int
|
|
build_via_driver(const char *driver, const char *tmpdir, const char *cwd,
|
|
const char *src)
|
|
{
|
|
char cmd[2048];
|
|
snprintf(cmd, sizeof cmd,
|
|
"cd %s && timeout 180 %s build -I %s/lib %s 2>/dev/null",
|
|
tmpdir, driver, cwd, src);
|
|
return runwait(cmd);
|
|
}
|
|
|
|
static int
|
|
run_row(const char *driver, const char *cwd, const struct row *r, int seq)
|
|
{
|
|
char tmpdir[256], src[512], base[64], outbin[768];
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/fvm_%d_d_%d", getpid(), seq);
|
|
snprintf(base, sizeof base, "main780");
|
|
snprintf(src, sizeof src, "%s/%s.ww", tmpdir, base);
|
|
mkdir(tmpdir, 0755);
|
|
if (write_source(src, r->src) != 0) { cleanup_tmp(tmpdir, base); return -1; }
|
|
int rc;
|
|
int br = build_via_driver(driver, tmpdir, cwd, src);
|
|
if (br == 0) {
|
|
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
|
|
rc = runwait(outbin);
|
|
} else {
|
|
rc = -1;
|
|
}
|
|
cleanup_tmp(tmpdir, base);
|
|
return rc;
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
const char *bin = getenv("BIN");
|
|
if (!bin) bin = "out/bin";
|
|
char cwd[256];
|
|
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
|
|
char absbin[512];
|
|
if (bin[0] != '/') {
|
|
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
|
|
bin = absbin;
|
|
}
|
|
|
|
char cdrv[640], wdrv[640];
|
|
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
|
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
|
|
|
int n = (int)(sizeof rows / sizeof rows[0]);
|
|
int total = 0, fail = 0;
|
|
int wwpresent = (access(wdrv, X_OK) == 0);
|
|
int seq = 0;
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
if (rows[i].stage_mask & STAGE_CS) {
|
|
total++;
|
|
int got = run_row(cdrv, cwd, &rows[i], seq++);
|
|
if (got != rows[i].want_exit) {
|
|
fprintf(stderr,
|
|
"fmt_vstream_mods_run[cs][%s]: exit=%d want=%d\n",
|
|
rows[i].label, got, rows[i].want_exit);
|
|
fail++;
|
|
}
|
|
}
|
|
if (wwpresent && (rows[i].stage_mask & STAGE_WW)) {
|
|
total++;
|
|
int got = run_row(wdrv, cwd, &rows[i], seq++);
|
|
if (got != rows[i].want_exit) {
|
|
fprintf(stderr,
|
|
"fmt_vstream_mods_run[ww][%s]: exit=%d want=%d\n",
|
|
rows[i].label, got, rows[i].want_exit);
|
|
fail++;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!wwpresent)
|
|
fprintf(stderr, "fmt_vstream_mods_run: skip wwstage (no %s)\n", wdrv);
|
|
|
|
if (fail) {
|
|
fprintf(stderr, "fmt_vstream_mods_run: %d/%d fixtures failed\n",
|
|
fail, total);
|
|
return 1;
|
|
}
|
|
printf("fmt_vstream_mods_run: %d/%d ok\n", total, total);
|
|
return 0;
|
|
}
|