convert more std lib files to postfix pointer deref

This commit is contained in:
Andrew Kelley 2018-05-16 00:43:28 -04:00
parent 5cfabdd493
commit 288fc3a8d3
6 changed files with 1203 additions and 1370 deletions

View File

@ -14,9 +14,8 @@ pub fn assertEqualHash(comptime Hasher: var, comptime expected: []const u8, inpu
pub fn assertEqual(comptime expected: []const u8, input: []const u8) void {
var expected_bytes: [expected.len / 2]u8 = undefined;
for (expected_bytes) |*r, i| {
*r = fmt.parseInt(u8, expected[2*i .. 2*i+2], 16) catch unreachable;
r.* = fmt.parseInt(u8, expected[2 * i .. 2 * i + 2], 16) catch unreachable;
}
debug.assert(mem.eql(u8, expected_bytes, input));
}

View File

@ -86,7 +86,7 @@ pub fn errol3(value: f64, buffer: []u8) FloatDecimal {
const data = enum3_data[i];
const digits = buffer[1..data.str.len + 1];
mem.copy(u8, digits, data.str);
return FloatDecimal {
return FloatDecimal{
.digits = digits,
.exp = data.exp,
};
@ -105,7 +105,6 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
return errolFixed(val, buffer);
}
// normalize the midpoint
const e = math.frexp(val).exponent;
@ -137,11 +136,11 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
}
// compute boundaries
var high = HP {
var high = HP{
.val = mid.val,
.off = mid.off + (fpnext(val) - val) * lten * ten / 2.0,
};
var low = HP {
var low = HP{
.val = mid.val,
.off = mid.off + (fpprev(val) - val) * lten * ten / 2.0,
};
@ -171,15 +170,12 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
var buf_index: usize = 1;
while (true) {
var hdig = u8(math.floor(high.val));
if ((high.val == f64(hdig)) and (high.off < 0))
hdig -= 1;
if ((high.val == f64(hdig)) and (high.off < 0)) hdig -= 1;
var ldig = u8(math.floor(low.val));
if ((low.val == f64(ldig)) and (low.off < 0))
ldig -= 1;
if ((low.val == f64(ldig)) and (low.off < 0)) ldig -= 1;
if (ldig != hdig)
break;
if (ldig != hdig) break;
buffer[buf_index] = hdig + '0';
buf_index += 1;
@ -191,13 +187,12 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
const tmp = (high.val + low.val) / 2.0;
var mdig = u8(math.floor(tmp + 0.5));
if ((f64(mdig) - tmp) == 0.5 and (mdig & 0x1) != 0)
mdig -= 1;
if ((f64(mdig) - tmp) == 0.5 and (mdig & 0x1) != 0) mdig -= 1;
buffer[buf_index] = mdig + '0';
buf_index += 1;
return FloatDecimal {
return FloatDecimal{
.digits = buffer[1..buf_index],
.exp = exp,
};
@ -235,7 +230,7 @@ fn hpProd(in: &const HP, val: f64) HP {
const p = in.val * val;
const e = ((hi * hi2 - p) + lo * hi2 + hi * lo2) + lo * lo2;
return HP {
return HP{
.val = p,
.off = in.off * val + e,
};
@ -246,8 +241,8 @@ fn hpProd(in: &const HP, val: f64) HP {
/// @hi: The high bits.
/// @lo: The low bits.
fn split(val: f64, hi: &f64, lo: &f64) void {
*hi = gethi(val);
*lo = val - *hi;
hi.* = gethi(val);
lo.* = val - hi.*;
}
fn gethi(in: f64) f64 {
@ -301,7 +296,6 @@ fn hpMul10(hp: &HP) void {
hpNormalize(hp);
}
/// Integer conversion algorithm, guaranteed correct, optimal, and best.
/// @val: The val.
/// @buf: The output buffer.
@ -343,8 +337,7 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
}
const m64 = @truncate(u64, @divTrunc(mid, x));
if (lf != hf)
mi += 19;
if (lf != hf) mi += 19;
var buf_index = u64toa(m64, buffer) - 1;
@ -354,7 +347,7 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
buf_index += 1;
}
return FloatDecimal {
return FloatDecimal{
.digits = buffer[0..buf_index],
.exp = i32(buf_index) + mi,
};
@ -396,25 +389,24 @@ fn errolFixed(val: f64, buffer: []u8) FloatDecimal {
buffer[j] = u8(mdig + '0');
j += 1;
if(hdig != ldig or j > 50)
break;
if (hdig != ldig or j > 50) break;
}
if (mid > 0.5) {
buffer[j-1] += 1;
} else if ((mid == 0.5) and (buffer[j-1] & 0x1) != 0) {
buffer[j-1] += 1;
buffer[j - 1] += 1;
} else if ((mid == 0.5) and (buffer[j - 1] & 0x1) != 0) {
buffer[j - 1] += 1;
}
} else {
while (buffer[j-1] == '0') {
buffer[j-1] = 0;
while (buffer[j - 1] == '0') {
buffer[j - 1] = 0;
j -= 1;
}
}
buffer[j] = 0;
return FloatDecimal {
return FloatDecimal{
.digits = buffer[0..j],
.exp = exp,
};
@ -587,7 +579,7 @@ fn u64toa(value_param: u64, buffer: []u8) usize {
buffer[buf_index] = c_digits_lut[d8 + 1];
buf_index += 1;
} else {
const a = u32(value / kTen16); // 1 to 1844
const a = u32(value / kTen16); // 1 to 1844
value %= kTen16;
if (a < 10) {
@ -686,7 +678,6 @@ fn fpeint(from: f64) u128 {
return u128(1) << @truncate(u7, (bits >> 52) -% 1023);
}
/// Given two different integers with the same length in terms of the number
/// of decimal digits, index the digits from the right-most position starting
/// from zero, find the first index where the digits in the two integers
@ -713,7 +704,6 @@ fn mismatch10(a: u64, b: u64) i32 {
a_copy /= 10;
b_copy /= 10;
if (a_copy == b_copy)
return i;
if (a_copy == b_copy) return i;
}
}

View File

@ -10,33 +10,56 @@ pub const STDIN_FILENO = 0;
pub const STDOUT_FILENO = 1;
pub const STDERR_FILENO = 2;
pub const PROT_NONE = 0x00; /// [MC2] no permissions
pub const PROT_READ = 0x01; /// [MC2] pages can be read
pub const PROT_WRITE = 0x02; /// [MC2] pages can be written
pub const PROT_EXEC = 0x04; /// [MC2] pages can be executed
/// [MC2] no permissions
pub const PROT_NONE = 0x00;
/// [MC2] pages can be read
pub const PROT_READ = 0x01;
/// [MC2] pages can be written
pub const PROT_WRITE = 0x02;
/// [MC2] pages can be executed
pub const PROT_EXEC = 0x04;
pub const MAP_ANONYMOUS = 0x1000; /// allocated from memory, swap space
pub const MAP_FILE = 0x0000; /// map from file (default)
pub const MAP_FIXED = 0x0010; /// interpret addr exactly
pub const MAP_HASSEMAPHORE = 0x0200; /// region may contain semaphores
pub const MAP_PRIVATE = 0x0002; /// changes are private
pub const MAP_SHARED = 0x0001; /// share changes
pub const MAP_NOCACHE = 0x0400; /// don't cache pages for this mapping
pub const MAP_NORESERVE = 0x0040; /// don't reserve needed swap area
/// allocated from memory, swap space
pub const MAP_ANONYMOUS = 0x1000;
/// map from file (default)
pub const MAP_FILE = 0x0000;
/// interpret addr exactly
pub const MAP_FIXED = 0x0010;
/// region may contain semaphores
pub const MAP_HASSEMAPHORE = 0x0200;
/// changes are private
pub const MAP_PRIVATE = 0x0002;
/// share changes
pub const MAP_SHARED = 0x0001;
/// don't cache pages for this mapping
pub const MAP_NOCACHE = 0x0400;
/// don't reserve needed swap area
pub const MAP_NORESERVE = 0x0040;
pub const MAP_FAILED = @maxValue(usize);
pub const WNOHANG = 0x00000001; /// [XSI] no hang in wait/no child to reap
pub const WUNTRACED = 0x00000002; /// [XSI] notify on stop, untraced child
/// [XSI] no hang in wait/no child to reap
pub const WNOHANG = 0x00000001;
/// [XSI] notify on stop, untraced child
pub const WUNTRACED = 0x00000002;
pub const SA_ONSTACK = 0x0001; /// take signal on signal stack
pub const SA_RESTART = 0x0002; /// restart system on signal return
pub const SA_RESETHAND = 0x0004; /// reset to SIG_DFL when taking signal
pub const SA_NOCLDSTOP = 0x0008; /// do not generate SIGCHLD on child stop
pub const SA_NODEFER = 0x0010; /// don't mask the signal we're delivering
pub const SA_NOCLDWAIT = 0x0020; /// don't keep zombies around
pub const SA_SIGINFO = 0x0040; /// signal handler with SA_SIGINFO args
pub const SA_USERTRAMP = 0x0100; /// do not bounce off kernel's sigtramp
pub const SA_64REGSET = 0x0200; /// signal handler with SA_SIGINFO args with 64bit regs information
/// take signal on signal stack
pub const SA_ONSTACK = 0x0001;
/// restart system on signal return
pub const SA_RESTART = 0x0002;
/// reset to SIG_DFL when taking signal
pub const SA_RESETHAND = 0x0004;
/// do not generate SIGCHLD on child stop
pub const SA_NOCLDSTOP = 0x0008;
/// don't mask the signal we're delivering
pub const SA_NODEFER = 0x0010;
/// don't keep zombies around
pub const SA_NOCLDWAIT = 0x0020;
/// signal handler with SA_SIGINFO args
pub const SA_SIGINFO = 0x0040;
/// do not bounce off kernel's sigtramp
pub const SA_USERTRAMP = 0x0100;
/// signal handler with SA_SIGINFO args with 64bit regs information
pub const SA_64REGSET = 0x0200;
pub const O_LARGEFILE = 0x0000;
pub const O_PATH = 0x0000;
@ -46,20 +69,34 @@ pub const X_OK = 1;
pub const W_OK = 2;
pub const R_OK = 4;
pub const O_RDONLY = 0x0000; /// open for reading only
pub const O_WRONLY = 0x0001; /// open for writing only
pub const O_RDWR = 0x0002; /// open for reading and writing
pub const O_NONBLOCK = 0x0004; /// do not block on open or for data to become available
pub const O_APPEND = 0x0008; /// append on each write
pub const O_CREAT = 0x0200; /// create file if it does not exist
pub const O_TRUNC = 0x0400; /// truncate size to 0
pub const O_EXCL = 0x0800; /// error if O_CREAT and the file exists
pub const O_SHLOCK = 0x0010; /// atomically obtain a shared lock
pub const O_EXLOCK = 0x0020; /// atomically obtain an exclusive lock
pub const O_NOFOLLOW = 0x0100; /// do not follow symlinks
pub const O_SYMLINK = 0x200000; /// allow open of symlinks
pub const O_EVTONLY = 0x8000; /// descriptor requested for event notifications only
pub const O_CLOEXEC = 0x1000000; /// mark as close-on-exec
/// open for reading only
pub const O_RDONLY = 0x0000;
/// open for writing only
pub const O_WRONLY = 0x0001;
/// open for reading and writing
pub const O_RDWR = 0x0002;
/// do not block on open or for data to become available
pub const O_NONBLOCK = 0x0004;
/// append on each write
pub const O_APPEND = 0x0008;
/// create file if it does not exist
pub const O_CREAT = 0x0200;
/// truncate size to 0
pub const O_TRUNC = 0x0400;
/// error if O_CREAT and the file exists
pub const O_EXCL = 0x0800;
/// atomically obtain a shared lock
pub const O_SHLOCK = 0x0010;
/// atomically obtain an exclusive lock
pub const O_EXLOCK = 0x0020;
/// do not follow symlinks
pub const O_NOFOLLOW = 0x0100;
/// allow open of symlinks
pub const O_SYMLINK = 0x200000;
/// descriptor requested for event notifications only
pub const O_EVTONLY = 0x8000;
/// mark as close-on-exec
pub const O_CLOEXEC = 0x1000000;
pub const O_ACCMODE = 3;
pub const O_ALERT = 536870912;
@ -87,52 +124,102 @@ pub const DT_LNK = 10;
pub const DT_SOCK = 12;
pub const DT_WHT = 14;
pub const SIG_BLOCK = 1; /// block specified signal set
pub const SIG_UNBLOCK = 2; /// unblock specified signal set
pub const SIG_SETMASK = 3; /// set specified signal set
/// block specified signal set
pub const SIG_BLOCK = 1;
/// unblock specified signal set
pub const SIG_UNBLOCK = 2;
/// set specified signal set
pub const SIG_SETMASK = 3;
pub const SIGHUP = 1; /// hangup
pub const SIGINT = 2; /// interrupt
pub const SIGQUIT = 3; /// quit
pub const SIGILL = 4; /// illegal instruction (not reset when caught)
pub const SIGTRAP = 5; /// trace trap (not reset when caught)
pub const SIGABRT = 6; /// abort()
pub const SIGPOLL = 7; /// pollable event ([XSR] generated, not supported)
pub const SIGIOT = SIGABRT; /// compatibility
pub const SIGEMT = 7; /// EMT instruction
pub const SIGFPE = 8; /// floating point exception
pub const SIGKILL = 9; /// kill (cannot be caught or ignored)
pub const SIGBUS = 10; /// bus error
pub const SIGSEGV = 11; /// segmentation violation
pub const SIGSYS = 12; /// bad argument to system call
pub const SIGPIPE = 13; /// write on a pipe with no one to read it
pub const SIGALRM = 14; /// alarm clock
pub const SIGTERM = 15; /// software termination signal from kill
pub const SIGURG = 16; /// urgent condition on IO channel
pub const SIGSTOP = 17; /// sendable stop signal not from tty
pub const SIGTSTP = 18; /// stop signal from tty
pub const SIGCONT = 19; /// continue a stopped process
pub const SIGCHLD = 20; /// to parent on child stop or exit
pub const SIGTTIN = 21; /// to readers pgrp upon background tty read
pub const SIGTTOU = 22; /// like TTIN for output if (tp->t_local&LTOSTOP)
pub const SIGIO = 23; /// input/output possible signal
pub const SIGXCPU = 24; /// exceeded CPU time limit
pub const SIGXFSZ = 25; /// exceeded file size limit
pub const SIGVTALRM = 26; /// virtual time alarm
pub const SIGPROF = 27; /// profiling time alarm
pub const SIGWINCH = 28; /// window size changes
pub const SIGINFO = 29; /// information request
pub const SIGUSR1 = 30; /// user defined signal 1
pub const SIGUSR2 = 31; /// user defined signal 2
/// hangup
pub const SIGHUP = 1;
/// interrupt
pub const SIGINT = 2;
/// quit
pub const SIGQUIT = 3;
/// illegal instruction (not reset when caught)
pub const SIGILL = 4;
/// trace trap (not reset when caught)
pub const SIGTRAP = 5;
/// abort()
pub const SIGABRT = 6;
/// pollable event ([XSR] generated, not supported)
pub const SIGPOLL = 7;
/// compatibility
pub const SIGIOT = SIGABRT;
/// EMT instruction
pub const SIGEMT = 7;
/// floating point exception
pub const SIGFPE = 8;
/// kill (cannot be caught or ignored)
pub const SIGKILL = 9;
/// bus error
pub const SIGBUS = 10;
/// segmentation violation
pub const SIGSEGV = 11;
/// bad argument to system call
pub const SIGSYS = 12;
/// write on a pipe with no one to read it
pub const SIGPIPE = 13;
/// alarm clock
pub const SIGALRM = 14;
/// software termination signal from kill
pub const SIGTERM = 15;
/// urgent condition on IO channel
pub const SIGURG = 16;
/// sendable stop signal not from tty
pub const SIGSTOP = 17;
/// stop signal from tty
pub const SIGTSTP = 18;
/// continue a stopped process
pub const SIGCONT = 19;
/// to parent on child stop or exit
pub const SIGCHLD = 20;
/// to readers pgrp upon background tty read
pub const SIGTTIN = 21;
/// like TTIN for output if (tp->t_local&LTOSTOP)
pub const SIGTTOU = 22;
/// input/output possible signal
pub const SIGIO = 23;
/// exceeded CPU time limit
pub const SIGXCPU = 24;
/// exceeded file size limit
pub const SIGXFSZ = 25;
/// virtual time alarm
pub const SIGVTALRM = 26;
/// profiling time alarm
pub const SIGPROF = 27;
/// window size changes
pub const SIGWINCH = 28;
/// information request
pub const SIGINFO = 29;
/// user defined signal 1
pub const SIGUSR1 = 30;
/// user defined signal 2
pub const SIGUSR2 = 31;
fn wstatus(x: i32) i32 { return x & 0o177; }
fn wstatus(x: i32) i32 {
return x & 0o177;
}
const wstopped = 0o177;
pub fn WEXITSTATUS(x: i32) i32 { return x >> 8; }
pub fn WTERMSIG(x: i32) i32 { return wstatus(x); }
pub fn WSTOPSIG(x: i32) i32 { return x >> 8; }
pub fn WIFEXITED(x: i32) bool { return wstatus(x) == 0; }
pub fn WIFSTOPPED(x: i32) bool { return wstatus(x) == wstopped and WSTOPSIG(x) != 0x13; }
pub fn WIFSIGNALED(x: i32) bool { return wstatus(x) != wstopped and wstatus(x) != 0; }
pub fn WEXITSTATUS(x: i32) i32 {
return x >> 8;
}
pub fn WTERMSIG(x: i32) i32 {
return wstatus(x);
}
pub fn WSTOPSIG(x: i32) i32 {
return x >> 8;
}
pub fn WIFEXITED(x: i32) bool {
return wstatus(x) == 0;
}
pub fn WIFSTOPPED(x: i32) bool {
return wstatus(x) == wstopped and WSTOPSIG(x) != 0x13;
}
pub fn WIFSIGNALED(x: i32) bool {
return wstatus(x) != wstopped and wstatus(x) != 0;
}
/// Get the errno from a syscall return value, or 0 for no error.
pub fn getErrno(r: usize) usize {
@ -184,11 +271,8 @@ pub fn write(fd: i32, buf: &const u8, nbyte: usize) usize {
return errnoWrap(c.write(fd, @ptrCast(&const c_void, buf), nbyte));
}
pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: u32, fd: i32,
offset: isize) usize
{
const ptr_result = c.mmap(@ptrCast(&c_void, address), length,
@bitCast(c_int, c_uint(prot)), @bitCast(c_int, c_uint(flags)), fd, offset);
pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
const ptr_result = c.mmap(@ptrCast(&c_void, address), length, @bitCast(c_int, c_uint(prot)), @bitCast(c_int, c_uint(flags)), fd, offset);
const isize_result = @bitCast(isize, @ptrToInt(ptr_result));
return errnoWrap(isize_result);
}
@ -202,7 +286,7 @@ pub fn unlink(path: &const u8) usize {
}
pub fn getcwd(buf: &u8, size: usize) usize {
return if (c.getcwd(buf, size) == null) @bitCast(usize, -isize(*c._errno())) else 0;
return if (c.getcwd(buf, size) == null) @bitCast(usize, -isize(c._errno().*)) else 0;
}
pub fn waitpid(pid: i32, status: &i32, options: u32) usize {
@ -223,7 +307,6 @@ pub fn pipe(fds: &[2]i32) usize {
return errnoWrap(c.pipe(@ptrCast(&c_int, fds)));
}
pub fn getdirentries64(fd: i32, buf_ptr: &u8, buf_len: usize, basep: &i64) usize {
return errnoWrap(@bitCast(isize, c.__getdirentries64(fd, buf_ptr, buf_len, basep)));
}
@ -269,7 +352,7 @@ pub fn nanosleep(req: &const timespec, rem: ?&timespec) usize {
}
pub fn realpath(noalias filename: &const u8, noalias resolved_name: &u8) usize {
return if (c.realpath(filename, resolved_name) == null) @bitCast(usize, -isize(*c._errno())) else 0;
return if (c.realpath(filename, resolved_name) == null) @bitCast(usize, -isize(c._errno().*)) else 0;
}
pub fn setreuid(ruid: u32, euid: u32) usize {
@ -287,8 +370,8 @@ pub fn sigprocmask(flags: u32, noalias set: &const sigset_t, noalias oldset: ?&s
pub fn sigaction(sig: u5, noalias act: &const Sigaction, noalias oact: ?&Sigaction) usize {
assert(sig != SIGKILL);
assert(sig != SIGSTOP);
var cact = c.Sigaction {
.handler = @ptrCast(extern fn(c_int)void, act.handler),
var cact = c.Sigaction{
.handler = @ptrCast(extern fn(c_int) void, act.handler),
.sa_flags = @bitCast(c_int, act.flags),
.sa_mask = act.mask,
};
@ -298,8 +381,8 @@ pub fn sigaction(sig: u5, noalias act: &const Sigaction, noalias oact: ?&Sigacti
return result;
}
if (oact) |old| {
*old = Sigaction {
.handler = @ptrCast(extern fn(i32)void, coact.handler),
old.* = Sigaction{
.handler = @ptrCast(extern fn(i32) void, coact.handler),
.flags = @bitCast(u32, coact.sa_flags),
.mask = coact.sa_mask,
};
@ -319,23 +402,22 @@ pub const sockaddr = c.sockaddr;
/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.
pub const Sigaction = struct {
handler: extern fn(i32)void,
handler: extern fn(i32) void,
mask: sigset_t,
flags: u32,
};
pub fn sigaddset(set: &sigset_t, signo: u5) void {
*set |= u32(1) << (signo - 1);
set.* |= u32(1) << (signo - 1);
}
/// Takes the return value from a syscall and formats it back in the way
/// that the kernel represents it to libc. Errno was a mistake, let's make
/// it go away forever.
fn errnoWrap(value: isize) usize {
return @bitCast(usize, if (value == -1) -isize(*c._errno()) else value);
return @bitCast(usize, if (value == -1) -isize(c._errno().*) else value);
}
pub const timezone = c.timezone;
pub const timeval = c.timeval;
pub const mach_timebase_info_data = c.mach_timebase_info_data;

View File

@ -40,7 +40,7 @@ pub const Tree = struct {
};
pub fn tokenLocationPtr(self: &Tree, start_index: usize, token: &const Token) Location {
var loc = Location {
var loc = Location{
.line = 0,
.column = 0,
.line_start = start_index,
@ -71,7 +71,6 @@ pub const Tree = struct {
pub fn dump(self: &Tree) void {
self.root_node.base.dump(0);
}
};
pub const Error = union(enum) {
@ -95,7 +94,7 @@ pub const Error = union(enum) {
ExpectedCommaOrEnd: ExpectedCommaOrEnd,
pub fn render(self: &Error, tokens: &Tree.TokenList, stream: var) !void {
switch (*self) {
switch (self.*) {
// TODO https://github.com/zig-lang/zig/issues/683
@TagType(Error).InvalidToken => |*x| return x.render(tokens, stream),
@TagType(Error).ExpectedVarDeclOrFn => |*x| return x.render(tokens, stream),
@ -119,7 +118,7 @@ pub const Error = union(enum) {
}
pub fn loc(self: &Error) TokenIndex {
switch (*self) {
switch (self.*) {
// TODO https://github.com/zig-lang/zig/issues/683
@TagType(Error).InvalidToken => |x| return x.token,
@TagType(Error).ExpectedVarDeclOrFn => |x| return x.token,
@ -144,15 +143,12 @@ pub const Error = union(enum) {
pub const InvalidToken = SingleTokenError("Invalid token {}");
pub const ExpectedVarDeclOrFn = SingleTokenError("Expected variable declaration or function, found {}");
pub const ExpectedAggregateKw = SingleTokenError("Expected " ++
@tagName(Token.Id.Keyword_struct) ++ ", " ++ @tagName(Token.Id.Keyword_union) ++ ", or " ++
@tagName(Token.Id.Keyword_enum) ++ ", found {}");
pub const ExpectedAggregateKw = SingleTokenError("Expected " ++ @tagName(Token.Id.Keyword_struct) ++ ", " ++ @tagName(Token.Id.Keyword_union) ++ ", or " ++ @tagName(Token.Id.Keyword_enum) ++ ", found {}");
pub const ExpectedEqOrSemi = SingleTokenError("Expected '=' or ';', found {}");
pub const ExpectedSemiOrLBrace = SingleTokenError("Expected ';' or '{{', found {}");
pub const ExpectedLabelable = SingleTokenError("Expected 'while', 'for', 'inline', 'suspend', or '{{', found {}");
pub const ExpectedInlinable = SingleTokenError("Expected 'while' or 'for', found {}");
pub const ExpectedAsmOutputReturnOrType = SingleTokenError("Expected '->' or " ++
@tagName(Token.Id.Identifier) ++ ", found {}");
pub const ExpectedAsmOutputReturnOrType = SingleTokenError("Expected '->' or " ++ @tagName(Token.Id.Identifier) ++ ", found {}");
pub const ExpectedSliceOrRBracket = SingleTokenError("Expected ']' or '..', found {}");
pub const ExpectedPrimaryExpr = SingleTokenError("Expected primary expression, found {}");
@ -165,8 +161,7 @@ pub const Error = union(enum) {
node: &Node,
pub fn render(self: &ExpectedCall, tokens: &Tree.TokenList, stream: var) !void {
return stream.print("expected " ++ @tagName(@TagType(Node.SuffixOp.Op).Call) ++ ", found {}",
@tagName(self.node.id));
return stream.print("expected " ++ @tagName(@TagType(Node.SuffixOp.Op).Call) ++ ", found {}", @tagName(self.node.id));
}
};
@ -174,8 +169,7 @@ pub const Error = union(enum) {
node: &Node,
pub fn render(self: &ExpectedCallOrFnProto, tokens: &Tree.TokenList, stream: var) !void {
return stream.print("expected " ++ @tagName(@TagType(Node.SuffixOp.Op).Call) ++ " or " ++
@tagName(Node.Id.FnProto) ++ ", found {}", @tagName(self.node.id));
return stream.print("expected " ++ @tagName(@TagType(Node.SuffixOp.Op).Call) ++ " or " ++ @tagName(Node.Id.FnProto) ++ ", found {}", @tagName(self.node.id));
}
};
@ -445,17 +439,17 @@ pub const Node = struct {
pub fn iterate(self: &Root, index: usize) ?&Node {
if (index < self.decls.len) {
return *self.decls.at(index);
return self.decls.at(index).*;
}
return null;
}
pub fn firstToken(self: &Root) TokenIndex {
return if (self.decls.len == 0) self.eof_token else (*self.decls.at(0)).firstToken();
return if (self.decls.len == 0) self.eof_token else (self.decls.at(0).*).firstToken();
}
pub fn lastToken(self: &Root) TokenIndex {
return if (self.decls.len == 0) self.eof_token else (*self.decls.at(self.decls.len - 1)).lastToken();
return if (self.decls.len == 0) self.eof_token else (self.decls.at(self.decls.len - 1).*).lastToken();
}
};
@ -545,7 +539,7 @@ pub const Node = struct {
pub fn iterate(self: &ErrorSetDecl, index: usize) ?&Node {
var i = index;
if (i < self.decls.len) return *self.decls.at(i);
if (i < self.decls.len) return self.decls.at(i).*;
i -= self.decls.len;
return null;
@ -598,10 +592,10 @@ pub const Node = struct {
i -= 1;
},
InitArg.None,
InitArg.Enum => { }
InitArg.Enum => {},
}
if (i < self.fields_and_decls.len) return *self.fields_and_decls.at(i);
if (i < self.fields_and_decls.len) return self.fields_and_decls.at(i).*;
i -= self.fields_and_decls.len;
return null;
@ -814,7 +808,7 @@ pub const Node = struct {
i -= 1;
}
if (i < self.params.len) return *self.params.at(self.params.len - i - 1);
if (i < self.params.len) return self.params.at(self.params.len - i - 1).*;
i -= self.params.len;
if (self.align_expr) |align_expr| {
@ -839,7 +833,6 @@ pub const Node = struct {
i -= 1;
}
return null;
}
@ -934,7 +927,7 @@ pub const Node = struct {
pub fn iterate(self: &Block, index: usize) ?&Node {
var i = index;
if (i < self.statements.len) return *self.statements.at(i);
if (i < self.statements.len) return self.statements.at(i).*;
i -= self.statements.len;
return null;
@ -1119,6 +1112,7 @@ pub const Node = struct {
base: Node,
switch_token: TokenIndex,
expr: &Node,
/// these can be SwitchCase nodes or LineComment nodes
cases: CaseList,
rbrace: TokenIndex,
@ -1131,7 +1125,7 @@ pub const Node = struct {
if (i < 1) return self.expr;
i -= 1;
if (i < self.cases.len) return *self.cases.at(i);
if (i < self.cases.len) return self.cases.at(i).*;
i -= self.cases.len;
return null;
@ -1157,7 +1151,7 @@ pub const Node = struct {
pub fn iterate(self: &SwitchCase, index: usize) ?&Node {
var i = index;
if (i < self.items.len) return *self.items.at(i);
if (i < self.items.len) return self.items.at(i).*;
i -= self.items.len;
if (self.payload) |payload| {
@ -1172,7 +1166,7 @@ pub const Node = struct {
}
pub fn firstToken(self: &SwitchCase) TokenIndex {
return (*self.items.at(0)).firstToken();
return (self.items.at(0).*).firstToken();
}
pub fn lastToken(self: &SwitchCase) TokenIndex {
@ -1616,7 +1610,7 @@ pub const Node = struct {
switch (self.op) {
@TagType(Op).Call => |*call_info| {
if (i < call_info.params.len) return *call_info.params.at(i);
if (i < call_info.params.len) return call_info.params.at(i).*;
i -= call_info.params.len;
},
Op.ArrayAccess => |index_expr| {
@ -1633,11 +1627,11 @@ pub const Node = struct {
}
},
Op.ArrayInitializer => |*exprs| {
if (i < exprs.len) return *exprs.at(i);
if (i < exprs.len) return exprs.at(i).*;
i -= exprs.len;
},
Op.StructInitializer => |*fields| {
if (i < fields.len) return *fields.at(i);
if (i < fields.len) return fields.at(i).*;
i -= fields.len;
},
}
@ -1830,7 +1824,7 @@ pub const Node = struct {
pub fn iterate(self: &BuiltinCall, index: usize) ?&Node {
var i = index;
if (i < self.params.len) return *self.params.at(i);
if (i < self.params.len) return self.params.at(i).*;
i -= self.params.len;
return null;
@ -1873,11 +1867,11 @@ pub const Node = struct {
}
pub fn firstToken(self: &MultilineStringLiteral) TokenIndex {
return *self.lines.at(0);
return self.lines.at(0).*;
}
pub fn lastToken(self: &MultilineStringLiteral) TokenIndex {
return *self.lines.at(self.lines.len - 1);
return self.lines.at(self.lines.len - 1).*;
}
};
@ -1974,7 +1968,7 @@ pub const Node = struct {
const Kind = union(enum) {
Variable: &Identifier,
Return: &Node
Return: &Node,
};
pub fn iterate(self: &AsmOutput, index: usize) ?&Node {
@ -1994,7 +1988,7 @@ pub const Node = struct {
Kind.Return => |return_type| {
if (i < 1) return return_type;
i -= 1;
}
},
}
return null;
@ -2059,13 +2053,13 @@ pub const Node = struct {
pub fn iterate(self: &Asm, index: usize) ?&Node {
var i = index;
if (i < self.outputs.len) return &(*self.outputs.at(index)).base;
if (i < self.outputs.len) return &(self.outputs.at(index).*).base;
i -= self.outputs.len;
if (i < self.inputs.len) return &(*self.inputs.at(index)).base;
if (i < self.inputs.len) return &(self.inputs.at(index).*).base;
i -= self.inputs.len;
if (i < self.clobbers.len) return *self.clobbers.at(index);
if (i < self.clobbers.len) return self.clobbers.at(index).*;
i -= self.clobbers.len;
return null;
@ -2159,11 +2153,11 @@ pub const Node = struct {
}
pub fn firstToken(self: &DocComment) TokenIndex {
return *self.lines.at(0);
return self.lines.at(0).*;
}
pub fn lastToken(self: &DocComment) TokenIndex {
return *self.lines.at(self.lines.len - 1);
return self.lines.at(self.lines.len - 1).*;
}
};
@ -2192,4 +2186,3 @@ pub const Node = struct {
}
};
};

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,7 @@ const Token = std.zig.Token;
const indent_delta = 4;
pub const Error = error {
pub const Error = error{
/// Ran out of memory allocating call stack frames to complete rendering.
OutOfMemory,
};
@ -17,9 +17,9 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) (@typeOf(
var it = tree.root_node.decls.iterator(0);
while (it.next()) |decl| {
try renderTopLevelDecl(allocator, stream, tree, 0, *decl);
try renderTopLevelDecl(allocator, stream, tree, 0, decl.*);
if (it.peek()) |next_decl| {
const n = if (nodeLineOffset(tree, *decl, *next_decl) >= 2) u8(2) else u8(1);
const n = if (nodeLineOffset(tree, decl.*, next_decl.*) >= 2) u8(2) else u8(1);
try stream.writeByteNTimes('\n', n);
}
}
@ -154,10 +154,10 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
var it = block.statements.iterator(0);
while (it.next()) |statement| {
try stream.writeByteNTimes(' ', block_indent);
try renderStatement(allocator, stream, tree, block_indent, *statement);
try renderStatement(allocator, stream, tree, block_indent, statement.*);
if (it.peek()) |next_statement| {
const n = if (nodeLineOffset(tree, *statement, *next_statement) >= 2) u8(2) else u8(1);
const n = if (nodeLineOffset(tree, statement.*, next_statement.*) >= 2) u8(2) else u8(1);
try stream.writeByteNTimes('\n', n);
}
}
@ -203,7 +203,6 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
try stream.write(" ");
try renderExpression(allocator, stream, tree, indent, body);
}
},
ast.Node.Id.InfixOp => {
@ -335,7 +334,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
var it = call_info.params.iterator(0);
while (it.next()) |param_node| {
try renderExpression(allocator, stream, tree, indent, *param_node);
try renderExpression(allocator, stream, tree, indent, param_node.*);
if (it.peek() != null) {
try stream.write(", ");
}
@ -351,7 +350,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
try stream.write("]");
},
ast.Node.SuffixOp.Op.SuffixOp {
ast.Node.SuffixOp.Op.SuffixOp => {
try renderExpression(allocator, stream, tree, indent, suffix_op.lhs);
try stream.write(".*");
},
@ -375,7 +374,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
}
if (field_inits.len == 1) {
const field_init = *field_inits.at(0);
const field_init = field_inits.at(0).*;
try renderExpression(allocator, stream, tree, indent, suffix_op.lhs);
try stream.write("{ ");
@ -392,12 +391,12 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
var it = field_inits.iterator(0);
while (it.next()) |field_init| {
try stream.writeByteNTimes(' ', new_indent);
try renderExpression(allocator, stream, tree, new_indent, *field_init);
if ((*field_init).id != ast.Node.Id.LineComment) {
try renderExpression(allocator, stream, tree, new_indent, field_init.*);
if ((field_init.*).id != ast.Node.Id.LineComment) {
try stream.write(",");
}
if (it.peek()) |next_field_init| {
const n = if (nodeLineOffset(tree, *field_init, *next_field_init) >= 2) u8(2) else u8(1);
const n = if (nodeLineOffset(tree, field_init.*, next_field_init.*) >= 2) u8(2) else u8(1);
try stream.writeByteNTimes('\n', n);
}
}
@ -408,14 +407,13 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
},
ast.Node.SuffixOp.Op.ArrayInitializer => |*exprs| {
if (exprs.len == 0) {
try renderExpression(allocator, stream, tree, indent, suffix_op.lhs);
try stream.write("{}");
return;
}
if (exprs.len == 1) {
const expr = *exprs.at(0);
const expr = exprs.at(0).*;
try renderExpression(allocator, stream, tree, indent, suffix_op.lhs);
try stream.write("{");
@ -432,11 +430,11 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
var it = exprs.iterator(0);
while (it.next()) |expr| {
try stream.writeByteNTimes(' ', new_indent);
try renderExpression(allocator, stream, tree, new_indent, *expr);
try renderExpression(allocator, stream, tree, new_indent, expr.*);
try stream.write(",");
if (it.peek()) |next_expr| {
const n = if (nodeLineOffset(tree, *expr, *next_expr) >= 2) u8(2) else u8(1);
const n = if (nodeLineOffset(tree, expr.*, next_expr.*) >= 2) u8(2) else u8(1);
try stream.writeByteNTimes('\n', n);
}
}
@ -469,7 +467,6 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
ast.Node.ControlFlowExpression.Kind.Return => {
try stream.print("return");
},
}
if (flow_expr.rhs) |rhs| {
@ -575,7 +572,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
switch (container_decl.layout) {
ast.Node.ContainerDecl.Layout.Packed => try stream.print("packed "),
ast.Node.ContainerDecl.Layout.Extern => try stream.print("extern "),
ast.Node.ContainerDecl.Layout.Auto => { },
ast.Node.ContainerDecl.Layout.Auto => {},
}
switch (container_decl.kind) {
@ -611,10 +608,10 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
var it = container_decl.fields_and_decls.iterator(0);
while (it.next()) |decl| {
try stream.writeByteNTimes(' ', new_indent);
try renderTopLevelDecl(allocator, stream, tree, new_indent, *decl);
try renderTopLevelDecl(allocator, stream, tree, new_indent, decl.*);
if (it.peek()) |next_decl| {
const n = if (nodeLineOffset(tree, *decl, *next_decl) >= 2) u8(2) else u8(1);
const n = if (nodeLineOffset(tree, decl.*, next_decl.*) >= 2) u8(2) else u8(1);
try stream.writeByteNTimes('\n', n);
}
}
@ -634,7 +631,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
}
if (err_set_decl.decls.len == 1) blk: {
const node = *err_set_decl.decls.at(0);
const node = err_set_decl.decls.at(0).*;
// if there are any doc comments or same line comments
// don't try to put it all on one line
@ -644,7 +641,6 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
break :blk;
}
try stream.write("error{");
try renderTopLevelDecl(allocator, stream, tree, indent, node);
try stream.write("}");
@ -657,12 +653,12 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
var it = err_set_decl.decls.iterator(0);
while (it.next()) |node| {
try stream.writeByteNTimes(' ', new_indent);
try renderTopLevelDecl(allocator, stream, tree, new_indent, *node);
if ((*node).id != ast.Node.Id.LineComment) {
try renderTopLevelDecl(allocator, stream, tree, new_indent, node.*);
if ((node.*).id != ast.Node.Id.LineComment) {
try stream.write(",");
}
if (it.peek()) |next_node| {
const n = if (nodeLineOffset(tree, *node, *next_node) >= 2) u8(2) else u8(1);
const n = if (nodeLineOffset(tree, node.*, next_node.*) >= 2) u8(2) else u8(1);
try stream.writeByteNTimes('\n', n);
}
}
@ -676,9 +672,9 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
const multiline_str_literal = @fieldParentPtr(ast.Node.MultilineStringLiteral, "base", base);
try stream.print("\n");
var i : usize = 0;
var i: usize = 0;
while (i < multiline_str_literal.lines.len) : (i += 1) {
const t = *multiline_str_literal.lines.at(i);
const t = multiline_str_literal.lines.at(i).*;
try stream.writeByteNTimes(' ', indent + indent_delta);
try stream.print("{}", tree.tokenSlice(t));
}
@ -695,7 +691,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
var it = builtin_call.params.iterator(0);
while (it.next()) |param_node| {
try renderExpression(allocator, stream, tree, indent, *param_node);
try renderExpression(allocator, stream, tree, indent, param_node.*);
if (it.peek() != null) {
try stream.write(", ");
}
@ -740,7 +736,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
var it = fn_proto.params.iterator(0);
while (it.next()) |param_decl_node| {
try renderParamDecl(allocator, stream, tree, indent, *param_decl_node);
try renderParamDecl(allocator, stream, tree, indent, param_decl_node.*);
if (it.peek() != null) {
try stream.write(", ");
@ -764,7 +760,6 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
try renderExpression(allocator, stream, tree, indent, node);
},
}
},
ast.Node.Id.PromiseType => {
@ -801,10 +796,10 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
var it = switch_node.cases.iterator(0);
while (it.next()) |node| {
try stream.writeByteNTimes(' ', new_indent);
try renderExpression(allocator, stream, tree, new_indent, *node);
try renderExpression(allocator, stream, tree, new_indent, node.*);
if (it.peek()) |next_node| {
const n = if (nodeLineOffset(tree, *node, *next_node) >= 2) u8(2) else u8(1);
const n = if (nodeLineOffset(tree, node.*, next_node.*) >= 2) u8(2) else u8(1);
try stream.writeByteNTimes('\n', n);
}
}
@ -819,7 +814,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
var it = switch_case.items.iterator(0);
while (it.next()) |node| {
try renderExpression(allocator, stream, tree, indent, *node);
try renderExpression(allocator, stream, tree, indent, node.*);
if (it.peek() != null) {
try stream.write(",\n");
@ -846,8 +841,10 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
try stream.print("{}", tree.tokenSlice(else_node.else_token));
const block_body = switch (else_node.body.id) {
ast.Node.Id.Block, ast.Node.Id.If,
ast.Node.Id.For, ast.Node.Id.While,
ast.Node.Id.Block,
ast.Node.Id.If,
ast.Node.Id.For,
ast.Node.Id.While,
ast.Node.Id.Switch => true,
else => false,
};
@ -972,7 +969,11 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
try renderExpression(allocator, stream, tree, indent, if_node.body);
switch (if_node.body.id) {
ast.Node.Id.Block, ast.Node.Id.If, ast.Node.Id.For, ast.Node.Id.While, ast.Node.Id.Switch => {
ast.Node.Id.Block,
ast.Node.Id.If,
ast.Node.Id.For,
ast.Node.Id.While,
ast.Node.Id.Switch => {
if (if_node.@"else") |@"else"| {
if (if_node.body.id == ast.Node.Id.Block) {
try stream.write(" ");
@ -995,7 +996,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
try renderExpression(allocator, stream, tree, indent, @"else".body);
}
}
},
}
},
@ -1018,11 +1019,11 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
{
var it = asm_node.outputs.iterator(0);
while (it.next()) |asm_output| {
const node = &(*asm_output).base;
const node = &(asm_output.*).base;
try renderExpression(allocator, stream, tree, indent_extra, node);
if (it.peek()) |next_asm_output| {
const next_node = &(*next_asm_output).base;
const next_node = &(next_asm_output.*).base;
const n = if (nodeLineOffset(tree, node, next_node) >= 2) u8(2) else u8(1);
try stream.writeByte(',');
try stream.writeByteNTimes('\n', n);
@ -1038,11 +1039,11 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
{
var it = asm_node.inputs.iterator(0);
while (it.next()) |asm_input| {
const node = &(*asm_input).base;
const node = &(asm_input.*).base;
try renderExpression(allocator, stream, tree, indent_extra, node);
if (it.peek()) |next_asm_input| {
const next_node = &(*next_asm_input).base;
const next_node = &(next_asm_input.*).base;
const n = if (nodeLineOffset(tree, node, next_node) >= 2) u8(2) else u8(1);
try stream.writeByte(',');
try stream.writeByteNTimes('\n', n);
@ -1058,7 +1059,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
{
var it = asm_node.clobbers.iterator(0);
while (it.next()) |node| {
try renderExpression(allocator, stream, tree, indent_once, *node);
try renderExpression(allocator, stream, tree, indent_once, node.*);
if (it.peek() != null) {
try stream.write(", ");
@ -1220,8 +1221,7 @@ fn renderComments(tree: &ast.Tree, stream: var, node: var, indent: usize) (@type
const comment = node.doc_comments ?? return;
var it = comment.lines.iterator(0);
while (it.next()) |line_token_index| {
try stream.print("{}\n", tree.tokenSlice(*line_token_index));
try stream.print("{}\n", tree.tokenSlice(line_token_index.*));
try stream.writeByteNTimes(' ', indent);
}
}