From 42a7e262927bfdbbfa15d688610eca37fd0d12e2 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Thu, 21 Oct 2021 20:18:48 -0700 Subject: [PATCH] Chapter 22.2 --- clox/src/compiler.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/clox/src/compiler.c b/clox/src/compiler.c index 6a20a20..1e48d22 100644 --- a/clox/src/compiler.c +++ b/clox/src/compiler.c @@ -149,6 +149,10 @@ static void endCompiler() { #endif } +static void beginScope() { current->scopeDepth++; } + +static void endScope() { current->scopeDepth--; } + static void expression(); static void statement(); static void declaration(); @@ -217,6 +221,14 @@ static void literal(bool canAssign) { static void expression() { parsePrecedence(PREC_ASSIGNMENT); } +static void block() { + while (!check(TOKEN_RIGHT_BRACE) && !check(TOKEN_EOF)) { + declaration(); + } + + consume(TOKEN_RIGHT_BRACE, "Expect '}' after block."); +} + static void varDeclaration() { uint8_t global = parseVariable("Expect variable name."); @@ -280,6 +292,10 @@ static void declaration() { static void statement() { if (match(TOKEN_PRINT)) { printStatement(); + } else if (match(TOKEN_LEFT_BRACE)) { + beginScope(); + block(); + endScope(); } else { expressionStatement(); }