diff options
Diffstat (limited to 'clox/src/compiler.c')
-rw-r--r-- | clox/src/compiler.c | 14 |
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}, |