This commit is contained in:
jaina heartles 2022-11-16 00:33:06 -08:00
parent c9d0090ab2
commit e4a04b869e
2 changed files with 30 additions and 22 deletions

View file

@ -4,15 +4,17 @@ pub fn main() !void {
try execute(std.io.getStdOut().writer(), @embedFile("./test.tmp.html"), .{ try execute(std.io.getStdOut().writer(), @embedFile("./test.tmp.html"), .{
.community = .{ .name = "abcd" }, .community = .{ .name = "abcd" },
.foo = [_][]const u8{ "5", "4", "3", "2", "1" }, .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 { pub fn execute(writer: anytype, comptime template: []const u8, args: anytype) !void {
@setEvalBranchQuota(@intCast(u32, template.len * 6)); @setEvalBranchQuota(@intCast(u32, template.len * 6));
const tmpl = comptime parseTemplate(TokenIter{ .text = template }, .root); 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 { 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 { fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype, captures: anytype) !void {
switch (stmt) { switch (stmt) {
.expression => |expr| switch (expr) { .expression => |expr| {
.arg_deref => |fields| { const val = evaluateExpression(expr, args, captures);
const arg = deref(args, fields); try print(writer, val);
try print(writer, arg);
},
.capture_deref => |fields| {
const arg = deref(captures, fields);
try print(writer, arg);
},
}, },
.for_loop => |loop| { .for_loop => |loop| {
const fields = loop.iterable.arg_deref; const iterable = evaluateExpression(loop.iterable, args, captures);
const iterable = deref(args, fields);
const subtemplate = loop.subtemplate; const subtemplate = loop.subtemplate;
try writer.writeAll("capture on " ++ loop.capture ++ ": ");
for (iterable) |v| { for (iterable) |v| {
try executeTemplate( try executeTemplate(
writer, writer,
@ -54,6 +48,7 @@ fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype, ca
fn print(writer: anytype, arg: anytype) !void { fn print(writer: anytype, arg: anytype) !void {
if (comptime std.meta.trait.isZigString(@TypeOf(arg))) return writer.writeAll(arg); if (comptime std.meta.trait.isZigString(@TypeOf(arg))) return writer.writeAll(arg);
@compileLog(@TypeOf(arg));
@compileError("TODO"); @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..]); 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 { fn AddCapture(comptime Root: type, comptime name: []const u8, comptime Val: type) type {
var fields = std.meta.fields(Root) ++ [_]std.builtin.Type.StructField{.{ var fields = std.meta.fields(Root) ++ [_]std.builtin.Type.StructField{.{
.name = name, .name = name,
@ -109,7 +122,6 @@ fn parseTemplate(comptime tokens: TokenIter, comptime template_type: TemplateTyp
var current_text: []const u8 = ""; var current_text: []const u8 = "";
parse_loop: while (iter.next()) |token| { parse_loop: while (iter.next()) |token| {
if (logging) @compileLog(token);
switch (token) { switch (token) {
.whitespace, .text => |text| current_text = current_text ++ text, .whitespace, .text => |text| current_text = current_text ++ text,
.open_bracket => { .open_bracket => {

View file

@ -9,12 +9,8 @@
<h2> {{ REAL BRACKETS }} </h2> <h2> {{ REAL BRACKETS }} </h2>
<section> <section>
{$bar} {#for .baz |$f|}{#for $f |$b|}{$b}:{#end_for}
{#for .foo |$f|}a testing thing: {$f} {#end_for} {#end_for}
{{#for args.notes |$note, $i|}}
<h3>Note no. {{$i}}</h3>
{{#template note_display ($note)}}
{{#end}}
</section> </section>
</body> </body>
</html> </html>