| ... | ... | @@ -33,10 +33,10 @@ pub fn parse(comptime input: []const u8) astgen.Value { |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | pub 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 | } |
| 38 | 38 | |
| 39 | | fn do(writer: anytype, comptime value: astgen.Value, data: anytype, indent: usize, flag1: bool) anyerror!void { |
| 39 | fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype, indent: usize, flag1: bool) anyerror!void { |
| 40 | 40 | switch (value) { |
| 41 | 41 | .element => |v| { |
| 42 | 42 | const hastext = for (v.children) |x| { |
| ... | ... | @@ -58,7 +58,7 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, indent: usiz |
| 58 | 58 | |
| 59 | 59 | if (!hastext) try writer.writeAll("\n"); |
| 60 | 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 | 63 | if (!hastext) for (range(indent)) |_| try writer.writeAll(" "); |
| 64 | 64 | try writer.print("</{s}>", .{v.name}); |
| ... | ... | @@ -69,18 +69,14 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, indent: usiz |
| 69 | 69 | try writer.writeAll(v[1 .. v.len - 1]); |
| 70 | 70 | }, |
| 71 | 71 | .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); |
| 75 | 74 | |
| 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; |
| 83 | 78 | } |
| 79 | @compileError("pek: compile: unsupported type: " ++ @typeName(TO)); |
| 84 | 80 | }, |
| 85 | 81 | .block => |v| { |
| 86 | 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 | 85 | .each => { |
| 90 | 86 | inline for (x) |item| { |
| 91 | 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 | }, |