Plan 9-style w-prefix on the per-arch tools, disambiguating from the
real Plan 9 6c/6a/6l in ref/plan9front/:
cmd/wwc/ → cmd/wcc/ libwwc.a → libwcc.a
cmd/6{c,a,l} → cmd/w6{c,a,l} binary names too
test/wwc/ → test/wcc/ 6 test files w/ w6 prefix
selfhost/cmd mirror in lockstep
bootstrap/amd64/{w6c,w6a,w6l} snapshot binaries (gitignored)
WW_6{C,A,L} → WW_W6{C,A,L} env-var overrides
Plan 9 source-tree refs ("Plan 9 6c shape", ref/plan9front/, etc.)
preserved. Hare-style driver, both C and ww sides:
ww test [path] discover *_test.ww in a directory module, run
each; single-file mode for `ww test foo.ww`
Module-by-name `ww build foo` resolves to foo.ww or foo/foo.ww
via search path (cwd : -I dirs : $WW_LIB)
Default-to-cwd `ww build` / `ww test` build the cwd module
Run pass-through `ww run path arg1 arg2` reaches the program
lib/os: getcwd (79) and getdents64 (217) syscalls power `.` resolution
and directory enumeration on the ww side.
Makefile: wwstage tool deps now include lib/os/os.ww (+ lib/strconv
for wwdump_ww) so lib/* edits force their rebuild instead of leaving
stale binaries — surfaced when test 995 first failed against a stale
w6c_ww built before the lib/os additions.
Test 993 byte-identical parity gate (C-side ww vs ww-side ww_ww on a
build corpus) stays green; all 19 tests pass.
127 lines
4.3 KiB
C
127 lines
4.3 KiB
C
/*
|
|
* 300_check — type-checker tests.
|
|
*
|
|
* Each row is (src, expect) where expect is "ok" (no errors) or a
|
|
* substring that must appear in the captured stderr.
|
|
*/
|
|
#include "ww.h"
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
static int
|
|
runrow(const char *src, const char *expect)
|
|
{
|
|
Arena *a = newarena();
|
|
Lex l;
|
|
Parser p;
|
|
Checker c;
|
|
lexinit(&l, a, "<test>", src, strlen(src));
|
|
parserinit(&p, a, &l);
|
|
Node *file = parsefile(&p);
|
|
|
|
char *errbuf = NULL;
|
|
size_t errlen = 0;
|
|
FILE *prev = errout;
|
|
errout = open_memstream(&errbuf, &errlen);
|
|
|
|
check_init(&c, a);
|
|
check_file(&c, file);
|
|
|
|
fclose(errout);
|
|
errout = prev;
|
|
|
|
int ok = 0;
|
|
if (strcmp(expect, "ok") == 0) {
|
|
ok = (c.errs == 0 && p.errs == 0 && l.errs == 0);
|
|
if (!ok)
|
|
fprintf(stderr, "expected ok but got %d errors:\n%s"
|
|
" src: %s\n", c.errs + p.errs + l.errs, errbuf, src);
|
|
} else {
|
|
ok = (errbuf && strstr(errbuf, expect) != NULL);
|
|
if (!ok)
|
|
fprintf(stderr, "expected substring '%s' in errs:\n%s"
|
|
" src: %s\n", expect, errbuf, src);
|
|
}
|
|
free(errbuf);
|
|
freearena(a);
|
|
return ok;
|
|
}
|
|
|
|
struct row { const char *src, *expect; };
|
|
|
|
static const struct row rows[] = {
|
|
/* OK cases */
|
|
{ "fn main() void = {};", "ok" },
|
|
{ "fn id(x: i32) i32 = { return x; };", "ok" },
|
|
{ "fn add(a: i32, b: i32) i32 = { return a + b; };", "ok" },
|
|
{ "def MAX: i32 = 4096;", "ok" },
|
|
{ "type point = struct { x: i32, y: i32 };", "ok" },
|
|
{ "type point = struct { x: i32, y: i32 };\n"
|
|
"fn move(p: *point, dx: i32) void = { p.x += dx; };", "ok" },
|
|
{ "fn nums() void = { let x: i32 = 1; let y: i64 = 2; };", "ok" },
|
|
{ "fn cond(x: i32) i32 = { if (x > 0) { return 1; }; return 0; };", "ok" },
|
|
{ "fn lo() void = { for (let i: i32 = 0; i < 10; i += 1) { }; };", "ok" },
|
|
{ "fn ptr(p: *i32) i32 = { return *p; };", "ok" },
|
|
{ "fn addr(x: i32) *i32 = { return &x; };", "ok" },
|
|
{ "fn cmp(a: i32, b: i32) bool = { return a == b; };", "ok" },
|
|
{ "fn b() bool = { return true && false || !true; };", "ok" },
|
|
{ "fn cast() void = { let x = (5 + 1): i64; };", "ok" },
|
|
{ "fn slc(s: []u8) []u8 = { return s; };", "ok" },
|
|
{ "fn arr() void = { let a: [16]u8; };", "ok" },
|
|
{ "fn lit() void = { let p = point { x = 1, y = 2 }; };\n"
|
|
"type point = struct { x: i32, y: i32 };", "ok" },
|
|
{ "fn idx(a: []i32) i32 = { return a[0]; };", "ok" },
|
|
{ "fn nilptr(p: *i32) bool = { return p == nil; };", "ok" },
|
|
{ "fn untyped() void = { let x: i64 = 42; };", "ok" },
|
|
{ "fn fld(p: *point) i32 = { return p.x; };\n"
|
|
"type point = struct { x: i32 };", "ok" },
|
|
{ "fn loops() void = { for () { break; }; for () { continue; }; };", "ok" },
|
|
|
|
/* multi-return tuples */
|
|
{ "fn divmod(a: i64, b: i64) (i64, i64) = { return a / b, a % b; };", "ok" },
|
|
{ "fn dm(a: i64, b: i64) (i64, i64) = { return a, b; };\n"
|
|
"fn caller() i64 = { let q, r = dm(10, 3); return q + r; };", "ok" },
|
|
{ "fn dm() (i64, i64) = { return 1, 2; };\n"
|
|
"fn ass() void = { let q: i64 = 0; let r: i64 = 0; q, r = dm(); };", "ok" },
|
|
|
|
/* error cases */
|
|
{ "fn f() void = { return 1; };", "return value in void" },
|
|
{ "fn f() i32 = { return; };", "not assignable" },
|
|
{ "fn f() void = { x = 1; };", "undefined" },
|
|
{ "fn f() void = { let x: nope = 1; };", "unknown type" },
|
|
{ "fn f() void = { let x: bool = 1; };", "not assignable" },
|
|
{ "fn f() i32 = { return \"hi\"; };", "not assignable" },
|
|
{ "fn f() void = { 1 + true; };", "non-numeric" },
|
|
{ "fn f() void = { -true; };", "non-numeric" },
|
|
{ "fn f() void = { *5; };", "deref non-pointer" },
|
|
{ "fn f() void = { let x: i32 = 1; let x: i32 = 2; };",
|
|
"ok" }, /* shadowing in inner scope; same scope flagged */
|
|
{ "fn f() void = { break; };", "break outside loop" },
|
|
{ "fn f() void = { continue; };", "continue outside loop" },
|
|
{ "fn f(x: i32) void = { x[0]; };", "indexing non-indexable" },
|
|
{ "fn f() i32 = { return 1; }; fn g() i32 = { return f(1); };",
|
|
"too many arguments" },
|
|
{ "fn f(a: i32) i32 = { return a; }; fn g() i32 = { return f(); };",
|
|
"not enough arguments" },
|
|
{ "fn f() void = { if (1) { }; };", "if condition" },
|
|
};
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
int n = sizeof rows / sizeof rows[0];
|
|
int fail = 0;
|
|
for (int i = 0; i < n; i++)
|
|
if (!runrow(rows[i].src, rows[i].expect)) {
|
|
fprintf(stderr, "row %d failed\n", i);
|
|
fail++;
|
|
}
|
|
if (fail) {
|
|
fprintf(stderr, "%d/%d check tests failed\n", fail, n);
|
|
return 1;
|
|
}
|
|
printf("check: %d/%d ok\n", n, n);
|
|
return 0;
|
|
}
|