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"), .{
.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 => {

View File

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