Compiler/AST interpreter: Difference between revisions

Content added Content deleted
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}", .{@intCastas(u8, @intCast((try self.interp(t.left)).?.integer))}),
.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 @boolToIntintFromBool(a < b);
}
fn less_equal(a: i32, b: i32) i32 {
return @boolToIntintFromBool(a <= b);
}
fn greater(a: i32, b: i32) i32 {
return @boolToIntintFromBool(a > b);
}
fn greater_equal(a: i32, b: i32) i32 {
return @boolToIntintFromBool(a >= b);
}
fn equal(a: i32, b: i32) i32 {
return @boolToIntintFromBool(a == b);
}
fn not_equal(a: i32, b: i32) i32 {
return @boolToIntintFromBool(a != b);
}
fn add(a: i32, b: i32) i32 {
Line 7,205 ⟶ 7,207:
}
fn @"or"(a: i32, b: i32) i32 {
return @boolToIntintFromBool((a != 0) or (b != 0));
}
fn @"and"(a: i32, b: i32) i32 {
return @boolToIntintFromBool((a != 0) and (b != 0));
}
};
Line 7,217 ⟶ 7,219:
const allocator = arena.allocator();
 
var arg_it = try std.process.argsargsWithAllocator(allocator);
_ = try arg_it.next(allocator) orelse unreachable; // program name
const file_name = arg_it.next(allocator);
// We accept both files and standard input.
var file_handle = blk: {
if (file_name) |file_name_delimited| {
const fname: []const u8 = try file_name_delimited;
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 {