forked from luna/jorts
add other bytecode emitters
This commit is contained in:
parent
2736bee8d8
commit
230fef20b5
1 changed files with 36 additions and 3 deletions
|
@ -3,12 +3,15 @@ const scanner = @import("new_scanner.zig");
|
||||||
const vm = @import("vm.zig");
|
const vm = @import("vm.zig");
|
||||||
const chunks = @import("chunk.zig");
|
const chunks = @import("chunk.zig");
|
||||||
const tokens = @import("token.zig");
|
const tokens = @import("token.zig");
|
||||||
|
const values = @import("value.zig");
|
||||||
|
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
const Scanner = scanner.Scanner;
|
const Scanner = scanner.Scanner;
|
||||||
const Chunk = chunks.Chunk;
|
const Chunk = chunks.Chunk;
|
||||||
const Token = tokens.Token;
|
const Token = tokens.Token;
|
||||||
const TokenType = tokens.TokenType;
|
const TokenType = tokens.TokenType;
|
||||||
|
const Value = values.Value;
|
||||||
|
const OpCode = chunks.OpCode;
|
||||||
|
|
||||||
pub const Parser = struct {
|
pub const Parser = struct {
|
||||||
previous: Token = undefined,
|
previous: Token = undefined,
|
||||||
|
@ -101,14 +104,44 @@ pub const Compiler = struct {
|
||||||
try self.emitByte(byte2);
|
try self.emitByte(byte2);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn writeReturn(self: *Compiler) !void {
|
fn emitReturn(self: *Compiler) !void {
|
||||||
try self.emitByte(chunks.OpCode.Return);
|
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 {
|
fn end(self: *Compiler) !void {
|
||||||
try self.writeReturn();
|
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
|
/// Compile the source given when initializing the compiler
|
||||||
/// into the given chunk.
|
/// into the given chunk.
|
||||||
pub fn compile(self: *Compiler, chunk: *Chunk) !bool {
|
pub fn compile(self: *Compiler, chunk: *Chunk) !bool {
|
||||||
|
|
Loading…
Reference in a new issue