mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
'print' now works as expected
String literals are now stored in the bucket mid-compilation, but this is fine, as the buckets are freed one the bytecode is complete.
This commit is contained in:
@@ -388,7 +388,7 @@ static Toy_AstFlag literal(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_As
|
|||||||
|
|
||||||
buffer[i] = '\0';
|
buffer[i] = '\0';
|
||||||
unsigned int len = i - escapeCounter; //NOTE: len is ONLY the string length
|
unsigned int len = i - escapeCounter; //NOTE: len is ONLY the string length
|
||||||
Toy_private_emitAstValue(bucketHandle, rootHandle, TOY_VALUE_FROM_STRING(Toy_toStringLength(bucketHandle, buffer, len)));
|
Toy_private_emitAstValue(bucketHandle, rootHandle, TOY_VALUE_FROM_STRING(Toy_createStringLength(bucketHandle, buffer, len))); //BUGFIX: create the string to avoid losing local var 'buffer'
|
||||||
|
|
||||||
return TOY_AST_FLAG_NONE;
|
return TOY_AST_FLAG_NONE;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,6 +42,21 @@ Toy_String* Toy_toStringLength(Toy_Bucket** bucketHandle, const char* cstring, u
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Toy_String* Toy_createStringLength(Toy_Bucket** bucketHandle, const char* cstring, unsigned int length) {
|
||||||
|
Toy_String* ret = (Toy_String*)Toy_partitionBucket(bucketHandle, sizeof(Toy_String));
|
||||||
|
char* data = (char*)Toy_partitionBucket(bucketHandle, length + 1);
|
||||||
|
|
||||||
|
strncpy(data, cstring, length);
|
||||||
|
|
||||||
|
ret->info.type = TOY_STRING_LEAF;
|
||||||
|
ret->info.length = length;
|
||||||
|
ret->info.refCount = 1;
|
||||||
|
ret->info.cachedHash = 0; //don't calc until needed
|
||||||
|
ret->leaf.data = data; //DO make a copy, stored in the bucket
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
Toy_String* Toy_copyString(Toy_String* str) {
|
Toy_String* Toy_copyString(Toy_String* str) {
|
||||||
assert(str->info.refCount != 0 && "Can't copy a string with refcount of zero");
|
assert(str->info.refCount != 0 && "Can't copy a string with refcount of zero");
|
||||||
incrementRefCount(str);
|
incrementRefCount(str);
|
||||||
|
|||||||
@@ -41,6 +41,8 @@ typedef union Toy_String_t {
|
|||||||
TOY_API Toy_String* Toy_toString(Toy_Bucket** bucketHandle, const char* cstring);
|
TOY_API Toy_String* Toy_toString(Toy_Bucket** bucketHandle, const char* cstring);
|
||||||
TOY_API Toy_String* Toy_toStringLength(Toy_Bucket** bucketHandle, const char* cstring, unsigned int length);
|
TOY_API Toy_String* Toy_toStringLength(Toy_Bucket** bucketHandle, const char* cstring, unsigned int length);
|
||||||
|
|
||||||
|
TOY_API Toy_String* Toy_createStringLength(Toy_Bucket** bucketHandle, const char* cstring, unsigned int length);
|
||||||
|
|
||||||
TOY_API Toy_String* Toy_copyString(Toy_String* str);
|
TOY_API Toy_String* Toy_copyString(Toy_String* str);
|
||||||
TOY_API Toy_String* Toy_concatStrings(Toy_Bucket** bucketHandle, Toy_String* left, Toy_String* right);
|
TOY_API Toy_String* Toy_concatStrings(Toy_Bucket** bucketHandle, Toy_String* left, Toy_String* right);
|
||||||
|
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ static void processRead(Toy_VM* vm) {
|
|||||||
|
|
||||||
case TOY_VALUE_STRING: {
|
case TOY_VALUE_STRING: {
|
||||||
enum Toy_StringType stringType = READ_BYTE(vm);
|
enum Toy_StringType stringType = READ_BYTE(vm);
|
||||||
int len = (int)READ_BYTE(vm); //only needed for name strings
|
//int len = (int)READ_BYTE(vm); //WARN: only used for name strings
|
||||||
|
|
||||||
//grab the jump as an integer
|
//grab the jump as an integer
|
||||||
unsigned int jump = *((int*)(vm->code + vm->jumpsAddr + READ_INT(vm)));
|
unsigned int jump = *((int*)(vm->code + vm->jumpsAddr + READ_INT(vm)));
|
||||||
@@ -73,7 +73,7 @@ static void processRead(Toy_VM* vm) {
|
|||||||
|
|
||||||
//build a string from the data section
|
//build a string from the data section
|
||||||
if (stringType == TOY_STRING_LEAF) {
|
if (stringType == TOY_STRING_LEAF) {
|
||||||
value = TOY_VALUE_FROM_STRING(Toy_toStringLength(&vm->memoryBucket, cstring, len));
|
value = TOY_VALUE_FROM_STRING(Toy_toString(&vm->memoryBucket, cstring));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Toy_error("Invalid string type found in opcode read");
|
Toy_error("Invalid string type found in opcode read");
|
||||||
|
|||||||
Reference in New Issue
Block a user