authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-06 03:25:59 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-06 03:25:59 -07:00
log2241163284861edaeca5f3e5f94f5d21410b55b5
treefca4d1ba2884c2f9f1a16cde8d6d8367986bee99
parent791a3f5dcd2498c6ced5f1800f1921e63f54f12b

add ctx param so that compile can always have ref to global context in recursion


1 files changed, 10 insertions(+), 14 deletions(-)

src/lib.zig+10-14
......@@ -33,10 +33,10 @@ pub fn parse(comptime input: []const u8) astgen.Value {
3333}
3434
3535pub fn compile(writer: anytype, comptime value: astgen.Value, data: anytype) !void {
36 return try do(writer, value, data, 0, false);
36 return try do(writer, value, data, data, 0, false);
3737}
3838
39fn do(writer: anytype, comptime value: astgen.Value, data: anytype, indent: usize, flag1: bool) anyerror!void {
39fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype, indent: usize, flag1: bool) anyerror!void {
4040 switch (value) {
4141 .element => |v| {
4242 const hastext = for (v.children) |x| {
......@@ -58,7 +58,7 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, indent: usiz
5858
5959 if (!hastext) try writer.writeAll("\n");
6060 inline for (v.children) |it| {
61 try do(writer, it, data, indent + 1, !hastext);
61 try do(writer, it, data, ctx, indent + 1, !hastext);
6262 }
6363 if (!hastext) for (range(indent)) |_| try writer.writeAll(" ");
6464 try writer.print("</{s}>", .{v.name});
......@@ -69,18 +69,14 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, indent: usiz
6969 try writer.writeAll(v[1 .. v.len - 1]);
7070 },
7171 .replacement => |v| {
72 const x = @field(data, v[0]);
73 if (v.len == 1) {
74 const TO = @TypeOf(x);
72 const x = if (comptime std.mem.eql(u8, v[0], "this")) search(data, v[1..]) else search(ctx, v);
73 const TO = @TypeOf(x);
7574
76 if (comptime std.meta.trait.isZigString(TO)) {
77 try writer.print("{s}", .{x});
78 return;
79 }
80 @compileError("pek: compile: unsupported type: " ++ @typeName(TO));
81 } else {
82 try do(writer, astgen.Value{ .replacement = v[1..] }, x, indent, flag1);
75 if (comptime std.meta.trait.isZigString(TO)) {
76 try writer.print("{s}", .{x});
77 return;
8378 }
79 @compileError("pek: compile: unsupported type: " ++ @typeName(TO));
8480 },
8581 .block => |v| {
8682 const x = comptime search(data, v.args);
......@@ -89,7 +85,7 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, indent: usiz
8985 .each => {
9086 inline for (x) |item| {
9187 inline for (v.body) |val| {
92 try do(writer, val, item, indent, flag1);
88 try do(writer, val, item, ctx, indent, flag1);
9389 }
9490 }
9591 },