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 {...@@ -33,10 +33,10 @@ pub fn parse(comptime input: []const u8) astgen.Value {
33}33}
3434
35pub fn compile(writer: anytype, comptime value: astgen.Value, data: anytype) !void {35pub 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);
37}37}
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 {
40 switch (value) {40 switch (value) {
41 .element => |v| {41 .element => |v| {
42 const hastext = for (v.children) |x| {42 const hastext = for (v.children) |x| {
...@@ -58,7 +58,7 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, indent: usiz...@@ -58,7 +58,7 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, indent: usiz
5858
59 if (!hastext) try writer.writeAll("\n");59 if (!hastext) try writer.writeAll("\n");
60 inline for (v.children) |it| {60 inline for (v.children) |it| {
61 try do(writer, it, data, indent + 1, !hastext);61 try do(writer, it, data, ctx, indent + 1, !hastext);
62 }62 }
63 if (!hastext) for (range(indent)) |_| try writer.writeAll(" ");63 if (!hastext) for (range(indent)) |_| try writer.writeAll(" ");
64 try writer.print("</{s}>", .{v.name});64 try writer.print("</{s}>", .{v.name});
...@@ -69,18 +69,14 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, indent: usiz...@@ -69,18 +69,14 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, indent: usiz
69 try writer.writeAll(v[1 .. v.len - 1]);69 try writer.writeAll(v[1 .. v.len - 1]);
70 },70 },
71 .replacement => |v| {71 .replacement => |v| {
72 const x = @field(data, v[0]);72 const x = if (comptime std.mem.eql(u8, v[0], "this")) search(data, v[1..]) else search(ctx, v);
73 if (v.len == 1) {73 const TO = @TypeOf(x);
74 const TO = @TypeOf(x);
7574
76 if (comptime std.meta.trait.isZigString(TO)) {75 if (comptime std.meta.trait.isZigString(TO)) {
77 try writer.print("{s}", .{x});76 try writer.print("{s}", .{x});
78 return;77 return;
79 }
80 @compileError("pek: compile: unsupported type: " ++ @typeName(TO));
81 } else {
82 try do(writer, astgen.Value{ .replacement = v[1..] }, x, indent, flag1);
83 }78 }
79 @compileError("pek: compile: unsupported type: " ++ @typeName(TO));
84 },80 },
85 .block => |v| {81 .block => |v| {
86 const x = comptime search(data, v.args);82 const x = comptime search(data, v.args);
...@@ -89,7 +85,7 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, indent: usiz...@@ -89,7 +85,7 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, indent: usiz
89 .each => {85 .each => {
90 inline for (x) |item| {86 inline for (x) |item| {
91 inline for (v.body) |val| {87 inline for (v.body) |val| {
92 try do(writer, val, item, indent, flag1);88 try do(writer, val, item, ctx, indent, flag1);
93 }89 }
94 }90 }
95 },91 },