feat: formatting strings at runtime

This commit is contained in:
Brett Broadhurst 2026-04-05 14:45:10 -06:00
parent 6924e929d8
commit d2cd7fa888
Failed to generate hash of commit
10 changed files with 211 additions and 34 deletions

View file

@ -739,9 +739,29 @@ fn stringLiteral(gi: *GenIr, node: *const Ast.Node) InnerError!Ir.Inst.Ref {
return gi.addStr(str.index, str.len);
}
fn stringExpr(gen: *GenIr, expr_node: *const Ast.Node) InnerError!Ir.Inst.Ref {
const first_node = expr_node.data.bin.lhs.?;
return stringLiteral(gen, first_node);
fn stringExpr(gi: *GenIr, scope: *Scope, node: *const Ast.Node) InnerError!Ir.Inst.Ref {
const data = node.data.list;
const gpa = gi.astgen.gpa;
const scratch_top = gi.astgen.scratch.items.len;
for (data.items) |sub_node| {
const result = switch (sub_node.tag) {
.inline_logic_expr => try inlineLogicExpr(gi, scope, sub_node),
.string_literal => try stringLiteral(gi, sub_node),
inline else => |_| unreachable,
};
try gi.astgen.scratch.append(gpa, @intFromEnum(result));
}
const multi_op_len = gi.astgen.scratch.items.len - scratch_top;
const extra_len = @typeInfo(Ir.Inst.MultiOp).@"struct".fields.len + multi_op_len;
try gi.astgen.extra.ensureUnusedCapacity(gpa, extra_len);
const extra_index = gi.astgen.addExtraAssumeCapacity(Ir.Inst.MultiOp{
.operands_len = @intCast(multi_op_len),
});
gi.astgen.appendBlockBody(@ptrCast(gi.astgen.scratch.items[scratch_top..]));
return gi.addPayloadNodeWithIndex(.str_format, node, extra_index);
}
fn identifier(
@ -764,8 +784,7 @@ fn expr(gi: *GenIr, scope: *Scope, optional_node: ?*const Ast.Node) InnerError!I
.false_literal => return .bool_false,
.number_literal => return numberLiteral(gi, node),
.string_literal => return stringLiteral(gi, node),
.string_expr => return stringExpr(gi, node),
.empty_string => return stringLiteral(gi, node),
.string_expr => return stringExpr(gi, scope, node),
.identifier => return identifier(gi, scope, node),
.add_expr => return binaryOp(gi, scope, node, .add),
.subtract_expr => return binaryOp(gi, scope, node, .sub),