From df1985ae846b35e14dc29dc7cf34861237d814c6 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Thu, 21 Oct 2021 20:00:19 -0700 Subject: Cahapter 22.1 --- clox/src/common.h | 2 ++ clox/src/compiler.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) (limited to 'clox') diff --git a/clox/src/common.h b/clox/src/common.h index 25fc22f..5cd0cf4 100644 --- a/clox/src/common.h +++ b/clox/src/common.h @@ -8,4 +8,6 @@ #define DEBUG_PRINT_CODE #define DEBUG_TRACE_EXECUTION +#define UINT8_COUNT (UINT8_MAX + 1) + #endif diff --git a/clox/src/compiler.c b/clox/src/compiler.c index 0fc41b8..6a20a20 100644 --- a/clox/src/compiler.c +++ b/clox/src/compiler.c @@ -38,7 +38,19 @@ typedef struct { Precedence precedence; } ParseRule; +typedef struct { + Token name; + int depth; +} Local; + +typedef struct { + Local locals[UINT8_COUNT]; + int localCount; + int scopeDepth; +} Compiler; + Parser parser; +Compiler *current = NULL; Chunk *compilingChunk; static Chunk *currentChunk() { return compilingChunk; } @@ -122,6 +134,12 @@ static void emitConstant(Value value) { emitBytes(OP_CONSTANT, makeConstant(value)); } +static void initCompiler(Compiler *compiler) { + compiler->localCount = 0; + compiler->scopeDepth = 0; + current = compiler; +} + static void endCompiler() { emitReturn(); #ifdef DEBUG_PRINT_CODE @@ -398,6 +416,8 @@ static ParseRule *getRule(TokenType type) { return &rules[type]; } bool compile(const char *source, Chunk *chunk) { initScanner(source); + Compiler compiler; + initCompiler(&compiler); compilingChunk = chunk; parser.hadError = false; -- cgit v1.2.3-54-g00ecf