diff --git a/clox/chunk.h b/clox/chunk.h index 2b3798f..34d73c4 100644 --- a/clox/chunk.h +++ b/clox/chunk.h @@ -6,12 +6,19 @@ typedef enum { OP_CONSTANT, + OP_NIL, + OP_TRUE, + OP_FALSE, + OP_EQUAL, + OP_GREATER, + OP_LESS, OP_RETURN, OP_NEGATE, OP_ADD, OP_SUBTRACT, OP_MULTIPLY, OP_DIVIDE, + OP_NOT, } OpCode; typedef struct { diff --git a/clox/clox b/clox/clox index e1e7feb..109e9f2 100755 Binary files a/clox/clox and b/clox/clox differ diff --git a/clox/compiler.c b/clox/compiler.c index 68bd0d0..aafb558 100644 --- a/clox/compiler.c +++ b/clox/compiler.c @@ -143,6 +143,15 @@ static void binary() { } } +static void literal() { + switch (parser.previous.type) { + case TOKEN_FALSE: emitByte(OP_FALSE); break; + case TOKEN_NIL: emitByte(OP_NIL); break; + case TOKEN_TRUE: emitByte(OP_TRUE); break; + default: return; // Unreachable. + } +} + static void expression() { // What goes here? parsePrecedence(PREC_ASSIGNMENT); @@ -154,7 +163,7 @@ static void grouping() { static void number() { double value = strtod(parser.previous.start, NULL); - emitConstant(value); + emitConstant(NUMBER_VAL(value)); } static void unary() { @@ -164,6 +173,7 @@ static void unary() { parsePrecedence(PREC_UNARY); // Emit the operator instruction. switch (operatorType) { + case TOKEN_BANG: emitByte(OP_NOT); break; case TOKEN_MINUS: emitByte(OP_NEGATE); break; default: return; // Unreachable. } @@ -200,7 +210,7 @@ ParseRule rules[] = { [TOKEN_SEMICOLON] = {NULL, NULL, PREC_NONE}, [TOKEN_SLASH] = {NULL, binary, PREC_FACTOR}, [TOKEN_STAR] = {NULL, binary, PREC_FACTOR}, - [TOKEN_BANG] = {NULL, NULL, PREC_NONE}, + [TOKEN_BANG] = {unary, NULL, PREC_NONE}, [TOKEN_BANG_EQUAL] = {NULL, NULL, PREC_NONE}, [TOKEN_EQUAL] = {NULL, NULL, PREC_NONE}, [TOKEN_EQUAL_EQUAL] = {NULL, NULL, PREC_NONE}, @@ -214,17 +224,17 @@ ParseRule rules[] = { [TOKEN_AND] = {NULL, NULL, PREC_NONE}, [TOKEN_CLASS] = {NULL, NULL, PREC_NONE}, [TOKEN_ELSE] = {NULL, NULL, PREC_NONE}, - [TOKEN_FALSE] = {NULL, NULL, PREC_NONE}, + [TOKEN_FALSE] = {literal, NULL, PREC_NONE}, [TOKEN_FOR] = {NULL, NULL, PREC_NONE}, [TOKEN_FUN] = {NULL, NULL, PREC_NONE}, [TOKEN_IF] = {NULL, NULL, PREC_NONE}, - [TOKEN_NIL] = {NULL, NULL, PREC_NONE}, + [TOKEN_NIL] = {literal, NULL, PREC_NONE}, [TOKEN_OR] = {NULL, NULL, PREC_NONE}, [TOKEN_PRINT] = {NULL, NULL, PREC_NONE}, [TOKEN_RETURN] = {NULL, NULL, PREC_NONE}, [TOKEN_SUPER] = {NULL, NULL, PREC_NONE}, [TOKEN_THIS] = {NULL, NULL, PREC_NONE}, - [TOKEN_TRUE] = {NULL, NULL, PREC_NONE}, + [TOKEN_TRUE] = {literal, NULL, PREC_NONE}, [TOKEN_VAR] = {NULL, NULL, PREC_NONE}, [TOKEN_WHILE] = {NULL, NULL, PREC_NONE}, [TOKEN_ERROR] = {NULL, NULL, PREC_NONE}, diff --git a/clox/compiler.o b/clox/compiler.o index 7e02c78..7f30988 100644 Binary files a/clox/compiler.o and b/clox/compiler.o differ diff --git a/clox/debug.c b/clox/debug.c index 8f7af21..4175c21 100644 --- a/clox/debug.c +++ b/clox/debug.c @@ -36,6 +36,18 @@ int disassembleInstruction(Chunk* chunk, int offset) { switch (instruction) { case OP_CONSTANT: return constantInstruction("OP_CONSTANT", chunk, offset); + case OP_NIL: + return simpleInstruction("OP_NIL", offset); + case OP_TRUE: + return simpleInstruction("OP_TRUE", offset); + case OP_FALSE: + return simpleInstruction("OP_FALSE", offset); + case OP_EQUAL: + return simpleInstruction("OP_EQUAL", offset); + case OP_GREATER: + return simpleInstruction("OP_GREATER", offset); + case OP_LESS: + return simpleInstruction("OP_LESS", offset); case OP_ADD: return simpleInstruction("OP_ADD", offset); case OP_SUBTRACT: @@ -46,6 +58,8 @@ int disassembleInstruction(Chunk* chunk, int offset) { return simpleInstruction("OP_DIVIDE", offset); case OP_NEGATE: return simpleInstruction("OP_NEGATE", offset); + case OP_NOT: + return simpleInstruction("OP_NOT", offset); case OP_RETURN: return simpleInstruction("OP_RETURN", offset); default: diff --git a/clox/debug.o b/clox/debug.o index 84609a4..1431a55 100644 Binary files a/clox/debug.o and b/clox/debug.o differ diff --git a/clox/value.c b/clox/value.c index 5657f68..19b7c0b 100644 --- a/clox/value.c +++ b/clox/value.c @@ -24,5 +24,22 @@ void freeValueArray(ValueArray* array) { initValueArray(array); } void printValue(Value value) { - printf("%g", value); + switch (value.type) { + case VAL_BOOL: + printf(AS_BOOL(value) ? "true" : "false"); + break; + case VAL_NIL: printf("nil"); break; + case VAL_NUMBER: printf("%g", AS_NUMBER(value)); break; + } } + +bool valuesEqual(Value a, Value b) { + if (a.type != b.type) return false; + switch (a.type) { + case VAL_BOOL: return AS_BOOL(a) == AS_BOOL(b); + case VAL_NIL: return true; + case VAL_NUMBER: return AS_NUMBER(a) == AS_NUMBER(b); + default: return false; // Unreachable. + } +} + diff --git a/clox/value.h b/clox/value.h index 86d4b1c..ee41d23 100644 --- a/clox/value.h +++ b/clox/value.h @@ -3,7 +3,33 @@ #include "common.h" -typedef double Value; + +//typedef double Value; + +typedef enum { + VAL_BOOL, + VAL_NIL, + VAL_NUMBER, +} ValueType; + +typedef struct { + ValueType type; + union { + bool boolean; + double number; + } as; +} Value; + +#define IS_BOOL(value) ((value).type == VAL_BOOL) +#define IS_NIL(value) ((value).type == VAL_NIL) +#define IS_NUMBER(value) ((value).type == VAL_NUMBER) + +#define AS_BOOL(value) ((value).as.boolean) +#define AS_NUMBER(value) ((value).as.number) + +#define BOOL_VAL(value) ((Value){VAL_BOOL, {.boolean = value}}) +#define NIL_VAL ((Value){VAL_NIL, {.number = 0}}) +#define NUMBER_VAL(value) ((Value){VAL_NUMBER, {.number = value}}) typedef struct { int capacity; diff --git a/clox/value.o b/clox/value.o index 96ee556..2a9e016 100644 Binary files a/clox/value.o and b/clox/value.o differ diff --git a/clox/vm.c b/clox/vm.c index 9c6c687..4d4d599 100644 --- a/clox/vm.c +++ b/clox/vm.c @@ -1,3 +1,4 @@ +#include #include #include "common.h" #include "compiler.h" @@ -7,6 +8,20 @@ VM vm; static void resetStack() { vm.stackTop = vm.stack; } + +static void runtimeError(const char* format, ...) { + va_list args; + va_start(args, format); + vfprintf(stderr, format, args); + va_end(args); + fputs("\n", stderr); + + size_t instruction = vm.ip - vm.chunk->code - 1; + int line = vm.chunk->lines[instruction]; + fprintf(stderr, "[line %d] in script\n", line); + resetStack(); +} + void initVM() { resetStack(); } @@ -23,14 +38,26 @@ Value pop() { return *vm.stackTop; } +static Value peek(int distance) { + return vm.stackTop[-1 - distance]; +} + +static bool isFalsey(Value value) { + return IS_NIL(value) || (IS_BOOL(value) && !AS_BOOL(value)); +} + static InterpretResult run() { #define READ_BYTE() (*vm.ip++) #define READ_CONSTANT() (vm.chunk->constants.values[READ_BYTE()]) -#define BINARY_OP(op) \ +#define BINARY_OP(valueType, op) \ do { \ - double b = pop(); \ - double a = pop(); \ - push(a op b); \ + if (!IS_NUMBER(peek(0)) || !IS_NUMBER(peek(1))) { \ + runtimeError("Operands must be numbers."); \ + return INTERPRET_RUNTIME_ERROR; \ + } \ + double b = AS_NUMBER(pop()); \ + double a = AS_NUMBER(pop()); \ + push(valueType(a op b)); \ } while (false) for (;;) { #ifdef DEBUG_TRACE_EXECUTION @@ -53,11 +80,25 @@ static InterpretResult run() { push(constant); break; } - case OP_NEGATE: push(-pop()); break; - case OP_ADD: BINARY_OP(+); break; - case OP_SUBTRACT: BINARY_OP(-); break; - case OP_MULTIPLY: BINARY_OP(*); break; - case OP_DIVIDE: BINARY_OP(/); break; + 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_GREATER: BINARY_OP(BOOL_VAL, >); break; + case OP_LESS: BINARY_OP(BOOL_VAL, <); break; + case OP_ADD: BINARY_OP(NUMBER_VAL, +); break; + case OP_SUBTRACT: BINARY_OP(NUMBER_VAL, -); break; + case OP_MULTIPLY: BINARY_OP(NUMBER_VAL, *); break; + case OP_DIVIDE: BINARY_OP(NUMBER_VAL, /); break; + case OP_NOT: + push(BOOL_VAL(isFalsey(pop()))); + break; + case OP_NEGATE: + if (!IS_NUMBER(peek(0))) { + runtimeError("Operand must be a number."); + return INTERPRET_RUNTIME_ERROR; + } + push(NUMBER_VAL(-AS_NUMBER(pop()))); + break; case OP_RETURN: { printValue(pop()); printf("\n"); diff --git a/clox/vm.o b/clox/vm.o index 10f26d5..287fae9 100644 Binary files a/clox/vm.o and b/clox/vm.o differ