39 lines
1,015 B
Zig
39 lines
1,015 B
Zig
|
const std = @import("std");
|
||
|
const scanners = @import("scanner.zig");
|
||
|
const main = @import("main.zig");
|
||
|
|
||
|
const Allocator = std.mem.Allocator;
|
||
|
const Result = main.Result;
|
||
|
|
||
|
pub const Runner = struct {
|
||
|
allocator: *Allocator,
|
||
|
stdout: main.StdOut,
|
||
|
|
||
|
pub fn init(allocator: *Allocator, stdout: var) Runner {
|
||
|
return Runner{ .allocator = allocator, .stdout = stdout };
|
||
|
}
|
||
|
|
||
|
pub fn execute(self: *Runner, code: []u8) !void {
|
||
|
var scanner = scanners.Scanner.init(self.allocator, code);
|
||
|
|
||
|
while (true) {
|
||
|
var tok_opt = scanner.nextToken() catch |err| {
|
||
|
try self.stdout.print(
|
||
|
"error at '{}': {}\n",
|
||
|
scanner.currentLexeme(),
|
||
|
err,
|
||
|
);
|
||
|
|
||
|
return Result.CompileError;
|
||
|
};
|
||
|
|
||
|
if (tok_opt) |tok| {
|
||
|
if (tok.ttype == .EOF) break;
|
||
|
try self.stdout.print("{x}\n", tok);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return Result.Ok;
|
||
|
}
|
||
|
};
|