Chapter 18.1 - 18.3

This commit is contained in:
Tom Willemse 2021-08-25 01:08:14 -07:00
parent 3a02da2161
commit 6508cfd8fe
Signed by: ryuslash
GPG key ID: 7D5C407B435025C1
4 changed files with 67 additions and 22 deletions

View file

@ -153,7 +153,7 @@ static void expression() { parsePrecedence(PREC_ASSIGNMENT); }
static void number() { static void number() {
double value = strtod(parser.previous.start, NULL); double value = strtod(parser.previous.start, NULL);
emitConstant(value); emitConstant(NUMBER_VAL(value));
} }
static void unary() { static void unary() {

View file

@ -3,28 +3,27 @@
#include "memory.h" #include "memory.h"
#include "value.h" #include "value.h"
void initValueArray(ValueArray* array) { void initValueArray(ValueArray *array) {
array->values = NULL; array->values = NULL;
array->capacity = 0; array->capacity = 0;
array->count = 0; array->count = 0;
} }
void writeValueArray(ValueArray* array, Value value) { void writeValueArray(ValueArray *array, Value value) {
if (array->capacity < array->count + 1) { if (array->capacity < array->count + 1) {
int oldCapacity = array->capacity; int oldCapacity = array->capacity;
array->capacity = GROW_CAPACITY(oldCapacity); array->capacity = GROW_CAPACITY(oldCapacity);
array->values = GROW_ARRAY(Value, array->values, oldCapacity, array->capacity); array->values =
GROW_ARRAY(Value, array->values, oldCapacity, array->capacity);
} }
array->values[array->count] = value; array->values[array->count] = value;
array->count++; array->count++;
} }
void freeValueArray(ValueArray* array) { void freeValueArray(ValueArray *array) {
FREE_ARRAY(Value, array->values, array->capacity); FREE_ARRAY(Value, array->values, array->capacity);
initValueArray(array); initValueArray(array);
} }
void printValue(Value value) { void printValue(Value value) { printf("%g", AS_NUMBER(value)); }
printf("%g", value);
}

View file

@ -3,17 +3,40 @@
#include "common.h" #include "common.h"
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 { typedef struct {
int capacity; int capacity;
int count; int count;
Value* values; Value *values;
} ValueArray; } ValueArray;
void initValueArray(ValueArray* array); void initValueArray(ValueArray *array);
void writeValueArray(ValueArray* array, Value value); void writeValueArray(ValueArray *array, Value value);
void freeValueArray(ValueArray* array); void freeValueArray(ValueArray *array);
void printValue(Value value); void printValue(Value value);
#endif #endif

View file

@ -1,3 +1,4 @@
#include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include "common.h" #include "common.h"
@ -9,6 +10,19 @@ VM vm;
static void resetStack() { vm.stackTop = vm.stack; } 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(); } void initVM() { resetStack(); }
void freeVM() {} void freeVM() {}
@ -23,14 +37,19 @@ Value pop() {
return *vm.stackTop; return *vm.stackTop;
} }
static Value peek(int distance) { return vm.stackTop[-1 - distance]; }
static InterpretResult run() { static InterpretResult run() {
#define READ_BYTE() (*vm.ip++) #define READ_BYTE() (*vm.ip++)
#define READ_CONSTANT() (vm.chunk->constants.values[READ_BYTE()]) #define READ_CONSTANT() (vm.chunk->constants.values[READ_BYTE()])
#define BINARY_OP(op) \ #define BINARY_OP(valueType, op) \
do { \ do { \
double b = pop(); \ if (!IS_NUMBER(peek(0)) || !IS_NUMBER(peek(1))) { \
double a = pop(); \ runtimeError("Operands must be numbers."); \
push(a op b); \ } \
double b = AS_NUMBER(pop()); \
double a = AS_NUMBER(pop()); \
push(valueType(a op b)); \
} while (false) } while (false)
for (;;) { for (;;) {
@ -53,19 +72,23 @@ static InterpretResult run() {
break; break;
} }
case OP_ADD: case OP_ADD:
BINARY_OP(+); BINARY_OP(NUMBER_VAL, +);
break; break;
case OP_SUBTRACT: case OP_SUBTRACT:
BINARY_OP(-); BINARY_OP(NUMBER_VAL, -);
break; break;
case OP_MULTIPLY: case OP_MULTIPLY:
BINARY_OP(*); BINARY_OP(NUMBER_VAL, *);
break; break;
case OP_DIVIDE: case OP_DIVIDE:
BINARY_OP(/); BINARY_OP(NUMBER_VAL, /);
break; break;
case OP_NEGATE: case OP_NEGATE:
push(-pop()); if (!IS_NUMBER(peek(0))) {
runtimeError("Operand must be a number.");
return INTERPRET_RUNTIME_ERROR;
}
push(NUMBER_VAL(-AS_NUMBER(pop())));
break; break;
case OP_RETURN: { case OP_RETURN: {
printValue(pop()); printValue(pop());