diff --git a/clox/src/CMakeLists.txt b/clox/src/CMakeLists.txt index e46b989..0deeb6a 100644 --- a/clox/src/CMakeLists.txt +++ b/clox/src/CMakeLists.txt @@ -15,6 +15,7 @@ add_executable(Lox scanner.h scanner.c object.h + object.c main.c) install(TARGETS Lox) diff --git a/clox/src/compiler.c b/clox/src/compiler.c index ab8fe7d..8606557 100644 --- a/clox/src/compiler.c +++ b/clox/src/compiler.c @@ -190,6 +190,11 @@ static void number() { emitConstant(NUMBER_VAL(value)); } +static void string() { + emitConstant(OBJ_VAL( + copyString(parser.previous.start + 1, parser.previous.length - 2))); +} + static void unary() { TokenType operatorType = parser.previous.type; @@ -235,7 +240,7 @@ ParseRule rules[] = { [TOKEN_LESS] = {NULL, binary, PREC_COMPARISON}, [TOKEN_LESS_EQUAL] = {NULL, binary, PREC_COMPARISON}, [TOKEN_IDENTIFIER] = {NULL, NULL, PREC_NONE}, - [TOKEN_STRING] = {NULL, NULL, PREC_NONE}, + [TOKEN_STRING] = {string, NULL, PREC_NONE}, [TOKEN_NUMBER] = {number, NULL, PREC_NONE}, [TOKEN_AND] = {NULL, NULL, PREC_NONE}, [TOKEN_CLASS] = {NULL, NULL, PREC_NONE}, diff --git a/clox/src/compiler.h b/clox/src/compiler.h index c4376a5..e7023a9 100644 --- a/clox/src/compiler.h +++ b/clox/src/compiler.h @@ -1,6 +1,7 @@ #ifndef COMPILER_H #define COMPILER_H +#include "object.h" #include "vm.h" bool compile(const char *source, Chunk *chunk); diff --git a/clox/src/memory.h b/clox/src/memory.h index 3c4bdb7..a810bd7 100644 --- a/clox/src/memory.h +++ b/clox/src/memory.h @@ -3,16 +3,18 @@ #include "common.h" -#define GROW_CAPACITY(capacity) \ - ((capacity) < 8 ? 8 : (capacity) * 2) +#define ALLOCATE(type, count) \ + (type *)reallocate(NULL, 0, sizeof(type) * (count)) -#define GROW_ARRAY(type, pointer, oldCount, newCount) \ - (type*)reallocate(pointer, sizeof(type) * (oldCount), \ - sizeof(type) * (newCount)) +#define GROW_CAPACITY(capacity) ((capacity) < 8 ? 8 : (capacity)*2) -#define FREE_ARRAY(type, pointer, oldCount) \ +#define GROW_ARRAY(type, pointer, oldCount, newCount) \ + (type *)reallocate(pointer, sizeof(type) * (oldCount), \ + sizeof(type) * (newCount)) + +#define FREE_ARRAY(type, pointer, oldCount) \ reallocate(pointer, sizeof(type) * (oldCount), 0) -void* reallocate(void* pointer, size_t oldSize, size_t newSize); +void *reallocate(void *pointer, size_t oldSize, size_t newSize); #endif diff --git a/clox/src/object.h b/clox/src/object.h index 7ab8f3a..2a87a51 100644 --- a/clox/src/object.h +++ b/clox/src/object.h @@ -25,6 +25,8 @@ struct ObjString { char *chars; }; +ObjString *copyString(const char *chars, int length); + static inline bool isObjType(Value value, ObjType type) { return IS_OBJ(value) && AS_OBJ(value)->type == type; }