Compiler/code generator: Difference between revisions

m
Update Zig to 0.9.0
(Add Zig language implementation)
m (Update Zig to 0.9.0)
Line 5,907:
 
pub const CodeGenerator = struct {
allocator: *std.mem.Allocator,
string_pool: std.ArrayList([]const u8),
globals: std.ArrayList([]const u8),
Line 5,916:
 
pub fn init(
allocator: *std.mem.Allocator,
string_pool: std.ArrayList([]const u8),
globals: std.ArrayList([]const u8),
Line 6,231:
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
varconst allocator = &arena.allocator();
 
var arg_it = std.process.args();
Line 6,330:
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 6,336:
}
 
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 6,346:
 
fn loadAST(
allocator: *std.mem.Allocator,
str: []const u8,
string_pool: *std.ArrayList([]const u8),
globals: *std.ArrayList([]const u8),
) LoadASTError!?*Tree {
var line_it = std.mem.split(u8, str, "\n");
return try loadASTHelper(allocator, &line_it, string_pool, globals);
}
 
fn loadASTHelper(
allocator: *std.mem.Allocator,
line_it: *std.mem.SplitIterator(u8),
string_pool: *std.ArrayList([]const u8),
globals: *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;
Line 6,409:
return null;
}
}
 
fn squishSpaces(allocator: *std.mem.Allocator, str: []const u8) ![]u8 {
var result = std.ArrayList(u8).init(allocator);
var was_space = false;
for (str) |ch| {
switch (ch) {
' ' => {
if (!was_space) {
was_space = true;
try result.append(ch);
}
},
else => {
was_space = false;
try result.append(ch);
},
}
}
return result.items;
}
</lang>
Anonymous user