aboutsummaryrefslogtreecommitdiffstats
path: root/clox/src/compiler.c
diff options
context:
space:
mode:
authorGravatar Tom Willemse2021-10-21 20:00:19 -0700
committerGravatar Tom Willemse2021-10-21 20:00:19 -0700
commitdf1985ae846b35e14dc29dc7cf34861237d814c6 (patch)
tree661791a9d8a951a3b73efd509d4cc67d1fb64634 /clox/src/compiler.c
parent26b418d25485f67b06fa70f3c32c151c26572f13 (diff)
downloadcrafting-interpreters-df1985ae846b35e14dc29dc7cf34861237d814c6.tar.gz
crafting-interpreters-df1985ae846b35e14dc29dc7cf34861237d814c6.zip
Cahapter 22.1
Diffstat (limited to 'clox/src/compiler.c')
-rw-r--r--clox/src/compiler.c20
1 files changed, 20 insertions, 0 deletions
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;