aboutsummaryrefslogtreecommitdiffstats
path: root/clox/src/compiler.c
diff options
context:
space:
mode:
authorGravatar Tom Willemse2022-08-13 00:04:31 -0700
committerGravatar Tom Willemse2022-08-13 00:04:31 -0700
commit4e77cfa61262a03055aeb273108fd9a49e8dfa4f (patch)
tree9c736eb135e8bd1143e0e4289e87edd2324586c9 /clox/src/compiler.c
parentf4185577e56a893b9375bc94d71bef5f9eb51891 (diff)
downloadcrafting-interpreters-4e77cfa61262a03055aeb273108fd9a49e8dfa4f.tar.gz
crafting-interpreters-4e77cfa61262a03055aeb273108fd9a49e8dfa4f.zip
Chapter 27.4
Diffstat (limited to 'clox/src/compiler.c')
-rw-r--r--clox/src/compiler.c14
1 files changed, 13 insertions, 1 deletions
diff --git a/clox/src/compiler.c b/clox/src/compiler.c
index b21a53d..6ab4855 100644
--- a/clox/src/compiler.c
+++ b/clox/src/compiler.c
@@ -307,6 +307,18 @@ static void call(bool canAssign) {
emitBytes(OP_CALL, argCount);
}
+static void dot(bool canAssign) {
+ consume(TOKEN_IDENTIFIER, "Expect property name after '.'.");
+ uint8_t name = identifierConstant(&parser.previous);
+
+ if (canAssign && match(TOKEN_EQUAL)) {
+ expression();
+ emitBytes(OP_SET_PROPERTY, name);
+ } else {
+ emitBytes(OP_GET_PROPERTY, name);
+ }
+}
+
static void literal(bool canAssign) {
switch (parser.previous.type) {
case TOKEN_FALSE:
@@ -689,7 +701,7 @@ ParseRule rules[] = {
[TOKEN_LEFT_BRACE] = {NULL, NULL, PREC_NONE},
[TOKEN_RIGHT_BRACE] = {NULL, NULL, PREC_NONE},
[TOKEN_COMMA] = {NULL, NULL, PREC_NONE},
- [TOKEN_DOT] = {NULL, NULL, PREC_NONE},
+ [TOKEN_DOT] = {NULL, dot, PREC_CALL},
[TOKEN_MINUS] = {unary, binary, PREC_TERM},
[TOKEN_PLUS] = {NULL, binary, PREC_TERM},
[TOKEN_SEMICOLON] = {NULL, NULL, PREC_NONE},