authorgravatar for lachlan@lakebythewoods.xyzLachlan Easton <lachlan@lakebythewoods.xyz> 2020-08-31 23:32:42+10:00
committergravatar for lachlan@lakebythewoods.xyzLachlan Easton <lachlan@lakebythewoods.xyz> 2020-08-31 23:39:50+10:00
log029ec456bce5fc6c57eea496db1cebed55e31ede
treeb04ebc1ba122151f53c3da7261a8c109f3ba0ae7
parent5aca3baea62326dee301ec29c567dc224baa4a08

zig fmt: Set indent_delta to 2 when rendering inline asm


3 files changed, 33 insertions(+), 20 deletions(-)

lib/std/io/auto_indenting_stream.zig+24-12
...@@ -13,7 +13,7 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {...@@ -13,7 +13,7 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {
1313
14 writer_pointer: *WriterType,14 writer_pointer: *WriterType,
1515
16 indent_stack: usize = 0,16 indent_count: usize = 0,
17 indent_delta: usize,17 indent_delta: usize,
18 current_line_empty: bool = true,18 current_line_empty: bool = true,
19 indent_one_shot_count: usize = 0, // automatically popped when applied19 indent_one_shot_count: usize = 0, // automatically popped when applied
...@@ -24,9 +24,6 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {...@@ -24,9 +24,6 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {
24 return Self{ .writer_pointer = writer_pointer, .indent_delta = indent_delta };24 return Self{ .writer_pointer = writer_pointer, .indent_delta = indent_delta };
25 }25 }
2626
27 /// Release all allocated memory.
28 pub fn deinit(self: Self) void {}
29
30 pub fn writer(self: *Self) Writer {27 pub fn writer(self: *Self) Writer {
31 return .{ .context = self };28 return .{ .context = self };
32 }29 }
...@@ -39,6 +36,21 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {...@@ -39,6 +36,21 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {
39 return self.writeNoIndent(bytes);36 return self.writeNoIndent(bytes);
40 }37 }
4138
39 // Change the indent delta without changing the final indentation level
40 pub fn setIndentDelta(self: *Self, indent_delta: usize) void {
41 if (self.indent_delta == indent_delta) {
42 return;
43 } else if (self.indent_delta > indent_delta) {
44 assert(self.indent_delta % indent_delta == 0);
45 self.indent_count = self.indent_count * (self.indent_delta / indent_delta);
46 } else {
47 // assert that the current indentation (in spaces) in a multiple of the new delta
48 assert((self.indent_count * self.indent_delta) % indent_delta == 0);
49 self.indent_count = self.indent_count / (indent_delta / self.indent_delta);
50 }
51 self.indent_delta = indent_delta;
52 }
53
42 fn writeNoIndent(self: *Self, bytes: []const u8) Error!usize {54 fn writeNoIndent(self: *Self, bytes: []const u8) Error!usize {
43 if (bytes.len == 0)55 if (bytes.len == 0)
44 return @as(usize, 0);56 return @as(usize, 0);
...@@ -68,7 +80,7 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {...@@ -68,7 +80,7 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {
68 pub fn pushIndent(self: *Self) void {80 pub fn pushIndent(self: *Self) void {
69 // Doesn't actually write any indentation.81 // Doesn't actually write any indentation.
70 // Just primes the stream to be able to write the correct indentation if it needs to.82 // Just primes the stream to be able to write the correct indentation if it needs to.
71 self.indent_stack += 1;83 self.indent_count += 1;
72 }84 }
7385
74 /// Push an indent that is automatically popped after being applied86 /// Push an indent that is automatically popped after being applied
...@@ -92,9 +104,9 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {...@@ -92,9 +104,9 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {
92 }104 }
93105
94 pub fn popIndent(self: *Self) void {106 pub fn popIndent(self: *Self) void {
95 assert(self.indent_stack != 0);107 assert(self.indent_count != 0);
96 self.indent_stack -= 1;108 self.indent_count -= 1;
97 self.indent_next_line = std.math.min(self.indent_stack, self.indent_next_line); // Tentative indent may have been popped before there was a newline109 self.indent_next_line = std.math.min(self.indent_count, self.indent_next_line); // Tentative indent may have been popped before there was a newline
98 }110 }
99111
100 /// Writes ' ' bytes if the current line is empty112 /// Writes ' ' bytes if the current line is empty
...@@ -105,7 +117,7 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {...@@ -105,7 +117,7 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {
105 self.applied_indent = current_indent;117 self.applied_indent = current_indent;
106 }118 }
107119
108 self.indent_stack -= self.indent_one_shot_count;120 self.indent_count -= self.indent_one_shot_count;
109 self.indent_one_shot_count = 0;121 self.indent_one_shot_count = 0;
110 self.current_line_empty = false;122 self.current_line_empty = false;
111 }123 }
...@@ -118,9 +130,9 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {...@@ -118,9 +130,9 @@ pub fn AutoIndentingStream(comptime WriterType: type) type {
118130
119 fn currentIndent(self: *Self) usize {131 fn currentIndent(self: *Self) usize {
120 var indent_current: usize = 0;132 var indent_current: usize = 0;
121 if (self.indent_stack > 0) {133 if (self.indent_count > 0) {
122 const stack_top = self.indent_stack - self.indent_next_line;134 const indent_count = self.indent_count - self.indent_next_line;
123 indent_current = stack_top * self.indent_delta;135 indent_current = indent_count * self.indent_delta;
124 }136 }
125 return indent_current;137 return indent_current;
126 }138 }
lib/std/zig/parser_test.zig+5-5
...@@ -2827,7 +2827,7 @@ test "zig fmt: inline asm" {...@@ -2827,7 +2827,7 @@ test "zig fmt: inline asm" {
2827 \\ return asm volatile ("syscall"2827 \\ return asm volatile ("syscall"
2828 \\ : [ret] "={rax}" (-> usize)2828 \\ : [ret] "={rax}" (-> usize)
2829 \\ : [number] "{rax}" (number),2829 \\ : [number] "{rax}" (number),
2830 \\ [arg1] "{rdi}" (arg1)2830 \\ [arg1] "{rdi}" (arg1)
2831 \\ : "rcx", "r11"2831 \\ : "rcx", "r11"
2832 \\ );2832 \\ );
2833 \\}2833 \\}
...@@ -2930,14 +2930,14 @@ test "zig fmt: inline asm parameter alignment" {...@@ -2930,14 +2930,14 @@ test "zig fmt: inline asm parameter alignment" {
2930 \\ \\ foo2930 \\ \\ foo
2931 \\ \\ bar2931 \\ \\ bar
2932 \\ : [_] "" (-> usize),2932 \\ : [_] "" (-> usize),
2933 \\ [_] "" (-> usize)2933 \\ [_] "" (-> usize)
2934 \\ );2934 \\ );
2935 \\ asm volatile (2935 \\ asm volatile (
2936 \\ \\ foo2936 \\ \\ foo
2937 \\ \\ bar2937 \\ \\ bar
2938 \\ :2938 \\ :
2939 \\ : [_] "" (0),2939 \\ : [_] "" (0),
2940 \\ [_] "" (0)2940 \\ [_] "" (0)
2941 \\ );2941 \\ );
2942 \\ asm volatile (2942 \\ asm volatile (
2943 \\ \\ foo2943 \\ \\ foo
...@@ -2950,9 +2950,9 @@ test "zig fmt: inline asm parameter alignment" {...@@ -2950,9 +2950,9 @@ test "zig fmt: inline asm parameter alignment" {
2950 \\ \\ foo2950 \\ \\ foo
2951 \\ \\ bar2951 \\ \\ bar
2952 \\ : [_] "" (-> usize),2952 \\ : [_] "" (-> usize),
2953 \\ [_] "" (-> usize)2953 \\ [_] "" (-> usize)
2954 \\ : [_] "" (0),2954 \\ : [_] "" (0),
2955 \\ [_] "" (0)2955 \\ [_] "" (0)
2956 \\ : "", ""2956 \\ : "", ""
2957 \\ );2957 \\ );
2958 \\}2958 \\}
lib/std/zig/render.zig+4-3
...@@ -11,6 +11,7 @@ const ast = std.zig.ast;...@@ -11,6 +11,7 @@ const ast = std.zig.ast;
11const Token = std.zig.Token;11const Token = std.zig.Token;
1212
13const indent_delta = 4;13const indent_delta = 4;
14const asm_indent_delta = 2;
1415
15pub const Error = error{16pub const Error = error{
16 /// Ran out of memory allocating call stack frames to complete rendering.17 /// Ran out of memory allocating call stack frames to complete rendering.
...@@ -25,7 +26,6 @@ pub fn render(allocator: *mem.Allocator, stream: anytype, tree: *ast.Tree) (meta...@@ -25,7 +26,6 @@ pub fn render(allocator: *mem.Allocator, stream: anytype, tree: *ast.Tree) (meta
25 var s = stream.*;26 var s = stream.*;
26 var change_detection_stream = std.io.changeDetectionStream(tree.source, &s);27 var change_detection_stream = std.io.changeDetectionStream(tree.source, &s);
27 var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, &change_detection_stream);28 var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, &change_detection_stream);
28 defer auto_indenting_stream.deinit();
2929
30 try renderRoot(allocator, &auto_indenting_stream, tree);30 try renderRoot(allocator, &auto_indenting_stream, tree);
3131
...@@ -784,7 +784,6 @@ fn renderExpression(...@@ -784,7 +784,6 @@ fn renderExpression(
784 // Null stream for counting the printed length of each expression784 // Null stream for counting the printed length of each expression
785 var counting_stream = std.io.countingOutStream(std.io.null_out_stream);785 var counting_stream = std.io.countingOutStream(std.io.null_out_stream);
786 var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, &counting_stream);786 var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, &counting_stream);
787 defer auto_indenting_stream.deinit();
788787
789 for (exprs) |expr, i| {788 for (exprs) |expr, i| {
790 counting_stream.bytes_written = 0;789 counting_stream.bytes_written = 0;
...@@ -903,7 +902,6 @@ fn renderExpression(...@@ -903,7 +902,6 @@ fn renderExpression(
903 for (field_inits) |field_init| {902 for (field_inits) |field_init| {
904 var find_stream = std.io.findByteOutStream('\n', &std.io.null_out_stream);903 var find_stream = std.io.findByteOutStream('\n', &std.io.null_out_stream);
905 var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, &find_stream);904 var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, &find_stream);
906 defer auto_indenting_stream.deinit();
907905
908 try renderExpression(allocator, &auto_indenting_stream, tree, field_init, Space.None);906 try renderExpression(allocator, &auto_indenting_stream, tree, field_init, Space.None);
909 if (find_stream.byte_found) break :blk false;907 if (find_stream.byte_found) break :blk false;
...@@ -1959,6 +1957,9 @@ fn renderExpression(...@@ -1959,6 +1957,9 @@ fn renderExpression(
19591957
1960 try renderExpression(allocator, stream, tree, asm_node.template, Space.Newline);1958 try renderExpression(allocator, stream, tree, asm_node.template, Space.Newline);
19611959
1960 stream.setIndentDelta(asm_indent_delta);
1961 defer stream.setIndentDelta(indent_delta);
1962
1962 const colon1 = tree.nextToken(asm_node.template.lastToken());1963 const colon1 = tree.nextToken(asm_node.template.lastToken());
19631964
1964 const colon2 = if (asm_node.outputs.len == 0) blk: {1965 const colon2 = if (asm_node.outputs.len == 0) blk: {