diff options
author | Tom Willemse | 2021-09-09 22:57:03 -0700 |
---|---|---|
committer | Tom Willemse | 2021-09-09 22:57:03 -0700 |
commit | 8ec913756107cc8e6aee965d0dc080039d4995ad (patch) | |
tree | 818fa7ca8314eaed7745d6fe4a7cc9f0c1b4ca6d /clox/src/object.c | |
parent | d5f352e577acf1f472150df5578ff6d693258ae3 (diff) | |
download | crafting-interpreters-8ec913756107cc8e6aee965d0dc080039d4995ad.tar.gz crafting-interpreters-8ec913756107cc8e6aee965d0dc080039d4995ad.zip |
Chapter 19.5
Diffstat (limited to 'clox/src/object.c')
-rw-r--r-- | clox/src/object.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/clox/src/object.c b/clox/src/object.c new file mode 100644 index 0000000..9622327 --- /dev/null +++ b/clox/src/object.c @@ -0,0 +1,45 @@ +#include <stdio.h> +#include <string.h> + +#include "memory.h" +#include "object.h" +#include "value.h" +#include "vm.h" + +#define ALLOCATE_OBJ(type, objectType) \ + (type *)allocateObject(sizeof(type), objectType) + +static Obj *allocateObject(size_t size, ObjType type) { + Obj *object = (Obj *)reallocate(NULL, 0, size); + object->type = type; + + object->next = vm.objects; + vm.objects = object; + return object; +} + +static ObjString *allocateString(char *chars, int length) { + ObjString *string = ALLOCATE_OBJ(ObjString, OBJ_STRING); + string->length = length; + string->chars = chars; + return string; +} + +ObjString *takeString(char *chars, int length) { + return allocateString(chars, length); +} + +ObjString *copyString(const char *chars, int length) { + char *heapChars = ALLOCATE(char, length + 1); + memcpy(heapChars, chars, length); + heapChars[length] = '\0'; + return allocateString(heapChars, length); +} + +void printObject(Value value) { + switch (OBJ_TYPE(value)) { + case OBJ_STRING: + printf("%s", AS_CSTRING(value)); + break; + } +} |