moving to the virtual machine part of the book, pt 1
the java part would still fuck me up since it lies on OOP stuff that zig doesnt provide, so i'm skipping towards the C part of the book which will hopefully be more understandable from a zig perspective.
This commit is contained in:
parent
727a259638
commit
c4401dc8cf
5 changed files with 125 additions and 35 deletions
71
src/chunk.zig
Normal file
71
src/chunk.zig
Normal file
|
@ -0,0 +1,71 @@
|
|||
const std = @import("std");
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
// hack. ugly hack. zig has compiler crash.
|
||||
const AllOpcodes = struct {
|
||||
pub Return: u8 = 0,
|
||||
};
|
||||
|
||||
pub const OpCode = AllOpcodes{};
|
||||
|
||||
fn simpleInstruction(
|
||||
stdout: var,
|
||||
comptime name: []const u8,
|
||||
offset: usize,
|
||||
) !usize {
|
||||
try stdout.print("{}\n", name);
|
||||
return offset + 1;
|
||||
}
|
||||
|
||||
pub const Chunk = struct {
|
||||
count: usize,
|
||||
code: []u8,
|
||||
allocator: *Allocator,
|
||||
|
||||
pub fn init(allocator: *Allocator) !Chunk {
|
||||
return Chunk{
|
||||
.count = 0,
|
||||
.allocator = allocator,
|
||||
.code = try allocator.alloc(u8, 0),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn write(self: *Chunk, byte: u8) !void {
|
||||
if (self.code.len < self.count + 1) {
|
||||
self.code = try self.allocator.realloc(
|
||||
self.code,
|
||||
self.count + 1,
|
||||
);
|
||||
}
|
||||
|
||||
self.code[self.count] = byte;
|
||||
self.count += 1;
|
||||
}
|
||||
|
||||
pub fn disassembleInstruction(
|
||||
self: *Chunk,
|
||||
stdout: var,
|
||||
index: usize,
|
||||
) !usize {
|
||||
try stdout.print("{} ", index);
|
||||
|
||||
var instruction = self.code[index];
|
||||
|
||||
if (instruction == 0) {
|
||||
return try simpleInstruction(stdout, "OP_RETURN", index);
|
||||
} else {
|
||||
try stdout.print("Unknown opcode: {}\n", instruction);
|
||||
return index + 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn disassemble(self: *Chunk, stdout: var, name: []const u8) !void {
|
||||
try stdout.print("== {} ==\n", name);
|
||||
|
||||
var i: usize = 0;
|
||||
while (i < self.count) : (i += 1) {
|
||||
i = try self.disassembleInstruction(stdout, i);
|
||||
}
|
||||
}
|
||||
};
|
13
src/compiler.zig
Normal file
13
src/compiler.zig
Normal file
|
@ -0,0 +1,13 @@
|
|||
const token = @import("token.zig");
|
||||
const scanner = @import("scanner.zig");
|
||||
const main = @import("main.zig");
|
||||
|
||||
pub const Compiler = struct {
|
||||
tokens: *scanner.TokenList,
|
||||
|
||||
fn init(tokens: *scanner.TokenList) Compiler {
|
||||
return Compiler{ .tokens = tokens };
|
||||
}
|
||||
|
||||
fn advance(self: *Compiler) void {}
|
||||
};
|
28
src/expr.zig
28
src/expr.zig
|
@ -1,28 +0,0 @@
|
|||
const Token = @import("token.zig").Token;
|
||||
|
||||
pub const Binary = struct {
|
||||
left: Expr,
|
||||
operator: Token,
|
||||
right: Expr,
|
||||
};
|
||||
|
||||
pub const Grouping = struct {
|
||||
expression: Expr,
|
||||
};
|
||||
|
||||
pub const Unary = struct {
|
||||
operator: Token,
|
||||
right: Expr,
|
||||
};
|
||||
|
||||
pub const ExprType = enum {
|
||||
Binary,
|
||||
Grouping,
|
||||
Unary,
|
||||
};
|
||||
|
||||
pub const Expr = union(ExprType) {
|
||||
Binary: Binary,
|
||||
Grouping: Grouping,
|
||||
Unary: Unary,
|
||||
};
|
41
src/main.zig
41
src/main.zig
|
@ -1,7 +1,11 @@
|
|||
const std = @import("std");
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
const Scanner = @import("scanner.zig").Scanner;
|
||||
|
||||
// const Scanner = @import("scanner.zig").Scanner;
|
||||
const chunk = @import("chunk.zig");
|
||||
|
||||
//const Compiler = @import("compiler.zig").Compiler;
|
||||
|
||||
pub var hadError = false;
|
||||
|
||||
|
@ -11,6 +15,7 @@ fn run(allocator: *Allocator, data: []u8) !void {
|
|||
|
||||
var scanner = try Scanner.init(allocator, data);
|
||||
var tokens = try scanner.scanTokens();
|
||||
|
||||
var it = tokens.iterator();
|
||||
|
||||
while (it.next()) |token| {
|
||||
|
@ -30,6 +35,8 @@ fn run(allocator: *Allocator, data: []u8) !void {
|
|||
}
|
||||
}
|
||||
|
||||
// fn run() !void {}
|
||||
|
||||
pub fn doError(line: usize, message: []const u8) !void {
|
||||
try errorReport(line, "", message);
|
||||
}
|
||||
|
@ -71,22 +78,42 @@ fn runPrompt(allocator: *Allocator) !void {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
pub fn mainOld() anyerror!void {
|
||||
var da = std.heap.DirectAllocator.init();
|
||||
var arena = std.heap.ArenaAllocator.init(&da.allocator);
|
||||
defer arena.deinit();
|
||||
var allocator = &arena.allocator;
|
||||
|
||||
var args_it = std.process.args();
|
||||
|
||||
const jorts_arg0 = try (args_it.next(allocator) orelse {
|
||||
var jorts_arg0 = try (args_it.next(allocator) orelse {
|
||||
// if you ever reach this, tell me what is your os lmao
|
||||
unreachable;
|
||||
});
|
||||
|
||||
const lox_path = try (args_it.next(allocator) orelse {
|
||||
try runPrompt(allocator);
|
||||
return;
|
||||
var lox_path = try (args_it.next(allocator) orelse {
|
||||
// try runPrompt(allocator);
|
||||
unreachable;
|
||||
});
|
||||
|
||||
try runFile(allocator, lox_path);
|
||||
//var vm = VM.init();
|
||||
//try runFile(allocator, lox_path);
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
var da = std.heap.DirectAllocator.init();
|
||||
var arena = std.heap.ArenaAllocator.init(&da.allocator);
|
||||
defer arena.deinit();
|
||||
var allocator = &arena.allocator;
|
||||
|
||||
var stdout_file = try std.io.getStdOut();
|
||||
const stdout = &stdout_file.outStream().stream;
|
||||
|
||||
var chk = try chunk.Chunk.init(allocator);
|
||||
|
||||
// this crashes zig??? lol
|
||||
//var opcode_byte: u8 = @enumToInt(chunk.OpCode.Return);
|
||||
try chk.write(chunk.OpCode.Return);
|
||||
|
||||
try chk.disassemble(stdout, "test chunk");
|
||||
}
|
||||
|
|
7
src/vm.zig
Normal file
7
src/vm.zig
Normal file
|
@ -0,0 +1,7 @@
|
|||
const Chunk = @import("chunk.zig");
|
||||
|
||||
pub const VM = struct {
|
||||
chunk: *Chunk,
|
||||
|
||||
pub fn init() VM {}
|
||||
};
|
Loading…
Reference in a new issue