diff --git a/src/compiler.zig b/src/compiler.zig index a223c67..e029b9c 100644 --- a/src/compiler.zig +++ b/src/compiler.zig @@ -1,42 +1,15 @@ 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 init(allocator: *Allocator, source: []const u8) Compiler { + return Compiler{ .src = source, .allocator = allocator }; } - 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; - } - } + pub fn compile(self: *Compiler) void {} }; diff --git a/src/main.zig b/src/main.zig index 4611d6f..44463ca 100644 --- a/src/main.zig +++ b/src/main.zig @@ -17,7 +17,7 @@ fn run(allocator: *Allocator, data: []u8) !InterpretResult { const stdout = &stdout_file.outStream().stream; var vmach = try vm.VM.init(allocator, stdout, data, true); - return try vmach.interpret(); + return vmach.interpret(); } pub fn doError(line: usize, message: []const u8) !void { diff --git a/src/new_scanner.zig b/src/new_scanner.zig deleted file mode 100644 index 88b0bab..0000000 --- a/src/new_scanner.zig +++ /dev/null @@ -1,25 +0,0 @@ -const std = @import("std"); -const tokens = @import("token.zig"); - -const Allocator = std.mem.Allocator; - -pub const Scanner = struct { - source: []const u8, - - start: usize = 0, - current: usize = 0, - line: usize = 1, - - allocator: *Allocator, - - pub fn init(allocator: *Allocator, data: []const u8) Scanner { - return Scanner{ - .allocator = allocator, - .source = data, - }; - } - - pub fn scanToken(self: *Scanner) tokens.Token { - return tokens.Token{}; - } -}; diff --git a/src/token.zig b/src/token.zig index 308e60e..9e88ee1 100644 --- a/src/token.zig +++ b/src/token.zig @@ -50,8 +50,58 @@ pub const TokenType = enum(u6) { EOF, }; -pub const Token = struct { - ttype: TokenType = TokenType.EOF, - lexeme: []const u8 = ""[0..], - line: usize = 0, +pub fn TokenFactory( + comptime T: type, +) type { + return struct { + const Self = @This(); + + ttype: TokenType, + lexeme: []u8, + line: usize, + literal: T, + + pub fn init( + ttype: TokenType, + lexeme: []u8, + line: usize, + literal: T, + ) Self { + return Self{ + .ttype = ttype, + .lexeme = lexeme, + .line = line, + .literal = literal, + }; + } + + pub fn printToken(self: Self, stdout: var) !void { + if (T == void) { + try stdout.print( + "Token(type={x}, lexeme='{}', line={})\n", + self.ttype, + self.lexeme, + self.line, + ); + } else { + try stdout.print( + "Token(type={x}, lexeme='{}', line={} literal='{}')\n", + self.ttype, + self.lexeme, + self.line, + self.literal, + ); + } + } + }; +} + +pub const SimpleToken = TokenFactory(void); +pub const SliceToken = TokenFactory([]u8); +pub const NumberToken = TokenFactory(f32); + +pub const Token = union(enum) { + Simple: SimpleToken, + Slice: SliceToken, + Number: NumberToken, }; diff --git a/src/vm.zig b/src/vm.zig index 175414c..d09511a 100644 --- a/src/vm.zig +++ b/src/vm.zig @@ -7,7 +7,7 @@ const Chunk = chunk.Chunk; const Value = value.Value; const Compiler = compiler.Compiler; -pub const StdOut = *std.io.OutStream(std.fs.File.WriteError); +const StdOut = *std.io.OutStream(std.fs.File.WriteError); pub const InterpretResult = enum { Ok, @@ -159,14 +159,14 @@ pub const VM = struct { } } - pub fn interpret(self: *VM) !InterpretResult { + pub fn interpret(self: *VM) InterpretResult { //self.ip = 0; //self.debug("VM start\n"); //var res = try self.run(); //self.debug("VM end\n"); //return res; - var cmpr = Compiler.init(self.allocator, self.stdout, self.src); - try cmpr.compile(); + var cmpr = Compiler.init(self.allocator, self.src); + cmpr.compile(); return InterpretResult.Ok; }