mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Implemented and tested Toy_String, read more
Strings are needed for the handling of identifiers in the key/value variable storage, so I've got them working first. I used the rope pattern, which seems to be quite an interesting approach. I'll add comparison checks later. Adjusted how buckets are handled in all tests, could've been an issue down the line. Added the build instructions to README.md.
This commit is contained in:
@@ -28,7 +28,7 @@ void Toy_initBucket(Toy_Bucket** bucketHandle, size_t capacity) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
(*bucketHandle) = malloc(sizeof(Toy_Bucket));
|
||||
(*bucketHandle) = malloc(sizeof(Toy_Bucket)); //TODO: rework the bucket, so there's only one malloc() call instead of two when partitioning
|
||||
|
||||
if ((*bucketHandle) == NULL) {
|
||||
fprintf(stderr, TOY_CC_ERROR "[internal] ERROR: Failed to allocate space for a bucket\n" TOY_CC_RESET);
|
||||
@@ -48,6 +48,12 @@ void* Toy_partBucket(Toy_Bucket** bucketHandle, size_t space) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
//if you try to allocate too much space
|
||||
if ((*bucketHandle)->capacity < space) {
|
||||
fprintf(stderr, TOY_CC_ERROR "[internal] ERROR: Failed to partition bucket memory, not enough capacity\n" TOY_CC_RESET);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
//if out of space in the current bucket
|
||||
if ((*bucketHandle)->capacity < (*bucketHandle)->count + space) {
|
||||
//move to the next bucket
|
||||
|
||||
126
source/toy_string.c
Normal file
126
source/toy_string.c
Normal file
@@ -0,0 +1,126 @@
|
||||
#include "toy_string.h"
|
||||
#include "toy_console_colors.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
//utils
|
||||
static void deepCopyUtil(char* dest, Toy_String* str) {
|
||||
//sometimes, "clever" can be a bad thing...
|
||||
if (str->type == TOY_STRING_NODE) {
|
||||
deepCopyUtil(dest, str->as.node.left);
|
||||
deepCopyUtil(dest + str->as.node.left->length, str->as.node.right);
|
||||
}
|
||||
|
||||
else {
|
||||
memcpy(dest, str->as.leaf.data, str->length);
|
||||
}
|
||||
}
|
||||
|
||||
static void incrementRefCount(Toy_String* str) {
|
||||
str->refCount++;
|
||||
if (str->type == TOY_STRING_NODE) {
|
||||
incrementRefCount(str->as.node.left);
|
||||
incrementRefCount(str->as.node.right);
|
||||
}
|
||||
}
|
||||
|
||||
static void decrementRefCount(Toy_String* str) {
|
||||
str->refCount--;
|
||||
if (str->type == TOY_STRING_NODE) {
|
||||
decrementRefCount(str->as.node.left);
|
||||
decrementRefCount(str->as.node.right);
|
||||
}
|
||||
}
|
||||
|
||||
//exposed functions
|
||||
Toy_String* Toy_createString(Toy_Bucket** bucket, const char* cstring) {
|
||||
int length = strlen(cstring);
|
||||
|
||||
return Toy_createStringLength(bucket, cstring, length);
|
||||
}
|
||||
|
||||
Toy_String* Toy_createStringLength(Toy_Bucket** bucket, const char* cstring, int length) {
|
||||
Toy_String* ret = (Toy_String*)Toy_partBucket(bucket, sizeof(Toy_String) + length + 1); //TODO: compensate for partitioning more space than bucket capacity
|
||||
|
||||
ret->type = TOY_STRING_LEAF;
|
||||
ret->length = length;
|
||||
ret->refCount = 1;
|
||||
memcpy(ret->as.leaf.data, cstring, length);
|
||||
ret->as.leaf.data[length] = '\0';
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Toy_String* Toy_copyString(Toy_Bucket** bucket, Toy_String* str) {
|
||||
if (str->refCount <= 0) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't copy a string with refcount below or equal to zero\n" TOY_CC_RESET);
|
||||
exit(-1);
|
||||
}
|
||||
incrementRefCount(str);
|
||||
return str;
|
||||
}
|
||||
|
||||
Toy_String* Toy_deepCopyString(Toy_Bucket** bucket, Toy_String* str) {
|
||||
if (str->refCount <= 0) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't deep copy a string with refcount below or equal to zero\n" TOY_CC_RESET);
|
||||
exit(-1);
|
||||
}
|
||||
Toy_String* ret = (Toy_String*)Toy_partBucket(bucket, sizeof(Toy_String) + str->length + 1); //TODO: compensate for partitioning more space than bucket capacity
|
||||
|
||||
//TODO
|
||||
ret->type = TOY_STRING_LEAF;
|
||||
ret->length = str->length;
|
||||
ret->refCount = 1;
|
||||
deepCopyUtil(ret->as.leaf.data, str); //copy each leaf into the buffer
|
||||
ret->as.leaf.data[ret->length] = '\0';
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Toy_String* Toy_concatString(Toy_Bucket** bucket, Toy_String* left, Toy_String* right) {
|
||||
if (left->refCount <= 0 || right->refCount <= 0) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't concatenate a string with refcount below or equal to zero\n" TOY_CC_RESET);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
Toy_String* ret = (Toy_String*)Toy_partBucket(bucket, sizeof(Toy_String));
|
||||
|
||||
ret->type = TOY_STRING_NODE;
|
||||
ret->length = left->length + right->length;
|
||||
ret->refCount = 1;
|
||||
ret->as.node.left = left;
|
||||
ret->as.node.right = right;
|
||||
|
||||
incrementRefCount(left);
|
||||
incrementRefCount(right);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Toy_freeString(Toy_String* str) {
|
||||
decrementRefCount(str); //TODO: tool for checking the bucket is empty, and freeing it
|
||||
}
|
||||
|
||||
int Toy_getStringLength(Toy_String* str) {
|
||||
return str->length;
|
||||
}
|
||||
|
||||
int Toy_getStringRefCount(Toy_String* str) {
|
||||
return str->refCount;
|
||||
}
|
||||
|
||||
char* Toy_getStringRawBuffer(Toy_String* str) {
|
||||
if (str->refCount <= 0) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't get raw string buffer of a string with refcount below or equal to zero\n" TOY_CC_RESET);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
char* buffer = TOY_ALLOCATE(char, str->length + 1);
|
||||
|
||||
deepCopyUtil(buffer, str);
|
||||
buffer[str->length] = '\0';
|
||||
|
||||
return buffer;
|
||||
}
|
||||
43
source/toy_string.h
Normal file
43
source/toy_string.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#include "toy_common.h"
|
||||
|
||||
#include "toy_memory.h"
|
||||
|
||||
//rope pattern
|
||||
typedef struct Toy_String { //32 | 64 BITNESS
|
||||
enum Toy_StringType {
|
||||
TOY_STRING_NODE,
|
||||
TOY_STRING_LEAF,
|
||||
} type; //4 | 4
|
||||
|
||||
int length; //4 | 4
|
||||
int refCount; //4 | 4
|
||||
|
||||
union {
|
||||
struct {
|
||||
struct Toy_String* left; //4 | 8
|
||||
struct Toy_String* right; //4 | 8
|
||||
} node; //8 | 16
|
||||
|
||||
struct {
|
||||
int dummy; //4 | 4
|
||||
char data[]; //- | -
|
||||
} leaf; //4 | 4
|
||||
} as; //8 | 16
|
||||
} Toy_String; //20 | 28
|
||||
|
||||
TOY_API Toy_String* Toy_createString(Toy_Bucket** bucket, const char* cstring);
|
||||
TOY_API Toy_String* Toy_createStringLength(Toy_Bucket** bucket, const char* cstring, int length);
|
||||
|
||||
TOY_API Toy_String* Toy_copyString(Toy_Bucket** bucket, Toy_String* str);
|
||||
TOY_API Toy_String* Toy_deepCopyString(Toy_Bucket** bucket, Toy_String* str);
|
||||
|
||||
TOY_API Toy_String* Toy_concatString(Toy_Bucket** bucket, Toy_String* left, Toy_String* right);
|
||||
|
||||
TOY_API void Toy_freeString(Toy_String* str);
|
||||
|
||||
TOY_API int Toy_getStringLength(Toy_String* str);
|
||||
TOY_API int Toy_getStringRefCount(Toy_String* str);
|
||||
|
||||
TOY_API char* Toy_getStringRawBuffer(Toy_String* str); //allocates the buffer on the heap, needs to be freed
|
||||
|
||||
//TODO: compare
|
||||
@@ -26,7 +26,8 @@ typedef struct Toy_VM {
|
||||
|
||||
int routineCounter;
|
||||
|
||||
//scope - block-level key/value pairs
|
||||
//heap - block-level key/value pairs
|
||||
//TODO: needs string util for identifiers
|
||||
|
||||
//stack - immediate-level values only
|
||||
Toy_Stack stack;
|
||||
|
||||
Reference in New Issue
Block a user