jorts/src/compiler.zig

43 lines
1.1 KiB
Zig

const std = @import("std");
const scanner = @import("new_scanner.zig");
const vm = @import("vm.zig");
const Allocator = std.mem.Allocator;
const TokenType = @import("token.zig").TokenType;
pub const Compiler = struct {
src: []const u8,
stdout: vm.StdOut,
allocator: *Allocator,
pub fn init(
allocator: *Allocator,
stdout: vm.StdOut,
source: []const u8,
) Compiler {
return Compiler{
.src = source,
.allocator = allocator,
.stdout = stdout,
};
}
pub fn compile(self: *Compiler) !void {
var scanr = scanner.Scanner.init(self.allocator, self.src);
var line: usize = 0;
while (true) {
var token = scanr.scanToken();
if (token.line != line) {
try self.stdout.print("{} ", token.line);
line = token.line;
} else {
try self.stdout.print(" | ");
}
try self.stdout.print("{} '{}'\n", token.ttype, token.lexeme);
if (token.ttype == TokenType.EOF) break;
}
}
};