Skip to content

Commit

Permalink
Rename VM to Vm
Browse files Browse the repository at this point in the history
  • Loading branch information
yukiisbored committed Jul 8, 2023
1 parent c637f3d commit 9e981cf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
File renamed without changes.
34 changes: 17 additions & 17 deletions src/ast.zig
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 9e981cf

Please sign in to comment.