std.zig.ast: add test for iterate

closes #1101
This commit is contained in:
Andrew Kelley 2018-06-13 11:48:06 -04:00
parent 41e6c664d8
commit e1f56c9af6

View File

@ -734,7 +734,7 @@ pub const Node = struct {
var i = index;
if (self.doc_comments) |comments| {
if (i < 1) return *comments.base;
if (i < 1) return &comments.base;
i -= 1;
}
@ -1243,7 +1243,7 @@ pub const Node = struct {
i -= 1;
if (self.@"else") |@"else"| {
if (i < 1) return *@"else".base;
if (i < 1) return &@"else".base;
i -= 1;
}
@ -1296,7 +1296,7 @@ pub const Node = struct {
i -= 1;
if (self.@"else") |@"else"| {
if (i < 1) return *@"else".base;
if (i < 1) return &@"else".base;
i -= 1;
}
@ -1347,7 +1347,7 @@ pub const Node = struct {
i -= 1;
if (self.@"else") |@"else"| {
if (i < 1) return *@"else".base;
if (i < 1) return &@"else".base;
i -= 1;
}
@ -1536,22 +1536,27 @@ pub const Node = struct {
var i = index;
switch (self.op) {
// TODO https://github.com/ziglang/zig/issues/1107
Op.SliceType => |addr_of_info| {
if (addr_of_info.align_info) |align_info| {
if (i < 1) return align_info.node;
i -= 1;
}
},
Op.AddrOf => |addr_of_info| {
Op.PtrType => |addr_of_info| {
if (addr_of_info.align_info) |align_info| {
if (i < 1) return align_info.node;
i -= 1;
}
},
Op.ArrayType => |size_expr| {
if (i < 1) return size_expr;
i -= 1;
},
Op.AddressOf,
Op.Await,
Op.BitNot,
Op.BoolNot,
@ -1561,8 +1566,6 @@ pub const Node = struct {
Op.NegationWrap,
Op.Try,
Op.Resume,
Op.UnwrapOptional,
Op.PointerType,
=> {},
}
@ -1667,7 +1670,9 @@ pub const Node = struct {
if (i < fields.len) return fields.at(i).*;
i -= fields.len;
},
Op.Deref => {},
Op.UnwrapOptional,
Op.Deref,
=> {},
}
return null;
@ -2022,7 +2027,7 @@ pub const Node = struct {
switch (self.kind) {
Kind.Variable => |variable_name| {
if (i < 1) return *variable_name.base;
if (i < 1) return &variable_name.base;
i -= 1;
},
Kind.Return => |return_type| {
@ -2092,10 +2097,10 @@ 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;
return null;
@ -2205,3 +2210,14 @@ pub const Node = struct {
}
};
};
test "iterate" {
var root = Node.Root{
.base = Node{ .id = Node.Id.Root },
.doc_comments = null,
.decls = Node.Root.DeclList.init(std.debug.global_allocator),
.eof_token = 0,
};
var base = &root.base;
assert(base.iterate(0) == null);
}