|
|
@@ -74,6 +74,7 @@ Compiler *current = NULL;
|
|
|
|
|
|
static void parsePrecedence(Precedence);
|
|
|
static uint8_t parseVariable(const char *);
|
|
|
+static void declareVariable();
|
|
|
static void defineVariable(uint8_t);
|
|
|
static uint8_t identifierConstant(Token *name);
|
|
|
static bool identifiersEqual(Token *a, Token *b);
|
|
|
@@ -210,6 +211,17 @@ static void function(FunctionType type) {
|
|
|
emitByte(compiler.upvalues[i].index);
|
|
|
}
|
|
|
}
|
|
|
+static void classDeclaration() {
|
|
|
+ consume(TOKEN_IDENTIFIER, "Expect class name.");
|
|
|
+ uint8_t nameConstant = identifierConstant(&parser.previous);
|
|
|
+ declareVariable();
|
|
|
+
|
|
|
+ emitBytes(OP_CLASS, nameConstant);
|
|
|
+ defineVariable(nameConstant);
|
|
|
+
|
|
|
+ consume(TOKEN_LEFT_BRACE, "Expect '{' before class body.");
|
|
|
+ consume(TOKEN_RIGHT_BRACE, "Expect '}' after class body.");
|
|
|
+}
|
|
|
static void funDeclaration() {
|
|
|
uint8_t global = parseVariable("Expect function name.");
|
|
|
markInitialized();
|
|
|
@@ -385,7 +397,9 @@ static void synchronize() {
|
|
|
/// | varDecl
|
|
|
/// | statement ;
|
|
|
static void declaration() {
|
|
|
- if (match(TOKEN_FUN)) {
|
|
|
+ if (match(TOKEN_CLASS)) {
|
|
|
+ classDeclaration();
|
|
|
+ } else if (match(TOKEN_FUN)) {
|
|
|
funDeclaration();
|
|
|
} else if (match(TOKEN_VAR)) {
|
|
|
varDeclaration();
|