Skip to content

Commit

Permalink
Print statement
Browse files Browse the repository at this point in the history
  • Loading branch information
dils2k committed Mar 26, 2023
1 parent 738127e commit 5ebedf6
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,5 @@ build
clox

.vscode

test.lox
2 changes: 2 additions & 0 deletions chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ typedef enum {
OP_NIL,
OP_TRUE,
OP_FALSE,
OP_POP,
OP_EQUAL,
OP_GREATER,
OP_LESS,
OP_NEGATE,
OP_PRINT,
OP_RETURN
} OpCode;

Expand Down
68 changes: 66 additions & 2 deletions compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ static void consume(TokenType type, const char* message) {
errorAtCurrent(message);
}

static bool check(TokenType type) {
return parser.current.type == type;
}

static bool match(TokenType type) {
if (!check(type)) return false;
advance();
return true;
}

static void emitByte(uint8_t byte) {
writeChunk(currentChunk(), byte, parser.previous.line);
}
Expand All @@ -112,6 +122,8 @@ static void endCompiler() {
}

static void expression();
static void statement();
static void declaration();
static ParseRule* getRule(TokenType type);
static void parsePrecedence(Precedence precedence);

Expand Down Expand Up @@ -160,6 +172,55 @@ static void expression() {
parsePrecedence(PREC_ASSIGNMENT);
}

static void expressionStatement() {
expression();
consume(TOKEN_SEMICOLON, "Expect ';' after expression.");
emitByte(OP_POP);
}

static void printStatement() {
expression();
consume(TOKEN_SEMICOLON, "Expect ';' after value.");
emitByte(OP_PRINT);
}

static void synchronize() {
parser.panicMode = false;

while (parser.current.type != TOKEN_EOF) {
if (parser.previous.type == TOKEN_SEMICOLON) return;
switch (parser.current.type) {
case TOKEN_CLASS:
case TOKEN_FUN:
case TOKEN_VAR:
case TOKEN_FOR:
case TOKEN_IF:
case TOKEN_WHILE:
case TOKEN_PRINT:
case TOKEN_RETURN:
return;
default:
; // Do nothing.
}

advance();
}
}

static void declaration() {
statement();

if (parser.panicMode) synchronize();
}

static void statement() {
if (match(TOKEN_PRINT)) {
printStatement();
} else {
expressionStatement();
}
}

static void grouping() {
expression();
consume(TOKEN_RIGHT_PAREN, "Expect ')' after expression.");
Expand Down Expand Up @@ -261,8 +322,11 @@ bool compile(const char* source, Chunk* chunk) {
parser.hadError = false;

advance();
expression();
consume(TOKEN_EOF, "Expect end of expression");

while (!match(TOKEN_EOF)) {
declaration();
}

endCompiler();
return !parser.hadError;
}
4 changes: 4 additions & 0 deletions debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ int disassembleInstruction(Chunk* chunk, int offset) {

uint8_t instruction = chunk->code[offset];
switch (instruction) {
case OP_PRINT:
return simpleInstruction("OP_PRINT", offset);
case OP_RETURN:
return simpleInstruction("OP_RETURN", offset);
case OP_CONSTANT:
Expand Down Expand Up @@ -57,6 +59,8 @@ int disassembleInstruction(Chunk* chunk, int offset) {
return simpleInstruction("OP_MULTIPLY", offset);
case OP_DIVIDE:
return simpleInstruction("OP_DEVIDE", offset);
case OP_POP:
return simpleInstruction("OP_POP", offset);
default:
printf("Unknown opcode %d\n", instruction);
return offset + 1;
Expand Down
7 changes: 6 additions & 1 deletion vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ static InterpretResult run() {
case OP_NIL: push(NIL_VAL); break;
case OP_TRUE: push(BOOL_VAL(true)); break;
case OP_FALSE: push(BOOL_VAL(false)); break;
case OP_POP: pop(); break;
case OP_GREATER: BINARY_OP(BOOL_VAL, >); break;
case OP_LESS: BINARY_OP(BOOL_VAL, <); break;
case OP_ADD: {
Expand Down Expand Up @@ -143,9 +144,13 @@ static InterpretResult run() {
push(BOOL_VAL(valuesEqual(a, b)));
break;
}
case OP_RETURN: {
case OP_PRINT: {
printValue(pop());
printf("\n");
break;
}
case OP_RETURN: {
// Exit interpreter.
return INTERPRET_OK;
}
}
Expand Down

0 comments on commit 5ebedf6

Please sign in to comment.