Skip to content

Commit

Permalink
std.zig.ast: add test for iterate
Browse files Browse the repository at this point in the history
closes #1101
  • Loading branch information
andrewrk committed Jun 13, 2018
1 parent 41e6c66 commit e1f56c9
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions std/zig/ast.zig
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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,
Expand All @@ -1561,8 +1566,6 @@ pub const Node = struct {
Op.NegationWrap,
Op.Try,
Op.Resume,
Op.UnwrapOptional,
Op.PointerType,
=> {},
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

0 comments on commit e1f56c9

Please sign in to comment.