aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom Willemse2021-10-21 20:18:48 -0700
committerGravatar Tom Willemse2021-10-21 20:18:48 -0700
commit42a7e262927bfdbbfa15d688610eca37fd0d12e2 (patch)
treecf955c7b2c1aa4ea8724d35ec7745417548c7835
parentdf1985ae846b35e14dc29dc7cf34861237d814c6 (diff)
downloadcrafting-interpreters-42a7e262927bfdbbfa15d688610eca37fd0d12e2.tar.gz
crafting-interpreters-42a7e262927bfdbbfa15d688610eca37fd0d12e2.zip
Chapter 22.2
-rw-r--r--clox/src/compiler.c16
1 files changed, 16 insertions, 0 deletions
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();
}