This commit is contained in:
agra
2026-02-14 22:26:58 +02:00
parent e7d2abdf0c
commit 3fd14bafac
4 changed files with 231 additions and 403 deletions

View File

@@ -1,6 +1,7 @@
const std = @import("std");
const types = @import("types.zig");
const Type = types.Type;
const unescape = @import("unescape.zig");
/// Runtime value for comptime evaluation.
/// Replaces codegen's JitResult with richer type support.
@@ -351,34 +352,6 @@ pub const Compiler = struct {
return null;
}
/// Process escape sequences in a raw string literal.
fn unescapeString(allocator: std.mem.Allocator, raw: []const u8) ![]u8 {
var result = try allocator.alloc(u8, raw.len);
var i: usize = 0;
var j: usize = 0;
while (i < raw.len) {
if (raw[i] == '\\' and i + 1 < raw.len) {
i += 1;
switch (raw[i]) {
'n' => result[j] = '\n',
't' => result[j] = '\t',
'r' => result[j] = '\r',
'\\' => result[j] = '\\',
'"' => result[j] = '"',
'0' => result[j] = 0,
else => result[j] = raw[i],
}
j += 1;
i += 1;
} else {
result[j] = raw[i];
j += 1;
i += 1;
}
}
return result[0..j];
}
/// Compile a string literal with escape sequences and interpolation support.
/// Handles `{expr}` patterns by parsing and compiling the inner expressions,
/// then concatenating all segments together.
@@ -389,7 +362,7 @@ pub const Compiler = struct {
fn compileStringLiteral(self: *Compiler, raw: []const u8) !void {
// String literals are plain text — {} is NOT interpolated here.
// String interpolation is handled by print() at the call site.
const unescaped = try unescapeString(self.allocator, raw);
const unescaped = try unescape.unescapeString(self.allocator, raw);
const idx = try self.addString(unescaped);
try self.emit(.{ .push_string = idx });
}