Compiler/virtual machine interpreter: Difference between revisions
Content added Content deleted
(Add Zig language implementation) |
m (Update Zig to 0.9.0) |
||
Line 3,768: | Line 3,768: | ||
pub const VirtualMachine = struct { |
pub const VirtualMachine = struct { |
||
allocator: |
allocator: std.mem.Allocator, |
||
stack: [stack_size]i32, |
stack: [stack_size]i32, |
||
program: std.ArrayList(u8), |
program: std.ArrayList(u8), |
||
Line 3,782: | Line 3,782: | ||
pub fn init( |
pub fn init( |
||
allocator: |
allocator: std.mem.Allocator, |
||
program: std.ArrayList(u8), |
program: std.ArrayList(u8), |
||
string_pool: std.ArrayList([]const u8), |
string_pool: std.ArrayList([]const u8), |
||
Line 3,915: | Line 3,915: | ||
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 4,001: | Line 4,001: | ||
// 100 lines of code to load serialized bytecode, eh |
// 100 lines of code to load serialized bytecode, eh |
||
fn loadBytecode( |
fn loadBytecode( |
||
allocator: |
allocator: std.mem.Allocator, |
||
str: []const u8, |
str: []const u8, |
||
string_pool: *std.ArrayList([]const u8), |
string_pool: *std.ArrayList([]const u8), |
||
Line 4,007: | Line 4,007: | ||
) !std.ArrayList(u8) { |
) !std.ArrayList(u8) { |
||
var result = std.ArrayList(u8).init(allocator); |
var result = std.ArrayList(u8).init(allocator); |
||
var line_it = std.mem.split(str, "\n"); |
var line_it = std.mem.split(u8, str, "\n"); |
||
while (line_it.next()) |line| { |
while (line_it.next()) |line| { |
||
if (std.mem.indexOf(u8, line, "halt")) |_| { |
if (std.mem.indexOf(u8, line, "halt")) |_| { |
||
var tok_it = std.mem.tokenize(line, " "); |
var tok_it = std.mem.tokenize(u8, line, " "); |
||
const size = try std.fmt.parseInt(usize, tok_it.next().?, 10); |
const size = try std.fmt.parseInt(usize, tok_it.next().?, 10); |
||
try result.resize(size + 1); |
try result.resize(size + 1); |
||
Line 4,023: | Line 4,023: | ||
const string_pool_size = try std.fmt.parseInt(usize, first_line[strings_index + " Strings: ".len ..], 10); |
const string_pool_size = try std.fmt.parseInt(usize, first_line[strings_index + " Strings: ".len ..], 10); |
||
try globals.resize(globals_size); |
try globals.resize(globals_size); |
||
try string_pool. |
try string_pool.ensureTotalCapacity(string_pool_size); |
||
var string_cnt: usize = 0; |
var string_cnt: usize = 0; |
||
while (string_cnt < string_pool_size) : (string_cnt += 1) { |
while (string_cnt < string_pool_size) : (string_cnt += 1) { |
||
Line 4,053: | Line 4,053: | ||
if (line.len == 0) break; |
if (line.len == 0) break; |
||
var tok_it = std.mem.tokenize(line, " "); |
var tok_it = std.mem.tokenize(u8, line, " "); |
||
const address = try std.fmt.parseInt(usize, tok_it.next().?, 10); |
const address = try std.fmt.parseInt(usize, tok_it.next().?, 10); |
||
const op = Op.fromString(tok_it.next().?); |
const op = Op.fromString(tok_it.next().?); |