[Chapter17&Scanning] - Fixing the compilation

This commit is contained in:
Adnan Ioricce 2024-10-17 13:04:21 -03:00
parent 4c96750dbe
commit 202123cdf1
4 changed files with 42 additions and 38 deletions

2
clox/.gitignore vendored Normal file

@ -0,0 +1,2 @@
*.o
clox

BIN
clox/clox

Binary file not shown.

@ -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;

Binary file not shown.