Jump to content

Compiler/virtual machine interpreter: Difference between revisions

m
Update Zig to 0.9.0
(Add Zig language implementation)
m (Update Zig to 0.9.0)
Line 3,768:
 
pub const VirtualMachine = struct {
allocator: *std.mem.Allocator,
stack: [stack_size]i32,
program: std.ArrayList(u8),
Line 3,782:
 
pub fn init(
allocator: *std.mem.Allocator,
program: std.ArrayList(u8),
string_pool: std.ArrayList([]const u8),
Line 3,915:
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
varconst allocator = &arena.allocator();
 
var arg_it = std.process.args();
Line 4,001:
// 100 lines of code to load serialized bytecode, eh
fn loadBytecode(
allocator: *std.mem.Allocator,
str: []const u8,
string_pool: *std.ArrayList([]const u8),
Line 4,007:
) !std.ArrayList(u8) {
var result = std.ArrayList(u8).init(allocator);
var line_it = std.mem.split(u8, str, "\n");
while (line_it.next()) |line| {
if (std.mem.indexOf(u8, line, "halt")) |_| {
var tok_it = std.mem.tokenize(u8, line, " ");
const size = try std.fmt.parseInt(usize, tok_it.next().?, 10);
try result.resize(size + 1);
Line 4,023:
const string_pool_size = try std.fmt.parseInt(usize, first_line[strings_index + " Strings: ".len ..], 10);
try globals.resize(globals_size);
try string_pool.ensureCapacityensureTotalCapacity(string_pool_size);
var string_cnt: usize = 0;
while (string_cnt < string_pool_size) : (string_cnt += 1) {
Line 4,053:
if (line.len == 0) break;
 
var tok_it = std.mem.tokenize(u8, line, " ");
const address = try std.fmt.parseInt(usize, tok_it.next().?, 10);
const op = Op.fromString(tok_it.next().?);
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.