diff --git a/src/template/lib.zig b/src/template/lib.zig index 83617dd..c997629 100644 --- a/src/template/lib.zig +++ b/src/template/lib.zig @@ -4,15 +4,17 @@ pub fn main() !void { try execute(std.io.getStdOut().writer(), @embedFile("./test.tmp.html"), .{ .community = .{ .name = "abcd" }, .foo = [_][]const u8{ "5", "4", "3", "2", "1" }, + .baz = [_][]const []const u8{ + &.{ "5", "4", "3", "2", "1" }, + &.{ "5", "4", "3", "2", "1" }, + }, }); } -const logging = false; - pub fn execute(writer: anytype, comptime template: []const u8, args: anytype) !void { @setEvalBranchQuota(@intCast(u32, template.len * 6)); const tmpl = comptime parseTemplate(TokenIter{ .text = template }, .root); - try executeTemplate(writer, tmpl.item, args, .{ .bar = "abc" }); + try executeTemplate(writer, tmpl.item, args, .{}); } fn executeTemplate(writer: anytype, comptime items: []const TemplateItem, args: anytype, captures: anytype) !void { @@ -24,21 +26,13 @@ fn executeTemplate(writer: anytype, comptime items: []const TemplateItem, args: fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype, captures: anytype) !void { switch (stmt) { - .expression => |expr| switch (expr) { - .arg_deref => |fields| { - const arg = deref(args, fields); - try print(writer, arg); - }, - .capture_deref => |fields| { - const arg = deref(captures, fields); - try print(writer, arg); - }, + .expression => |expr| { + const val = evaluateExpression(expr, args, captures); + try print(writer, val); }, .for_loop => |loop| { - const fields = loop.iterable.arg_deref; - const iterable = deref(args, fields); + const iterable = evaluateExpression(loop.iterable, args, captures); const subtemplate = loop.subtemplate; - try writer.writeAll("capture on " ++ loop.capture ++ ": "); for (iterable) |v| { try executeTemplate( writer, @@ -54,6 +48,7 @@ fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype, ca fn print(writer: anytype, arg: anytype) !void { if (comptime std.meta.trait.isZigString(@TypeOf(arg))) return writer.writeAll(arg); + @compileLog(@TypeOf(arg)); @compileError("TODO"); } @@ -74,6 +69,24 @@ fn deref(arg: anytype, comptime names: []const []const u8) Deref(@TypeOf(arg), n return deref(@field(arg, names[0]), names[1..]); } +fn EvaluateExpression(comptime expression: Expression, comptime Args: type, comptime Captures: type) type { + return switch (expression) { + .arg_deref => |names| Deref(Args, names), + .capture_deref => |names| Deref(Captures, names), + }; +} + +fn evaluateExpression( + comptime expression: Expression, + args: anytype, + captures: anytype, +) EvaluateExpression(expression, @TypeOf(args), @TypeOf(captures)) { + return switch (expression) { + .arg_deref => |names| deref(args, names), + .capture_deref => |names| deref(captures, names), + }; +} + fn AddCapture(comptime Root: type, comptime name: []const u8, comptime Val: type) type { var fields = std.meta.fields(Root) ++ [_]std.builtin.Type.StructField{.{ .name = name, @@ -109,7 +122,6 @@ fn parseTemplate(comptime tokens: TokenIter, comptime template_type: TemplateTyp var current_text: []const u8 = ""; parse_loop: while (iter.next()) |token| { - if (logging) @compileLog(token); switch (token) { .whitespace, .text => |text| current_text = current_text ++ text, .open_bracket => { diff --git a/src/template/test.tmp.html b/src/template/test.tmp.html index 08b8dfb..87bac71 100644 --- a/src/template/test.tmp.html +++ b/src/template/test.tmp.html @@ -9,12 +9,8 @@