authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-08 20:43:39 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-08-08 20:43:39 -07:00
log8183f483019d1771fc5fd2d58580572502c5754c
tree3d0e0840b0ff76dc124fd42af92e564f803b8831
parent2d1d0d4ae5ac5ceaec6df1877d0fcc080ca41b6f

ensure compile can be run with runtime data


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

src/lib.zig+27-10
......@@ -56,7 +56,7 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype
5656 try writer.writeAll(v[1 .. v.len - 1]);
5757 },
5858 .replacement => |v| {
59 const x = if (comptime std.mem.eql(u8, v[0], "this")) search(data, v[1..]) else search(ctx, v);
59 const x = if (comptime std.mem.eql(u8, v[0], "this")) search(v[1..], data) else search(v, ctx);
6060 const TO = @TypeOf(x);
6161
6262 if (comptime std.meta.trait.isZigString(TO)) {
......@@ -76,7 +76,7 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype
7676 switch (v.name) {
7777 .each => {
7878 comptime assertEqual(v.args.len, 1);
79 const x = comptime search(data, v.args[0]);
79 const x = search(v.args[0], data);
8080 inline for (x) |item| {
8181 inline for (v.body) |val| {
8282 try do(writer, val, item, ctx, indent, flag1);
......@@ -85,7 +85,7 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype
8585 },
8686 .@"if" => {
8787 comptime assertEqual(v.args.len, 1);
88 const x = comptime search(data, v.args[0]);
88 const x = search(v.args[0], data);
8989 if (x) {
9090 inline for (v.body) |val| {
9191 try do(writer, val, data, ctx, indent, flag1);
......@@ -94,7 +94,7 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype
9494 },
9595 .ifnot => {
9696 comptime assertEqual(v.args.len, 1);
97 const x = comptime search(data, v.args[0]);
97 const x = search(v.args[0], data);
9898 if (!x) {
9999 inline for (v.body) |val| {
100100 try do(writer, val, data, ctx, indent, flag1);
......@@ -103,8 +103,8 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype
103103 },
104104 .ifequal => {
105105 comptime assertEqual(v.args.len, 2);
106 const x = comptime search(data, v.args[0]);
107 const y = comptime search(data, v.args[1]);
106 const x = search(v.args[0], data);
107 const y = search(v.args[1], data);
108108 if (x == y) {
109109 inline for (v.body) |val| {
110110 try do(writer, val, data, ctx, indent, flag1);
......@@ -113,8 +113,8 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype
113113 },
114114 .ifnotequal => {
115115 comptime assertEqual(v.args.len, 2);
116 const x = comptime search(data, v.args[0]);
117 const y = comptime search(data, v.args[1]);
116 const x = search(v.args[0], data);
117 const y = search(v.args[1], data);
118118 if (x != y) {
119119 inline for (v.body) |val| {
120120 try do(writer, val, data, ctx, indent, flag1);
......@@ -127,8 +127,25 @@ fn do(writer: anytype, comptime value: astgen.Value, data: anytype, ctx: anytype
127127 }
128128}
129129
130fn search(comptime T: anytype, comptime args: []const []const u8) @TypeOf(if (args.len == 1) @field(T, args[0]) else search(@field(T, args[0]), args[1..])) {
131 return if (args.len == 1) @field(T, args[0]) else search(@field(T, args[0]), args[1..]);
130fn search(comptime args: []const []const u8, ctx: anytype) FieldSearch(@TypeOf(ctx), args) {
131 const f = @field(ctx, args[0]);
132 if (args.len == 1) return f;
133 return search(args[1..], f);
134}
135
136fn FieldSearch(comptime T: type, comptime args: []const []const u8) type {
137 return if (args.len == 1) Field(T, args[0]) else FieldSearch(Field(T, args[0]), args[1..]);
138}
139
140fn Field(comptime T: type, comptime field_name: []const u8) type {
141 inline for (std.meta.fields(T)) |fld| {
142 if (std.mem.eql(u8, fld.name, field_name)) return fld.field_type;
143 }
144 if (std.meta.trait.isIndexable(T) and std.mem.eql(u8, field_name, "len")) {
145 return usize;
146 }
147 @compileLog(field_name);
148 @compileLog(std.meta.fieldNames(T));
132149}
133150
134151fn entityLookupBefore(in: []const u8) ?htmlentities.Entity {