jorts/src/compiler.zig

158 lines
4.2 KiB
Zig

const std = @import("std");
const scanner = @import("new_scanner.zig");
const vm = @import("vm.zig");
const chunks = @import("chunk.zig");
const tokens = @import("token.zig");
const values = @import("value.zig");
const Allocator = std.mem.Allocator;
const Scanner = scanner.Scanner;
const Chunk = chunks.Chunk;
const Token = tokens.Token;
const TokenType = tokens.TokenType;
const Value = values.Value;
const OpCode = chunks.OpCode;
pub const Parser = struct {
previous: Token = undefined,
current: Token = undefined,
// TODO are those needed
hadError: bool = false,
panicMode: bool = false,
};
pub const Compiler = struct {
src: []const u8,
stdout: vm.StdOut,
allocator: *Allocator,
parser: Parser,
scanr: Scanner = undefined,
chunk: *chunks.Chunk,
pub fn init(
allocator: *Allocator,
chunk: *chunks.Chunk,
stdout: vm.StdOut,
source: []const u8,
) Compiler {
return Compiler{
.src = source,
.chunk = chunk,
.allocator = allocator,
.stdout = stdout,
.parser = Parser{},
};
}
fn errorAt(self: *Compiler, token: Token, msg: []const u8) void {
if (self.parser.panicMode) return;
self.parser.panicMode = true;
std.debug.warn("[line {}] Error", token.line);
if (token.ttype == TokenType.EOF) {
std.debug.warn(" at end");
} else {
std.debug.warn(" at '{}'", token.lexeme);
}
std.debug.warn(": {}\n", msg);
self.parser.hadError = true;
}
fn errorCurrent(self: *Compiler, msg: []const u8) void {
self.errorAt(self.parser.current, msg);
}
fn errorPrevious(self: *Compiler, msg: []const u8) void {
self.errorAt(self.parser.previous, msg);
}
fn advance(self: *Compiler) !void {
self.parser.previous = self.parser.current;
while (true) {
var token_opt = try self.scanr.scanToken();
if (token_opt) |token| {
self.parser.current = token;
break;
} else {
self.errorCurrent(self.parser.current.lexeme);
}
}
}
fn consume(self: *Compiler, ttype: TokenType, msg: []const u8) !void {
if (self.parser.current.ttype == ttype) {
try self.advance();
return;
}
self.errorCurrent(msg);
}
fn currentChunk(self: *Compiler) *chunks.Chunk {
return self.chunk;
}
fn emitByte(self: *Compiler, byte: u8) !void {
try self.currentChunk().write(byte, self.parser.previous.line);
}
fn emitBytes(self: *Compiler, byte1: u8, byte2: u82) !void {
try self.emitByte(byte1);
try self.emitByte(byte2);
}
fn emitReturn(self: *Compiler) !void {
try self.emitByte(OpCode.Return);
}
fn emitConstant(self: *Compiler, value: Value) !void {
try self.currentChunk().writeConstant(
value,
self.parser.previous.line,
);
}
fn end(self: *Compiler) !void {
try self.emitReturn();
}
fn grouping(self: *Compiler) !void {
try self.expression();
try self.consume(.RIGHT_PAREN, "Expect ')' after expression.");
}
/// Emits bytecode for a number being loaded into the code.
fn number(self: *Compiler) !void {
var value: f64 = try std.fmt.parseFloat(f64, parser.previous.lexeme);
try self.emitConstant(value);
}
/// Emits bytecode for a given unary.
fn unary(self: *Compiler) !void {
var ttype = self.parser.previous.ttype;
try self.expression();
switch (ttype) {
.MINUS => try self.emitByte(OpCode.Negate),
else => unreachable,
}
}
fn expression(self: *Compiler) !void {}
/// Compile the source given when initializing the compiler
/// into the given chunk.
pub fn compile(self: *Compiler, chunk: *Chunk) !bool {
self.scanr = try scanner.Scanner.init(self.allocator, self.src);
try self.advance();
//try self.expression();
try self.consume(.EOF, "Expect end of expression.");
try self.end();
return !self.parser.hadError;
}
};