golly jeepers it's taking a long time to update translate-c

master
Andrew Kelley 2020-05-20 13:53:53 -04:00
parent 4617c5907a
commit 8c10178a1e
4 changed files with 339 additions and 267 deletions

View File

@ -49,6 +49,26 @@ pub fn SinglyLinkedList(comptime T: type) type {
node.next = next_node.next;
return next_node;
}
/// Iterate over the singly-linked list from this node, until the final node is found.
/// This operation is O(N).
pub fn findLast(node: *Node) *Node {
var it = node;
while (true) {
it = it.next orelse return it;
}
}
/// Iterate over each next node, returning the count of all nodes except the starting one.
/// This operation is O(N).
pub fn countChildren(node: *const Node) usize {
var count: usize = 0;
var it: ?*const Node = node;
while (it) |n| : (it = n.next) {
count += 1;
}
return count;
}
};
first: ?*Node = null,
@ -87,6 +107,16 @@ pub fn SinglyLinkedList(comptime T: type) type {
list.first = first.next;
return first;
}
/// Iterate over all nodes, returning the count.
/// This operation is O(N).
pub fn len(list: Self) usize {
if (list.first) |n| {
return 1 + n.countChildren();
} else {
return 0;
}
}
};
}

View File

@ -915,8 +915,7 @@ fn renderExpression(
if (maybe_row_size) |row_size| {
// A place to store the width of each expression and its column's maximum
const exprs_len = countLen(exprs.first);
var widths = try allocator.alloc(usize, exprs_len + row_size);
var widths = try allocator.alloc(usize, exprs.len() + row_size);
defer allocator.free(widths);
mem.set(usize, widths, 0);
@ -1391,7 +1390,7 @@ fn renderExpression(
{
break :blk false;
}
const last_node = findLast(builtin_call.params.first.?).data;
const last_node = builtin_call.params.first.?.findLast().data;
const maybe_comma = tree.nextToken(last_node.lastToken());
break :blk tree.tokens[maybe_comma].id == .Comma;
};
@ -1615,7 +1614,7 @@ fn renderExpression(
assert(switch_case.items.first != null);
const src_has_trailing_comma = blk: {
const last_node = findLast(switch_case.items.first.?).data;
const last_node = switch_case.items.first.?.findLast().data;
const maybe_comma = tree.nextToken(last_node.lastToken());
break :blk tree.tokens[maybe_comma].id == .Comma;
};
@ -2510,19 +2509,3 @@ fn copyFixingWhitespace(stream: var, slice: []const u8) @TypeOf(stream).Error!vo
else => try stream.writeByte(byte),
};
}
fn countLen(node: ?*std.SinglyLinkedList(*ast.Node).Node) usize {
var count: usize = 0;
var it = node;
while (it) |n| : (it = n.next) {
count += 1;
}
return count;
}
fn findLast(node: *std.SinglyLinkedList(*ast.Node).Node) *std.SinglyLinkedList(*ast.Node).Node {
var it = node;
while (true) {
it = it.next orelse return it;
}
}

View File

@ -600,8 +600,7 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
};
defer tree.deinit();
var error_it = tree.errors.iterator(0);
while (error_it.next()) |parse_error| {
for (tree.errors) |parse_error| {
try printErrMsgToFile(gpa, parse_error, tree, "<stdin>", stderr_file, color);
}
if (tree.errors.len != 0) {
@ -701,8 +700,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
};
defer tree.deinit();
var error_it = tree.errors.iterator(0);
while (error_it.next()) |parse_error| {
for (tree.errors) |parse_error| {
try printErrMsgToFile(fmt.gpa, parse_error, tree, file_path, std.io.getStdErr(), fmt.color);
}
if (tree.errors.len != 0) {
@ -730,7 +728,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
fn printErrMsgToFile(
gpa: *mem.Allocator,
parse_error: *const ast.Error,
parse_error: ast.Error,
tree: *ast.Tree,
path: []const u8,
file: fs.File,
@ -745,15 +743,15 @@ fn printErrMsgToFile(
const span_first = lok_token;
const span_last = lok_token;
const first_token = tree.tokens.at(span_first);
const last_token = tree.tokens.at(span_last);
const first_token = tree.tokens[span_first];
const last_token = tree.tokens[span_last];
const start_loc = tree.tokenLocationPtr(0, first_token);
const end_loc = tree.tokenLocationPtr(first_token.end, last_token);
var text_buf = std.ArrayList(u8).init(gpa);
defer text_buf.deinit();
const out_stream = text_buf.outStream();
try parse_error.render(&tree.tokens, out_stream);
try parse_error.render(tree.tokens, out_stream);
const text = text_buf.span();
const stream = file.outStream();

File diff suppressed because it is too large Load Diff