authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-13 11:48:06-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-13 11:48:06-04:00
loge1f56c9af6fd9ab495e0c499e2c545e7d048fa9e
treeca0434ab12d5d1735dcd84f2a2f15a515ee999b3
parent41e6c664d8b751b81effd2341cf1bd5950c40d8c

std.zig.ast: add test for iterate

closes #1101

1 files changed, 27 insertions(+), 11 deletions(-)

std/zig/ast.zig+27-11
...@@ -734,7 +734,7 @@ pub const Node = struct {...@@ -734,7 +734,7 @@ pub const Node = struct {
734 var i = index;734 var i = index;
735735
736 if (self.doc_comments) |comments| {736 if (self.doc_comments) |comments| {
737 if (i < 1) return *comments.base;737 if (i < 1) return &comments.base;
738 i -= 1;738 i -= 1;
739 }739 }
740740
...@@ -1243,7 +1243,7 @@ pub const Node = struct {...@@ -1243,7 +1243,7 @@ pub const Node = struct {
1243 i -= 1;1243 i -= 1;
12441244
1245 if (self.@"else") |@"else"| {1245 if (self.@"else") |@"else"| {
1246 if (i < 1) return *@"else".base;1246 if (i < 1) return &@"else".base;
1247 i -= 1;1247 i -= 1;
1248 }1248 }
12491249
...@@ -1296,7 +1296,7 @@ pub const Node = struct {...@@ -1296,7 +1296,7 @@ pub const Node = struct {
1296 i -= 1;1296 i -= 1;
12971297
1298 if (self.@"else") |@"else"| {1298 if (self.@"else") |@"else"| {
1299 if (i < 1) return *@"else".base;1299 if (i < 1) return &@"else".base;
1300 i -= 1;1300 i -= 1;
1301 }1301 }
13021302
...@@ -1347,7 +1347,7 @@ pub const Node = struct {...@@ -1347,7 +1347,7 @@ pub const Node = struct {
1347 i -= 1;1347 i -= 1;
13481348
1349 if (self.@"else") |@"else"| {1349 if (self.@"else") |@"else"| {
1350 if (i < 1) return *@"else".base;1350 if (i < 1) return &@"else".base;
1351 i -= 1;1351 i -= 1;
1352 }1352 }
13531353
...@@ -1536,22 +1536,27 @@ pub const Node = struct {...@@ -1536,22 +1536,27 @@ pub const Node = struct {
1536 var i = index;1536 var i = index;
15371537
1538 switch (self.op) {1538 switch (self.op) {
1539 // TODO https://github.com/ziglang/zig/issues/1107
1539 Op.SliceType => |addr_of_info| {1540 Op.SliceType => |addr_of_info| {
1540 if (addr_of_info.align_info) |align_info| {1541 if (addr_of_info.align_info) |align_info| {
1541 if (i < 1) return align_info.node;1542 if (i < 1) return align_info.node;
1542 i -= 1;1543 i -= 1;
1543 }1544 }
1544 },1545 },
1545 Op.AddrOf => |addr_of_info| {1546
1547 Op.PtrType => |addr_of_info| {
1546 if (addr_of_info.align_info) |align_info| {1548 if (addr_of_info.align_info) |align_info| {
1547 if (i < 1) return align_info.node;1549 if (i < 1) return align_info.node;
1548 i -= 1;1550 i -= 1;
1549 }1551 }
1550 },1552 },
1553
1551 Op.ArrayType => |size_expr| {1554 Op.ArrayType => |size_expr| {
1552 if (i < 1) return size_expr;1555 if (i < 1) return size_expr;
1553 i -= 1;1556 i -= 1;
1554 },1557 },
1558
1559 Op.AddressOf,
1555 Op.Await,1560 Op.Await,
1556 Op.BitNot,1561 Op.BitNot,
1557 Op.BoolNot,1562 Op.BoolNot,
...@@ -1561,8 +1566,6 @@ pub const Node = struct {...@@ -1561,8 +1566,6 @@ pub const Node = struct {
1561 Op.NegationWrap,1566 Op.NegationWrap,
1562 Op.Try,1567 Op.Try,
1563 Op.Resume,1568 Op.Resume,
1564 Op.UnwrapOptional,
1565 Op.PointerType,
1566 => {},1569 => {},
1567 }1570 }
15681571
...@@ -1667,7 +1670,9 @@ pub const Node = struct {...@@ -1667,7 +1670,9 @@ pub const Node = struct {
1667 if (i < fields.len) return fields.at(i).*;1670 if (i < fields.len) return fields.at(i).*;
1668 i -= fields.len;1671 i -= fields.len;
1669 },1672 },
1670 Op.Deref => {},1673 Op.UnwrapOptional,
1674 Op.Deref,
1675 => {},
1671 }1676 }
16721677
1673 return null;1678 return null;
...@@ -2022,7 +2027,7 @@ pub const Node = struct {...@@ -2022,7 +2027,7 @@ pub const Node = struct {
20222027
2023 switch (self.kind) {2028 switch (self.kind) {
2024 Kind.Variable => |variable_name| {2029 Kind.Variable => |variable_name| {
2025 if (i < 1) return *variable_name.base;2030 if (i < 1) return &variable_name.base;
2026 i -= 1;2031 i -= 1;
2027 },2032 },
2028 Kind.Return => |return_type| {2033 Kind.Return => |return_type| {
...@@ -2092,10 +2097,10 @@ pub const Node = struct {...@@ -2092,10 +2097,10 @@ pub const Node = struct {
2092 pub fn iterate(self: *Asm, index: usize) ?*Node {2097 pub fn iterate(self: *Asm, index: usize) ?*Node {
2093 var i = index;2098 var i = index;
20942099
2095 if (i < self.outputs.len) return *(self.outputs.at(index).*).base;2100 if (i < self.outputs.len) return &self.outputs.at(index).*.base;
2096 i -= self.outputs.len;2101 i -= self.outputs.len;
20972102
2098 if (i < self.inputs.len) return *(self.inputs.at(index).*).base;2103 if (i < self.inputs.len) return &self.inputs.at(index).*.base;
2099 i -= self.inputs.len;2104 i -= self.inputs.len;
21002105
2101 return null;2106 return null;
...@@ -2205,3 +2210,14 @@ pub const Node = struct {...@@ -2205,3 +2210,14 @@ pub const Node = struct {
2205 }2210 }
2206 };2211 };
2207};2212};
2213
2214test "iterate" {
2215 var root = Node.Root{
2216 .base = Node{ .id = Node.Id.Root },
2217 .doc_comments = null,
2218 .decls = Node.Root.DeclList.init(std.debug.global_allocator),
2219 .eof_token = 0,
2220 };
2221 var base = &root.base;
2222 assert(base.iterate(0) == null);
2223}