Hare-faithful index/predicate family per ref/hare/strings/{dup,
concat,trim,index,suffix,contains,compare,utf8}.ha. Non-variadic
subset (concat 2-arg, trim single-rune, contains single-needle)
pending task #16 — cstage variadic-pack drops .len on multi-field
element types; ship the Hare-faithful single-arg shape now, file
the variadic upgrade as follow-up. `sub` follow-up filed as #29
(commit 2 with iterator + utf8.chars relocation).
Surface: dup, concat, trim/trimprefix/trimsuffix (single rune),
hasprefix, hassuffix (both with (str|rune) sum needle),
byteindex, rbyteindex (both with (str|rune) sum needle),
contains (single str needle), compare, toutf8, fromutf8_unsafe,
runebytes helper. (str|rune) match arms route the rune via
utf8.encoderune into a [4]u8 scratch then bytes.index/rindex —
drew-devault's directive for clean Hare-fidelity over invented
ASCII-only rune-byte arms.
byteindex / rbyteindex rune-arm semantic correction —
corpus-coverage-blind unmask. Pre-existing impl scanned for
`r: u8` (broken for all rune values >0x7F since strings.ww first
landed; no caller exercised it). Replaced with utf8.encoderune-
based scan via runebytes helper. Severity-marker: silent
wrong-result for any non-ASCII rune needle, masked by zero
in-tree callers until lib/strings + utf8 chain pulled the shape
in.
Build-system propagation: lib/strings depends transitively on
lib/encoding/utf8 (via byteindex's rune arm). cmd/ww driver's
locate_import_in (cmd/ww/main.c:85) walks `<dir>/<name>.ww` and
`<dir>/<name>/<name>.ww` only — `use utf8;` doesn't find
lib/encoding/utf8/utf8.ww without explicit `-I lib/encoding/utf8`.
Propagated through 5 wwstage-tool Makefile targets + 7 test
wrappers + test/wcc/995_self_rebuild.c sprintf lines. Task #17
filed for the principled resolver fix (subdir walk vs Hare's
qualified `use encoding::utf8;` notation).
This commit chain (#15 strings) surfaced 7 cgen bugs during
landing: #16 cstage variadic-pack, #17 resolver nested-paths,
#27 aliaslookup leaf-collision, #22 zero-init !void/void-alias
let-decl, #15-cstage retscr SSoT name, #24 composite CALL return
as composite arg, #28 N_DOT calleeparams. All blocking ones
fixed (#16/#17 deferred-with-stopgap, others fixed in their
respective commits). Pre-flight + stop-and-surface discipline
held throughout — no workarounds shipped in stdlib.
Tests:
- 966_strings_run drives lib/strings/stringstest.ww via ww run.
15 @test fns: dup (alloc, multibyte), concat (empty, lopsided,
multibyte), trim/ltrim/rtrim incl. 4-byte rune U+1D68A,
hasprefix/hassuffix with (str|rune) incl. multibyte,
byteindex/rbyteindex both arms 1/2/3/4-byte rune coverage,
compare. Cited from ref/hare/strings/+test.ha where vectors
apply.
100/100 ok. 995_self_rebuild stays green (ww2==ww3==ww4 byte-id).
175 lines
4.7 KiB
C
175 lines
4.7 KiB
C
/*
|
|
* 993_ww_ww — phase-10 marker for the ww-side driver port.
|
|
*
|
|
* Diffs the C-built `ww` against the ww-built `ww_ww`. Both drivers
|
|
* orchestrate the same w6c/w6a/w6l, so the resulting binaries must be
|
|
* byte-identical for every program in the corpus. The corpus mixes:
|
|
* - a tiny single-file program (no -I flags)
|
|
* - a multi-import build (selfhost/cmd/wwdump/main.ww), exercising
|
|
* `use` resolution + the -I search path + the libwwrt.a link.
|
|
*
|
|
* Each build runs from a per-driver scratch directory so the
|
|
* intermediate .combined.ww/.s/.o files don't collide.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.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;
|
|
}
|
|
|
|
static const char *
|
|
absbin(void)
|
|
{
|
|
const char *b = getenv("BIN");
|
|
if (!b) b = "out/bin";
|
|
if (b[0] == '/') return b;
|
|
static char buf[2048];
|
|
char cwd[1024];
|
|
if (getcwd(cwd, sizeof cwd) == NULL) return NULL;
|
|
snprintf(buf, sizeof buf, "%s/%s", cwd, b);
|
|
return buf;
|
|
}
|
|
|
|
static int
|
|
slurp_eq(const char *a, const char *b)
|
|
{
|
|
FILE *fa = fopen(a, "rb");
|
|
FILE *fb = fopen(b, "rb");
|
|
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
|
|
int rc = 0;
|
|
for (;;) {
|
|
int ca = fgetc(fa);
|
|
int cb = fgetc(fb);
|
|
if (ca != cb) { rc = -1; break; }
|
|
if (ca == EOF) break;
|
|
}
|
|
fclose(fa); fclose(fb);
|
|
return rc;
|
|
}
|
|
|
|
/* Build `src` via the named driver, expecting output binary `out` in
|
|
* the build directory. `incs` may be a colon-separated list of include
|
|
* dirs (the ww driver accepts -I path1:path2:path3). Returns 0 on
|
|
* success. */
|
|
static int
|
|
build_via(const char *bin, const char *driver, const char *src,
|
|
const char *workdir, const char *incs)
|
|
{
|
|
char cmd[4096];
|
|
if (incs && incs[0]) {
|
|
snprintf(cmd, sizeof cmd,
|
|
"cd %s && %s/%s build -I %s %s 2>/dev/null",
|
|
workdir, bin, driver, incs, src);
|
|
} else {
|
|
snprintf(cmd, sizeof cmd,
|
|
"cd %s && %s/%s build %s 2>/dev/null",
|
|
workdir, bin, driver, src);
|
|
}
|
|
return runwait(cmd);
|
|
}
|
|
|
|
static int
|
|
diff_one(const char *bin, const char *cwd, const char *label,
|
|
const char *src, const char *out_basename,
|
|
const char *incs)
|
|
{
|
|
char dc[64], dw[64];
|
|
snprintf(dc, sizeof dc, "/tmp/ww_d_%d_c", getpid());
|
|
snprintf(dw, sizeof dw, "/tmp/ww_d_%d_w", getpid());
|
|
|
|
char cmd[256];
|
|
snprintf(cmd, sizeof cmd, "rm -rf %s %s && mkdir -p %s %s", dc, dw, dc, dw);
|
|
if (runwait(cmd) != 0) return -1;
|
|
|
|
if (build_via(bin, "ww", src, dc, incs) != 0) {
|
|
fprintf(stderr, "ww_ww FAIL: C ww errored on %s\n", label);
|
|
return -1;
|
|
}
|
|
if (build_via(bin, "ww_ww", src, dw, incs) != 0) {
|
|
fprintf(stderr, "ww_ww FAIL: ww ww errored on %s\n", label);
|
|
return -1;
|
|
}
|
|
|
|
char co[1024], wo[1024];
|
|
snprintf(co, sizeof co, "%s/%s", dc, out_basename);
|
|
snprintf(wo, sizeof wo, "%s/%s", dw, out_basename);
|
|
|
|
int rc = 0;
|
|
if (slurp_eq(co, wo) != 0) {
|
|
fprintf(stderr, "ww_ww FAIL: %s — driver outputs differ\n", label);
|
|
rc = -1;
|
|
}
|
|
snprintf(cmd, sizeof cmd, "rm -rf %s %s", dc, dw);
|
|
runwait(cmd);
|
|
(void)cwd;
|
|
return rc;
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
const char *bin = absbin();
|
|
if (!bin) return 1;
|
|
|
|
char cwd[1024];
|
|
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
|
|
|
|
/* Stage a tiny program in a place both drivers can reach. */
|
|
{
|
|
FILE *f = fopen("/tmp/ww_d_hello.ww", "w");
|
|
if (!f) return 1;
|
|
fputs("use os;\n\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tos.write(1, \"hi\\n\".ptr, 3u64);\n"
|
|
"\treturn 0;\n"
|
|
"};\n", f);
|
|
fclose(f);
|
|
}
|
|
|
|
struct {
|
|
const char *label;
|
|
const char *src; /* may be relative to cwd */
|
|
const char *out; /* basename of expected output */
|
|
const char *incs; /* colon-separated -I list, may be NULL */
|
|
} cases[] = {
|
|
{ "hello", "/tmp/ww_d_hello.ww", "ww_d_hello", "" },
|
|
{ "wwdump", NULL, "main", NULL }, /* filled in below */
|
|
{ NULL, NULL, NULL, NULL },
|
|
};
|
|
|
|
/* wwdump case: absolute paths so the driver finds the imports
|
|
* regardless of the per-driver workdir. */
|
|
static char wwdump_src[2048], wwdump_incs[4096];
|
|
snprintf(wwdump_src, sizeof wwdump_src, "%s/selfhost/cmd/wwdump/main.ww", cwd);
|
|
snprintf(wwdump_incs, sizeof wwdump_incs,
|
|
"%s/lib/ww:%s/lib/ww/lex:%s/lib/ww/parse:%s/lib/encoding/utf8:%s/selfhost/cmd/wcc",
|
|
cwd, cwd, cwd, cwd, cwd);
|
|
cases[1].src = wwdump_src;
|
|
cases[1].incs = wwdump_incs;
|
|
|
|
int fail = 0;
|
|
int n = 0;
|
|
for (int i = 0; cases[i].label; i++) {
|
|
if (diff_one(bin, cwd, cases[i].label, cases[i].src,
|
|
cases[i].out, cases[i].incs) != 0)
|
|
fail++;
|
|
n++;
|
|
}
|
|
unlink("/tmp/ww_d_hello.ww");
|
|
if (fail) {
|
|
fprintf(stderr, "ww_ww: %d/%d diff(s) failed\n", fail, n);
|
|
return 1;
|
|
}
|
|
printf("ww_ww: byte-identical to C ww on %d corpus builds "
|
|
"(single-file + selfhost/wwdump multi-import)\n", n);
|
|
return 0;
|
|
}
|