Skip to content

Commit

Permalink
Compiler done
Browse files Browse the repository at this point in the history
  • Loading branch information
dils2k committed Feb 26, 2023
1 parent 8ba86bb commit 9a26fe1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
1 change: 1 addition & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stddef.h>
#include <stdint.h>

#define DEBUG_PRINT_CODE
#define DEBUG_TRACE_EXECUTION

#endif
45 changes: 26 additions & 19 deletions compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include "compiler.h"
#include "scanner.h"

#ifdef DEBUG_PRINT_CODE
#include "debug.h"
#endif

typedef struct {
Token current;
Token previous;
Expand Down Expand Up @@ -100,6 +104,11 @@ static void emitReturn() {

static void endCompiler() {
emitReturn();
#ifdef DEBUG_PRINT_CODE
if (!parser.hadError) {
disassembleChunk(currentChunk(), "code");
}
#endif
}

static void expression();
Expand Down Expand Up @@ -133,13 +142,13 @@ static void parsePrecedence(Precedence precedence) {
return;
}

prefixRule();

while (precedence <= getRule(parser.current.type)->precedence) {
advance();
ParseFn infixRule = getRule(parser.previous.type)->infix;
infixRule();
}

prefixRule();
}

static void expression() {
Expand All @@ -152,18 +161,30 @@ static void grouping() {
}

static void unary() {
parsePrecedence(PREC_UNARY);

TokenType operatorType = parser.previous.type;

expression();
parsePrecedence(PREC_UNARY);

switch (operatorType) {
case TOKEN_MINUS: emitByte(OP_NEGATE); break;
default: return;
}
}

static void binary() {
TokenType operatorType = parser.previous.type;
ParseRule* rule = getRule(operatorType);
parsePrecedence((Precedence)(rule->precedence+1));

switch (operatorType) {
case TOKEN_PLUS: emitByte(OP_ADD); break;
case TOKEN_MINUS: emitByte(OP_SUBTRACT); break;
case TOKEN_STAR: emitByte(OP_MULTIPLY); break;
case TOKEN_SLASH: emitByte(OP_DIVIDE); break;
default: return; // Unreachable.
}
}

ParseRule rules[] = {
[TOKEN_LEFT_PAREN] = {grouping, NULL, PREC_NONE},
[TOKEN_RIGHT_PAREN] = {NULL, NULL, PREC_NONE},
Expand Down Expand Up @@ -211,20 +232,6 @@ static ParseRule* getRule(TokenType type) {
return &rules[type];
}

static void binary() {
TokenType operatorType = parser.previous.type;
ParseRule* rule = getRule(operatorType);
parsePrecedence((Precedence)(rule->precedence+1));

switch (operatorType) {
case TOKEN_PLUS: emitByte(OP_ADD); break;
case TOKEN_MINUS: emitByte(OP_SUBTRACT); break;
case TOKEN_STAR: emitByte(OP_MULTIPLY); break;
case TOKEN_SLASH: emitByte(OP_DIVIDE); break;
default: return; // Unreachable.
}
}

bool compile(const char* source, Chunk* chunk) {
initScanner(source);
compilingChunk = chunk;
Expand Down
2 changes: 1 addition & 1 deletion vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ InterpretResult interpret(const char* source) {
InterpretResult result = run();

freeChunk(&chunk);
return INTERPRET_OK;
return result;
}

0 comments on commit 9a26fe1

Please sign in to comment.