aboutsummaryrefslogtreecommitdiffstats
path: root/clox/src/compiler.c
diff options
context:
space:
mode:
Diffstat (limited to 'clox/src/compiler.c')
-rw-r--r--clox/src/compiler.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/clox/src/compiler.c b/clox/src/compiler.c
index 9dd7927..f125c10 100644
--- a/clox/src/compiler.c
+++ b/clox/src/compiler.c
@@ -138,6 +138,7 @@ static ParseRule *getRule(TokenType type);
static void parsePrecedence(Precedence precedence);
static uint8_t parseVariable(const char *name);
static void defineVariable(uint8_t global);
+static uint8_t identifierConstant(Token *name);
static void binary() {
TokenType operatorType = parser.previous.type;
@@ -276,6 +277,15 @@ static void string() {
copyString(parser.previous.start + 1, parser.previous.length - 2)));
}
+static void namedVariable(Token name) {
+ uint8_t arg = identifierConstant(&name);
+ emitBytes(OP_GET_GLOBAL, arg);
+}
+
+static void variable() {
+ namedVariable(parser.previous);
+}
+
static void unary() {
TokenType operatorType = parser.previous.type;
@@ -320,7 +330,7 @@ ParseRule rules[] = {
[TOKEN_GREATER_EQUAL] = {NULL, binary, PREC_COMPARISON},
[TOKEN_LESS] = {NULL, binary, PREC_COMPARISON},
[TOKEN_LESS_EQUAL] = {NULL, binary, PREC_COMPARISON},
- [TOKEN_IDENTIFIER] = {NULL, NULL, PREC_NONE},
+ [TOKEN_IDENTIFIER] = {variable, NULL, PREC_NONE},
[TOKEN_STRING] = {string, NULL, PREC_NONE},
[TOKEN_NUMBER] = {number, NULL, PREC_NONE},
[TOKEN_AND] = {NULL, NULL, PREC_NONE},