diff --git a/src/VM.zig b/src/Vm.zig similarity index 100% rename from src/VM.zig rename to src/Vm.zig diff --git a/src/ast.zig b/src/ast.zig index a3bebb5..6d30776 100644 --- a/src/ast.zig +++ b/src/ast.zig @@ -1,6 +1,6 @@ const std = @import("std"); -const VM = @import("./VM.zig"); +const Vm = @import("./Vm.zig"); pub const AST = union(enum) { n: i32, @@ -21,7 +21,7 @@ pub const AST = union(enum) { pub const Program = struct { allocator: std.mem.Allocator, - routines: std.ArrayList(std.ArrayList(VM.Instruction)), + routines: std.ArrayList(std.ArrayList(Vm.Instruction)), pub const Routine = struct { name: []const u8, @@ -39,14 +39,14 @@ pub const AST = union(enum) { } }; - pub fn init(allocator: std.mem.Allocator, vm: *VM, routines: []const Routine) !Program { + pub fn init(allocator: std.mem.Allocator, vm: *Vm, routines: []const Routine) !Program { var res = Program{ .allocator = allocator, - .routines = std.ArrayList(std.ArrayList(VM.Instruction)).init(allocator), + .routines = std.ArrayList(std.ArrayList(Vm.Instruction)).init(allocator), }; for (routines) |r| { - var i = std.ArrayList(VM.Instruction).init(allocator); + var i = std.ArrayList(Vm.Instruction).init(allocator); try compile(&i, r.ast); try i.append(.ret); try vm.addRoutine(r.name, i.items); @@ -105,22 +105,22 @@ pub const AST = union(enum) { } fn compile( - instructions: *std.ArrayList(VM.Instruction), + instructions: *std.ArrayList(Vm.Instruction), routine: []const AST, ) !void { for (routine) |c| { switch (c) { - .n => |n| try instructions.append(VM.Instruction{ .psh = VM.Value{ .n = n } }), - .b => |b| try instructions.append(VM.Instruction{ .psh = VM.Value{ .b = b } }), - .s => |s| try instructions.append(VM.Instruction{ .psh = VM.Value{ .s = s } }), - .id => |s| try instructions.append(VM.Instruction{ .cal = s }), + .n => |n| try instructions.append(Vm.Instruction{ .psh = Vm.Value{ .n = n } }), + .b => |b| try instructions.append(Vm.Instruction{ .psh = Vm.Value{ .b = b } }), + .s => |s| try instructions.append(Vm.Instruction{ .psh = Vm.Value{ .s = s } }), + .id => |s| try instructions.append(Vm.Instruction{ .cal = s }), .@"if" => |s| { - try instructions.append(VM.Instruction{ .jif = 0 }); + try instructions.append(Vm.Instruction{ .jif = 0 }); const false_jump_index = instructions.items.len - 1; try compile(instructions, s.if_true); - try instructions.append(VM.Instruction{ .jmp = 0 }); + try instructions.append(Vm.Instruction{ .jmp = 0 }); const end_jump_index = instructions.items.len - 1; const false_jump = end_jump_index + 1; @@ -129,18 +129,18 @@ pub const AST = union(enum) { try instructions.append(.nop); const end_jump = instructions.items.len - 1; - instructions.items[false_jump_index] = VM.Instruction{ .jif = false_jump }; - instructions.items[end_jump_index] = VM.Instruction{ .jmp = end_jump }; + instructions.items[false_jump_index] = Vm.Instruction{ .jif = false_jump }; + instructions.items[end_jump_index] = Vm.Instruction{ .jmp = end_jump }; }, .@"while" => |s| { const start_index = instructions.items.len; try compile(instructions, s); - try instructions.append(VM.Instruction{ .jif = 0 }); + try instructions.append(Vm.Instruction{ .jif = 0 }); const false_jump_index = instructions.items.len - 1; - try instructions.append(VM.Instruction{ .jmp = start_index }); + try instructions.append(Vm.Instruction{ .jmp = start_index }); const false_jump = instructions.items.len; - instructions.items[false_jump_index] = VM.Instruction{ .jif = false_jump }; + instructions.items[false_jump_index] = Vm.Instruction{ .jif = false_jump }; }, .add => try instructions.append(.add), .sub => try instructions.append(.sub), diff --git a/src/main.zig b/src/main.zig index 32cdf22..9c2b4a9 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,7 +1,7 @@ const std = @import("std"); const print = std.debug.print; -const VM = @import("./VM.zig"); +const Vm = @import("./Vm.zig"); const AST = @import("./ast.zig").AST; const Scanner = @import("./Scanner.zig"); const Parser = @import("./Parser.zig"); @@ -85,7 +85,7 @@ pub fn main() !void { print("=== VM INIT ===\n", .{}); } - var vm = try VM.init(allocator); + var vm = try Vm.init(allocator); defer vm.deinit(); if (debug) {