Skip to content

ch26 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
13 changes: 13 additions & 0 deletions examples/ch25-counter-closure.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fun initCounter() {
var privateCounter = 0;
fun incr() {
privateCounter = privateCounter + 1;
return privateCounter;
}
return incr;
}

var count = initCounter();
print(count());
print(count());
print(count());
25 changes: 21 additions & 4 deletions src/compiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ const FunctionType = enum {
Script,
};

const Compiler = struct {
pub const Compiler = struct {
function: *ObjFunction,
type_: FunctionType,

Expand All @@ -142,7 +142,7 @@ const Compiler = struct {
upvalues: [U8_COUNT]Upvalue,
scopeDepth: i16,

pub fn init(type_: FunctionType, om: *ObjManager, enclosing: *Compiler) !Compiler {
pub fn init(type_: FunctionType, om: *ObjManager, enclosing: ?*Compiler) !Compiler {
var c = Compiler{
.function = undefined, // see 24.2.1 .. revisit in light of garbage collector
.type_ = type_,
Expand Down Expand Up @@ -171,10 +171,13 @@ pub fn compile(source: []u8, om: *ObjManager) !*ObjFunction {
// startup -- could be comptime TODO
initRules();

// setup the compiler
// save reference to object manager
objManager = om;
var compiler = try Compiler.init(FunctionType.Script, objManager, current);

// setup the compiler
var compiler = try Compiler.init(FunctionType.Script, objManager, null);
current = &compiler;
objManager.isCompilerInitialized = true;

scanner = Scanner.init(source);
parser = Parser{
Expand Down Expand Up @@ -900,6 +903,7 @@ fn endCompiler() *ObjFunction {
} else {
// we're at the top of the stack, so we're done
// unsafe warning warning
objManager.isCompilerInitialized = false;
current = undefined;
}

Expand Down Expand Up @@ -989,3 +993,16 @@ fn emitByte(byte: u8) void {
fn tokenString(token: Token) []const u8 {
return scanner.source[token.start .. token.start + token.length];
}

// markCompilerRoots is for garbage collection
pub fn markCompilerRoots() void {
if (debug.LOG_GC) print("-- gc: mark compiler roots\n", .{});

var compiler: *Compiler = current;
compiler.function.markObject(objManager);

while (compiler.enclosing != null) {
compiler = compiler.enclosing.?;
compiler.function.markObject(objManager);
}
}
10 changes: 7 additions & 3 deletions src/debug.zig
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// Debug Flags

// global flag to enable/disable all
const enable_debugging = false;
const enable_debugging = true;

// VM (Runtime)
pub const TRACE_EXECUTION_INCLUDE_INSTRUCTIONS = enable_debugging and true;
pub const TRACE_EXECUTION_INCLUDE_INSTRUCTIONS = enable_debugging and false;
pub const TRACE_EXECUTION_SHOW_GET_SET_VARS = enable_debugging and false;
pub const TRACE_EXECUTION_PRINT_STACK = enable_debugging and true;
pub const TRACE_EXECUTION_PRINT_STACK = enable_debugging and false;

// Compiler
pub const PRINT_CODE_AFTER_END_COMPILER = enable_debugging and false; // TODO: try out comptime
pub const TRACE_PARSER = enable_debugging and false;

// Object Manager (esp Garbage Collection)
pub const STRESS_GC = enable_debugging and true;
pub const LOG_GC = enable_debugging and true;
Loading