[Chapter17&Scanning] - Fixing the compilation
This commit is contained in:
parent
4c96750dbe
commit
202123cdf1
2
clox/.gitignore
vendored
Normal file
2
clox/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
*.o
|
||||||
|
clox
|
BIN
clox/clox
BIN
clox/clox
Binary file not shown.
@ -99,6 +99,24 @@ static void emitBytes(uint8_t byte1, uint8_t byte2) {
|
|||||||
emitByte(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() {
|
static void endCompiler() {
|
||||||
emitReturn();
|
emitReturn();
|
||||||
#ifdef DEBUG_PRINT_CODE
|
#ifdef DEBUG_PRINT_CODE
|
||||||
@ -125,23 +143,33 @@ static void binary() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void emitReturn() {
|
static void expression() {
|
||||||
emitByte(OP_RETURN);
|
// What goes here?
|
||||||
|
parsePrecedence(PREC_ASSIGNMENT);
|
||||||
|
}
|
||||||
|
static void grouping() {
|
||||||
|
expression();
|
||||||
|
consume(TOKEN_RIGHT_PAREN, "Expect ')' after expression.");
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t makeConstant(Value value) {
|
static void number() {
|
||||||
int constant = addConstant(currentChunk(), value);
|
double value = strtod(parser.previous.start, NULL);
|
||||||
if (constant > UINT8_MAX) {
|
emitConstant(value);
|
||||||
error("Too many constants in one chunk.");
|
}
|
||||||
return 0;
|
|
||||||
|
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) {
|
static void parsePrecedence(Precedence precedence) {
|
||||||
advance();
|
advance();
|
||||||
@ -207,32 +235,6 @@ static ParseRule* getRule(TokenType type) {
|
|||||||
return &rules[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) {
|
bool compile(const char* source, Chunk* chunk) {
|
||||||
initScanner(source);
|
initScanner(source);
|
||||||
compilingChunk = chunk;
|
compilingChunk = chunk;
|
||||||
|
BIN
clox/compiler.o
BIN
clox/compiler.o
Binary file not shown.
Loading…
Reference in New Issue
Block a user