fix: passing context

This commit is contained in:
Lucas F. 2026-01-22 12:49:02 -03:00
parent 777b13ed98
commit 8bf9902ebf

View file

@ -195,15 +195,15 @@ pub const Renderer = struct {
.tag => { .tag => {
switch (node.tag.?.kind) { switch (node.tag.?.kind) {
.if_block => { .if_block => {
const condition = try self.evaluateCondition(alloc, node.tag.?.body.@"if".condition); const condition = try self.evaluateCondition(alloc, node.tag.?.body.@"if".condition, context);
if (condition) { if (condition) {
for (node.tag.?.body.@"if".true_body) |child| { for (node.tag.?.body.@"if".true_body) |child| {
try self.renderNode(alloc, nodes, child, writer, null, null); try self.renderNode(alloc, nodes, child, writer, context, null);
} }
} else { } else {
for (node.tag.?.body.@"if".false_body) |child| { for (node.tag.?.body.@"if".false_body) |child| {
try self.renderNode(alloc, nodes, child, writer, null, null); try self.renderNode(alloc, nodes, child, writer, context, null);
} }
} }
}, },
@ -480,7 +480,7 @@ pub const Renderer = struct {
}; };
} }
fn evaluateCondition(self: *const Renderer, allocator: Allocator, expr: []const u8) RenderError!bool { fn evaluateCondition(self: *const Renderer, allocator: Allocator, expr: []const u8, context: ?*Context) RenderError!bool {
const trimmed = std.mem.trim(u8, expr, " \t\r\n"); const trimmed = std.mem.trim(u8, expr, " \t\r\n");
if (trimmed.len == 0) return false; if (trimmed.len == 0) return false;
@ -513,7 +513,12 @@ pub const Renderer = struct {
const op = tokens.items[1]; const op = tokens.items[1];
const right_str = tokens.items[2]; const right_str = tokens.items[2];
const left_value = self.context.get(left) orelse Value.null; var left_value: Value = Value.null;
if (context) |ctx| {
left_value = ctx.get(left) orelse Value.null;
}
if (left_value == Value.null) left_value = self.context.get(left) orelse Value.null;
const right_value = parseLiteral(right_str); const right_value = parseLiteral(right_str);
if (std.mem.eql(u8, op, ">")) return compare(left_value, right_value, .gt); if (std.mem.eql(u8, op, ">")) return compare(left_value, right_value, .gt);