diff --git a/clox/.gitignore b/clox/.gitignore new file mode 100644 index 0000000..1e4d659 --- /dev/null +++ b/clox/.gitignore @@ -0,0 +1,2 @@ +*.o +clox diff --git a/clox/clox b/clox/clox index 2c5a0e7..e1e7feb 100755 Binary files a/clox/clox and b/clox/clox differ diff --git a/clox/compiler.c b/clox/compiler.c index 7915530..68bd0d0 100644 --- a/clox/compiler.c +++ b/clox/compiler.c @@ -99,6 +99,24 @@ static void emitBytes(uint8_t byte1, uint8_t byte2) { emitByte(byte2); } +static uint8_t makeConstant(Value value) { + int constant = addConstant(currentChunk(), value); + if (constant > UINT8_MAX) { + error("Too many constants in one chunk."); + return 0; + } + + return (uint8_t)constant; +} + +static void emitReturn() { + emitByte(OP_RETURN); +} + +static void emitConstant(Value value) { + emitBytes(OP_CONSTANT, makeConstant(value)); +} + static void endCompiler() { emitReturn(); #ifdef DEBUG_PRINT_CODE @@ -125,23 +143,33 @@ static void binary() { } } -static void emitReturn() { - emitByte(OP_RETURN); +static void expression() { + // What goes here? + parsePrecedence(PREC_ASSIGNMENT); +} +static void grouping() { + expression(); + consume(TOKEN_RIGHT_PAREN, "Expect ')' after expression."); } -static uint8_t makeConstant(Value value) { - int constant = addConstant(currentChunk(), value); - if (constant > UINT8_MAX) { - error("Too many constants in one chunk."); - return 0; +static void number() { + double value = strtod(parser.previous.start, NULL); + emitConstant(value); +} + +static void unary() { + TokenType operatorType = parser.previous.type; + + // Compile the operand. + parsePrecedence(PREC_UNARY); + // Emit the operator instruction. + switch (operatorType) { + case TOKEN_MINUS: emitByte(OP_NEGATE); break; + default: return; // Unreachable. } - - return (uint8_t)constant; } -static void emitConstant(Value value) { - emitBytes(OP_CONSTANT, makeConstant(value)); -} + static void parsePrecedence(Precedence precedence) { advance(); @@ -207,32 +235,6 @@ static ParseRule* getRule(TokenType type) { return &rules[type]; } -static void expression() { - // What goes here? - parsePrecedence(PREC_ASSIGNMENT); -} -static void grouping() { - expression(); - consume(TOKEN_RIGHT_PAREN, "Expect ')' after expression."); -} - -static void number() { - double value = strtod(parser.previous.start, NULL); - emitConstant(value); -} - -static void unary() { - TokenType operatorType = parser.previous.type; - - // Compile the operand. - parsePrecedence(PREC_UNARY); - // Emit the operator instruction. - switch (operatorType) { - case TOKEN_MINUS: emitByte(OP_NEGATE); break; - default: return; // Unreachable. - } -} - bool compile(const char* source, Chunk* chunk) { initScanner(source); compilingChunk = chunk; diff --git a/clox/compiler.o b/clox/compiler.o index dd7dd1f..7e02c78 100644 Binary files a/clox/compiler.o and b/clox/compiler.o differ