split main code into a runner struct
This commit is contained in:
parent
a44f04c7c8
commit
c19f6df834
3 changed files with 44 additions and 21 deletions
26
src/main.zig
26
src/main.zig
|
@ -1,6 +1,6 @@
|
|||
const std = @import("std");
|
||||
|
||||
const scanners = @import("scanner.zig");
|
||||
const runners = @import("runner.zig");
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
|
@ -9,30 +9,14 @@ pub const Result = error{
|
|||
CompileError,
|
||||
};
|
||||
|
||||
pub const StdOut = *std.io.OutStream(std.fs.File.WriteError);
|
||||
|
||||
fn run(allocator: *Allocator, data: []u8) !void {
|
||||
var stdout_file = try std.io.getStdOut();
|
||||
const stdout = &stdout_file.outStream().stream;
|
||||
|
||||
var scanner = scanners.Scanner.init(allocator, data);
|
||||
|
||||
while (true) {
|
||||
var tok_opt = scanner.nextToken() catch |err| {
|
||||
try stdout.print(
|
||||
"error at '{}': {}\n",
|
||||
scanner.currentLexeme(),
|
||||
err,
|
||||
);
|
||||
|
||||
return Result.CompileError;
|
||||
};
|
||||
|
||||
if (tok_opt) |tok| {
|
||||
if (tok.ttype == .EOF) break;
|
||||
try stdout.print("{x}\n", tok);
|
||||
}
|
||||
}
|
||||
|
||||
return Result.Ok;
|
||||
var runner = runners.Runner.init(allocator, stdout);
|
||||
return runner.execute(data);
|
||||
}
|
||||
|
||||
fn runFile(allocator: *Allocator, path: []const u8) !void {
|
||||
|
|
1
src/parser.zig
Normal file
1
src/parser.zig
Normal file
|
@ -0,0 +1 @@
|
|||
pub const Parser = struct {};
|
38
src/runner.zig
Normal file
38
src/runner.zig
Normal file
|
@ -0,0 +1,38 @@
|
|||
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;
|
||||
}
|
||||
};
|
Loading…
Reference in a new issue