authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-12-10 21:26:28-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-12-10 21:26:52-05:00
logf210f17d306ed1298901d38a29c1e50a1ee38ae5
tree77e220c6b51859d1c808a5fbcab82c97e1705fc3
parent4b1d120f5874a3cad4d05e21e6a3219e48f9b056

add self-hosted parsing and rendering to main tests


6 files changed, 72 insertions(+), 7 deletions(-)

build.zig+4
......@@ -53,6 +53,10 @@ pub fn build(b: &Builder) {
5353 "std/special/compiler_rt/index.zig", "compiler-rt", "Run the compiler_rt tests",
5454 with_lldb));
5555
56 test_step.dependOn(tests.addPkgTests(b, test_filter,
57 "src-self-hosted/main.zig", "fmt", "Run the fmt tests",
58 with_lldb));
59
5660 test_step.dependOn(tests.addCompareOutputTests(b, test_filter));
5761 test_step.dependOn(tests.addBuildExampleTests(b, test_filter));
5862 test_step.dependOn(tests.addCompileErrorTests(b, test_filter));
src-self-hosted/main.zig+35
......@@ -1387,6 +1387,7 @@ const Parser = struct {
13871387
13881388 %return stream.print("(");
13891389
1390 %return stack.append(RenderState { .Text = "\n" });
13901391 if (fn_proto.fn_def_node == null) {
13911392 %return stack.append(RenderState { .Text = ";" });
13921393 }
......@@ -1536,3 +1537,37 @@ fn removeNullCast(x: var) -> {const InnerPtr = @typeOf(x).Child.Child; &InnerPtr
15361537 const InnerPtr = @typeOf(x).Child.Child;
15371538 return @ptrCast(&InnerPtr, x);
15381539}
1540
1541
1542
1543fn testCanonical(source: []const u8) {
1544 const allocator = std.debug.global_allocator;
1545 std.debug.global_allocator_index = 0;
1546
1547 var tokenizer = Tokenizer.init(source);
1548 var parser = Parser.init(&tokenizer, allocator, "(memory buffer)");
1549 defer parser.deinit();
1550
1551 const root_node = parser.parse() %% unreachable;
1552 defer parser.freeAst(root_node);
1553
1554 var buffer = std.Buffer.initSize(allocator, 0) %% unreachable;
1555 var buffer_out_stream = io.BufferOutStream.init(&buffer);
1556 parser.renderSource(&buffer_out_stream.stream, root_node) %% unreachable;
1557
1558 if (!mem.eql(u8, buffer.toSliceConst(), source)) {
1559 warn("\n====== expected this output: =========\n");
1560 warn("{}", source);
1561 warn("\n======== instead found this: =========\n");
1562 warn("{}", buffer.toSliceConst());
1563 warn("\n======================================\n");
1564 @panic("test failed");
1565 }
1566}
1567
1568test "zig fmt" {
1569 testCanonical(
1570 \\extern fn puts(s: &const u8) -> c_int;
1571 \\
1572 );
1573}
std/buffer.zig+3
......@@ -98,14 +98,17 @@ pub const Buffer = struct {
9898 mem.copy(u8, self.list.toSlice()[old_len..], m);
9999 }
100100
101 // TODO: remove, use OutStream for this
101102 pub fn appendFormat(self: &Buffer, comptime format: []const u8, args: ...) -> %void {
102103 return fmt.format(self, append, format, args);
103104 }
104105
106 // TODO: remove, use OutStream for this
105107 pub fn appendByte(self: &Buffer, byte: u8) -> %void {
106108 return self.appendByteNTimes(byte, 1);
107109 }
108110
111 // TODO: remove, use OutStream for this
109112 pub fn appendByteNTimes(self: &Buffer, byte: u8, count: usize) -> %void {
110113 var prev_size: usize = self.len();
111114 %return self.resize(prev_size + count);
std/debug.zig+8-7
......@@ -966,28 +966,29 @@ fn readILeb128(in_stream: &io.InStream) -> %i64 {
966966}
967967
968968pub const global_allocator = &global_allocator_state;
969
969970var global_allocator_state = mem.Allocator {
970971 .allocFn = globalAlloc,
971972 .reallocFn = globalRealloc,
972973 .freeFn = globalFree,
973974};
974975
975var some_mem: [100 * 1024]u8 = undefined;
976var some_mem_index: usize = 0;
976pub var global_allocator_mem: [100 * 1024]u8 = undefined;
977pub var global_allocator_index: usize = 0;
977978
978979error OutOfMemory;
979980
980981fn globalAlloc(self: &mem.Allocator, n: usize, alignment: u29) -> %[]u8 {
981 const addr = @ptrToInt(&some_mem[some_mem_index]);
982 const addr = @ptrToInt(&global_allocator_mem[global_allocator_index]);
982983 const rem = @rem(addr, alignment);
983984 const march_forward_bytes = if (rem == 0) 0 else (alignment - rem);
984 const adjusted_index = some_mem_index + march_forward_bytes;
985 const adjusted_index = global_allocator_index + march_forward_bytes;
985986 const end_index = adjusted_index + n;
986 if (end_index > some_mem.len) {
987 if (end_index > global_allocator_mem.len) {
987988 return error.OutOfMemory;
988989 }
989 const result = some_mem[adjusted_index .. end_index];
990 some_mem_index = end_index;
990 const result = global_allocator_mem[adjusted_index .. end_index];
991 global_allocator_index = end_index;
991992 return result;
992993}
993994
std/index.zig+1
......@@ -3,6 +3,7 @@ pub const AlignedArrayList = @import("array_list.zig").AlignedArrayList;
33pub const BufMap = @import("buf_map.zig").BufMap;
44pub const BufSet = @import("buf_set.zig").BufSet;
55pub const Buffer = @import("buffer.zig").Buffer;
6pub const BufferOutStream = @import("buffer.zig").BufferOutStream;
67pub const HashMap = @import("hash_map.zig").HashMap;
78pub const LinkedList = @import("linked_list.zig").LinkedList;
89
std/io.zig+21
......@@ -633,3 +633,24 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize) -> type {
633633 }
634634 };
635635}
636
637/// Implementation of OutStream trait for Buffer
638pub const BufferOutStream = struct {
639 buffer: &Buffer,
640 stream: OutStream,
641
642 pub fn init(buffer: &Buffer) -> BufferOutStream {
643 return BufferOutStream {
644 .buffer = buffer,
645 .stream = OutStream {
646 .writeFn = writeFn,
647 },
648 };
649 }
650
651 fn writeFn(out_stream: &OutStream, bytes: []const u8) -> %void {
652 const self = @fieldParentPtr(BufferOutStream, "stream", out_stream);
653 return self.buffer.append(bytes);
654 }
655};
656