Compiler/AST interpreter: Difference between revisions
Content added Content deleted
(Add Zig language implementation) |
m (Update Zig to 0.9.0) |
||
Line 3,263: | Line 3,263: | ||
const Self = @This(); |
const Self = @This(); |
||
pub fn init(allocator: |
pub fn init(allocator: std.mem.Allocator) Self { |
||
return ASTInterpreter{ |
return ASTInterpreter{ |
||
.output = std.ArrayList(u8).init(allocator), |
.output = std.ArrayList(u8).init(allocator), |
||
Line 3,391: | Line 3,391: | ||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); |
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); |
||
defer arena.deinit(); |
defer arena.deinit(); |
||
const allocator = arena.allocator(); |
|||
var arg_it = std.process.args(); |
var arg_it = std.process.args(); |
||
Line 3,489: | Line 3,489: | ||
value: ?NodeValue = null, |
value: ?NodeValue = null, |
||
fn makeNode(allocator: |
fn makeNode(allocator: std.mem.Allocator, typ: NodeType, left: ?*Tree, right: ?*Tree) !*Tree { |
||
const result = try allocator.create(Tree); |
const result = try allocator.create(Tree); |
||
result.* = Tree{ .left = left, .right = right, .typ = typ }; |
result.* = Tree{ .left = left, .right = right, .typ = typ }; |
||
Line 3,495: | Line 3,495: | ||
} |
} |
||
fn makeLeaf(allocator: |
fn makeLeaf(allocator: std.mem.Allocator, typ: NodeType, value: ?NodeValue) !*Tree { |
||
const result = try allocator.create(Tree); |
const result = try allocator.create(Tree); |
||
result.* = Tree{ .left = null, .right = null, .typ = typ, .value = value }; |
result.* = Tree{ .left = null, .right = null, .typ = typ, .value = value }; |
||
Line 3,505: | Line 3,505: | ||
fn loadAST( |
fn loadAST( |
||
allocator: |
allocator: std.mem.Allocator, |
||
str: []const u8, |
str: []const u8, |
||
string_pool: *std.ArrayList([]const u8), |
string_pool: *std.ArrayList([]const u8), |
||
) LoadASTError!?*Tree { |
) LoadASTError!?*Tree { |
||
var line_it = std.mem.split(str, "\n"); |
var line_it = std.mem.split(u8, str, "\n"); |
||
return try loadASTHelper(allocator, &line_it, string_pool); |
return try loadASTHelper(allocator, &line_it, string_pool); |
||
} |
} |
||
fn loadASTHelper( |
fn loadASTHelper( |
||
allocator: |
allocator: std.mem.Allocator, |
||
line_it: *std.mem.SplitIterator, |
line_it: *std.mem.SplitIterator(u8), |
||
string_pool: *std.ArrayList([]const u8), |
string_pool: *std.ArrayList([]const u8), |
||
) LoadASTError!?*Tree { |
) LoadASTError!?*Tree { |
||
if (line_it.next()) |line| { |
if (line_it.next()) |line| { |
||
var tok_it = std.mem.tokenize(line, " "); |
var tok_it = std.mem.tokenize(u8, line, " "); |
||
const tok_str = tok_it.next().?; |
const tok_str = tok_it.next().?; |
||
if (tok_str[0] == ';') return null; |
if (tok_str[0] == ';') return null; |