refactor: relocating constants
This commit is contained in:
parent
893b229312
commit
eb265777d9
23
build.zig
23
build.zig
@ -15,20 +15,6 @@ pub fn build(b: *std.Build) void {
|
||||
// set a preferred release mode, allowing the user to decide how to optimize.
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
const lib = b.addStaticLibrary(.{
|
||||
.name = "zlox",
|
||||
// In this case the main source file is merely a path, however, in more
|
||||
// complicated build scripts, this could be a generated file.
|
||||
.root_source_file = b.path("src/root.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
// This declares intent for the library to be installed into the standard
|
||||
// location when the user invokes the "install" step (the default step when
|
||||
// running `zig build`).
|
||||
b.installArtifact(lib);
|
||||
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "zlox",
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
@ -66,14 +52,6 @@ pub fn build(b: *std.Build) void {
|
||||
|
||||
// Creates a step for unit testing. This only builds the test executable
|
||||
// but does not run it.
|
||||
const lib_unit_tests = b.addTest(.{
|
||||
.root_source_file = b.path("src/root.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
|
||||
|
||||
const exe_unit_tests = b.addTest(.{
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
.target = target,
|
||||
@ -86,6 +64,5 @@ pub fn build(b: *std.Build) void {
|
||||
// the `zig build --help` menu, providing a way for the user to request
|
||||
// running the unit tests.
|
||||
const test_step = b.step("test", "Run unit tests");
|
||||
test_step.dependOn(&run_lib_unit_tests.step);
|
||||
test_step.dependOn(&run_exe_unit_tests.step);
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ const std = @import("std");
|
||||
const debug = std.debug;
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const constants = @import("./constant.zig");
|
||||
|
||||
const Obj = @import("./object.zig").Obj;
|
||||
const ObjType = @import("./object.zig").ObjType;
|
||||
|
||||
@ -161,7 +163,7 @@ const Parser = struct {
|
||||
|
||||
fn make_constant(self: *Parser, value: Value) !u8 {
|
||||
const constant = try self.chunk.add_constant(value);
|
||||
if (constant > 256) {
|
||||
if (constant > constants.UINT8_MAX) {
|
||||
self.error_msg("Too many constants in one chunk.");
|
||||
return 0;
|
||||
}
|
||||
@ -521,7 +523,7 @@ const Parser = struct {
|
||||
}
|
||||
|
||||
fn add_local(self: *Parser, token: Token) void {
|
||||
if (self.compiler.local_count == 256) {
|
||||
if (self.compiler.local_count == constants.UINT8_COUNT) {
|
||||
self.error_msg("Too many local variables in function.");
|
||||
return;
|
||||
}
|
||||
@ -589,7 +591,7 @@ const Parser = struct {
|
||||
fn patch_jump(self: *Parser, offset: usize) void {
|
||||
const jump = self.chunk.count - offset - 2;
|
||||
|
||||
if (jump > 65535) {
|
||||
if (jump > constants.UINT16_MAX) {
|
||||
self.error_msg("Too much code to jump over.");
|
||||
}
|
||||
|
||||
@ -639,7 +641,7 @@ const Parser = struct {
|
||||
try self.emit_byte(@intFromEnum(OpCode.OP_LOOP));
|
||||
|
||||
const offset = self.chunk.count - loop_start + 2;
|
||||
if (offset > 65536) {
|
||||
if (offset > constants.UINT16_MAX) {
|
||||
self.error_msg("Loop body too large.");
|
||||
}
|
||||
|
||||
@ -697,7 +699,7 @@ const Parser = struct {
|
||||
};
|
||||
|
||||
const Compiler = struct {
|
||||
locals: [256]Local,
|
||||
locals: [constants.UINT8_COUNT]Local,
|
||||
local_count: usize,
|
||||
scope_depth: usize,
|
||||
|
||||
|
10
src/constant.zig
Normal file
10
src/constant.zig
Normal file
@ -0,0 +1,10 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub const TABLE_MAX_LOAD = 0.75;
|
||||
|
||||
pub const UINT8_MAX = std.math.maxInt(u8);
|
||||
pub const UINT16_MAX = std.math.maxInt(u16);
|
||||
|
||||
pub const UINT8_COUNT = UINT8_MAX + 1;
|
||||
|
||||
pub const STACK_MAX = 256;
|
10
src/root.zig
10
src/root.zig
@ -1,10 +0,0 @@
|
||||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
|
||||
export fn add(a: i32, b: i32) i32 {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
test "basic add functionality" {
|
||||
try testing.expect(add(3, 7) == 10);
|
||||
}
|
@ -2,14 +2,14 @@ const std = @import("std");
|
||||
const debug = std.debug;
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const constants = @import("./constant.zig");
|
||||
|
||||
const Obj = @import("./object.zig").Obj;
|
||||
const Value = @import("./values.zig").Value;
|
||||
|
||||
const grow_capacity = @import("./utils.zig").grow_capacity;
|
||||
const compute_hash = @import("./utils.zig").compute_hash;
|
||||
|
||||
const TABLE_MAX_LOAD = 0.75;
|
||||
|
||||
const Entry = struct {
|
||||
key: ?*Obj.String,
|
||||
value: Value,
|
||||
@ -42,7 +42,7 @@ pub const Table = struct {
|
||||
const current_count: f32 = @floatFromInt(self.count + 1);
|
||||
const current_capacity: f32 = @floatFromInt(self.capacity);
|
||||
|
||||
if (current_count > current_capacity * TABLE_MAX_LOAD) {
|
||||
if (current_count > current_capacity * constants.TABLE_MAX_LOAD) {
|
||||
const capacity = grow_capacity(self.capacity);
|
||||
self.adjust_capacity(capacity);
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ const std = @import("std");
|
||||
const debug = std.debug;
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const constants = @import("./constant.zig");
|
||||
|
||||
const Chunk = @import("./chunk.zig").Chunk;
|
||||
const OpCode = @import("./opcode.zig").OpCode;
|
||||
const Value = @import("./values.zig").Value;
|
||||
@ -15,8 +17,6 @@ const DEBUG_TRACE_EXECUTION = @import("./main.zig").DEBUG_TRACE_EXECUTION;
|
||||
|
||||
const print_value = @import("./values.zig").print_value;
|
||||
|
||||
const STACK_MAX = 256;
|
||||
|
||||
pub const InterpretResult = enum {
|
||||
OK,
|
||||
COMPILE_ERROR,
|
||||
|
Loading…
Reference in New Issue
Block a user