Build w6a and w6l from package-main directories and expose the wcc backend through a narrow package API so w6c and wwdump no longer import implementation files. Retarget the remaining load-bearing fixtures and example sources to directory packages; retain the one intentional flat compiler collision as an explicitly composed raw unit.
70 lines
2.0 KiB
Plaintext
70 lines
2.0 KiB
Plaintext
// Port of cmd/w6a/lex.c.
|
|
|
|
package main;
|
|
|
|
export fn isidstart(c: i32) bool = {
|
|
if (c == 95) { return true; };
|
|
if (c >= 65) { if (c <= 90) { return true; }; }; // A-Z
|
|
if (c >= 97) { if (c <= 122) { return true; }; }; // a-z
|
|
return false;
|
|
};
|
|
|
|
export fn isidcont(c: i32) bool = {
|
|
if (isidstart(c)) { return true; };
|
|
if (c >= 48) { if (c <= 57) { return true; }; }; // 0-9
|
|
if (c == 46) { return true; }; // .
|
|
return false;
|
|
};
|
|
|
|
export fn parsenum(p: *u8, n: u64) (i64, u64) = {
|
|
// strtoll(s, end, 0) semantics, matching the C twin cmd/w6a/lex.c:30:
|
|
// skip leading whitespace, optional sign, base-0 prefix detection
|
|
// (0x -> hex, leading 0 -> octal, else decimal). w6c never emits the
|
|
// `$ 5` / `$08` edge shapes; this aligns the hand-written-asm path
|
|
// with cstage so the two assemblers agree byte-for-byte (#62).
|
|
let i: u64 = 0u64;
|
|
for (i < n) {
|
|
if (p[i] != 32u8) { if (p[i] != 9u8) { break; }; };
|
|
i += 1u64;
|
|
};
|
|
let neg: bool = false;
|
|
if (i < n) {
|
|
if (p[i] == 45u8) { neg = true; i += 1u64; }
|
|
else { if (p[i] == 43u8) { i += 1u64; }; };
|
|
};
|
|
let base: i64 = 10i64;
|
|
if (i < n) {
|
|
if (p[i] == 48u8) { // leading '0' -> octal, unless '0x'/'0X'
|
|
if (i + 1u64 < n) {
|
|
if (p[i + 1u64] == 120u8) { base = 16i64; i += 2u64; }
|
|
else { if (p[i + 1u64] == 88u8) { base = 16i64; i += 2u64; }
|
|
else { base = 8i64; i += 1u64; }; };
|
|
} else { base = 8i64; i += 1u64; };
|
|
};
|
|
};
|
|
let v: i64 = 0i64;
|
|
let scan: bool = true;
|
|
for (scan) {
|
|
if (i >= n) { scan = false; }
|
|
else {
|
|
let c: u8 = p[i];
|
|
let d: i64 = -1i64;
|
|
if (c >= 48u8) { if (c <= 57u8) { d = (c - 48u8): i64; }; };
|
|
if (d < 0i64) {
|
|
if (base == 16i64) {
|
|
if (c >= 97u8) { if (c <= 102u8) { d = (c - 97u8): i64 + 10i64; }; };
|
|
if (c >= 65u8) { if (c <= 70u8) { d = (c - 65u8): i64 + 10i64; }; };
|
|
};
|
|
};
|
|
if (d < 0i64) { scan = false; }
|
|
else { if (d >= base) { scan = false; }
|
|
else {
|
|
v = v * base + d;
|
|
i += 1u64;
|
|
}; };
|
|
};
|
|
};
|
|
if (neg) { v = -v; };
|
|
return v, i;
|
|
};
|