Compiler/AST interpreter: Difference between revisions
m
→{{header|Zig}}: update to Zig 0.11.0 - casting builtins renaming, fix errors from std library changes
Recombinant (talk | contribs) m (→{{header|Zig}}: update to Zig 0.11.0 - casting builtins renaming, fix errors from std library changes) |
|||
Line 7,075:
</pre>
{{works with|Zig|0.11.0}}
To simplify memory allocation management <tt>std.heap.ArenaAllocator</tt> is used in the code below. This allows all an arena's allocations to be freed together with a single call to arena.deinit()
=={{header|Zig}}==
<syntaxhighlight lang="zig">
Line 7,142 ⟶ 7,144:
.prts => _ = try self.out("{s}", .{(try self.interp(t.left)).?.string}),
.prti => _ = try self.out("{d}", .{(try self.interp(t.left)).?.integer}),
.prtc => _ = try self.out("{c}", .{@
.string => return t.value,
.integer => return t.value,
Line 7,161 ⟶ 7,163:
fn binOp(
self: *Self,
comptime func: fn (a: i32, b: i32) i32,
a: ?*Tree,
b: ?*Tree,
Line 7,172 ⟶ 7,174:
fn less(a: i32, b: i32) i32 {
return @
}
fn less_equal(a: i32, b: i32) i32 {
return @
}
fn greater(a: i32, b: i32) i32 {
return @
}
fn greater_equal(a: i32, b: i32) i32 {
return @
}
fn equal(a: i32, b: i32) i32 {
return @
}
fn not_equal(a: i32, b: i32) i32 {
return @
}
fn add(a: i32, b: i32) i32 {
Line 7,205 ⟶ 7,207:
}
fn @"or"(a: i32, b: i32) i32 {
return @
}
fn @"and"(a: i32, b: i32) i32 {
return @
}
};
Line 7,217 ⟶ 7,219:
const allocator = arena.allocator();
var arg_it = try std.process.
_ =
const file_name = arg_it.next(
// We accept both files and standard input.
var file_handle = blk: {
if (file_name) |file_name_delimited| {
const fname: []const u8 =
break :blk try std.fs.cwd().openFile(fname, .{});
} else {
Line 7,339 ⟶ 7,341:
fn loadASTHelper(
allocator: std.mem.Allocator,
line_it: *std.mem.SplitIterator(u8, std.mem.DelimiterType.sequence),
string_pool: *std.ArrayList([]const u8),
) LoadASTError!?*Tree {
|