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(); }