Jump to content

Compiler/AST interpreter: Difference between revisions

m
Update Zig to 0.9.0
(Add Zig language implementation)
m (Update Zig to 0.9.0)
Line 3,263:
const Self = @This();
 
pub fn init(allocator: *std.mem.Allocator) Self {
return ASTInterpreter{
.output = std.ArrayList(u8).init(allocator),
Line 3,391:
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
varconst allocator = &arena.allocator();
 
var arg_it = std.process.args();
Line 3,489:
value: ?NodeValue = null,
 
fn makeNode(allocator: *std.mem.Allocator, typ: NodeType, left: ?*Tree, right: ?*Tree) !*Tree {
const result = try allocator.create(Tree);
result.* = Tree{ .left = left, .right = right, .typ = typ };
Line 3,495:
}
 
fn makeLeaf(allocator: *std.mem.Allocator, typ: NodeType, value: ?NodeValue) !*Tree {
const result = try allocator.create(Tree);
result.* = Tree{ .left = null, .right = null, .typ = typ, .value = value };
Line 3,505:
 
fn loadAST(
allocator: *std.mem.Allocator,
str: []const u8,
string_pool: *std.ArrayList([]const u8),
) LoadASTError!?*Tree {
var line_it = std.mem.split(u8, str, "\n");
return try loadASTHelper(allocator, &line_it, string_pool);
}
 
fn loadASTHelper(
allocator: *std.mem.Allocator,
line_it: *std.mem.SplitIterator(u8),
string_pool: *std.ArrayList([]const u8),
) LoadASTError!?*Tree {
if (line_it.next()) |line| {
var tok_it = std.mem.tokenize(u8, line, " ");
const tok_str = tok_it.next().?;
if (tok_str[0] == ';') return null;
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.