Files
Toy/source/toy_string.h
Kayne Ruse 4bcf8e84a9 String literals are being parsed, compiled and printed, read more
Strings, due to their potentially large size, are stored outside of a
routine's code section, in the data section. To access the correct
string, you must read the jump index, then the real address from the
jump table - and extra layer of indirection will result in more flexible
data down the road, I hope.

Other changes include:

* Added string concat operator ..
* Added TOY_STRING_MAX_LENGTH
* Strings can't be created or concatenated longer than the max length
* The parser will display a warning if the bucket is too small for a
  string at max length, but it will continue
* Added TOY_BUCKET_IDEAL to correspend with max string length
* The bucket now allocates an address that is 4-byte aligned
* Fixed missing entries in the parser rule table
* Corrected some failing TOY_BITNESS tests
2024-10-08 00:33:36 +11:00

59 lines
1.9 KiB
C

#pragma once
#include "toy_common.h"
#include "toy_bucket.h"
#include "toy_value.h"
#define TOY_STRING_MAX_LENGTH 1000
//rope pattern
typedef struct Toy_String { //32 | 64 BITNESS
enum Toy_StringType {
TOY_STRING_NODE,
TOY_STRING_LEAF,
TOY_STRING_NAME,
} type; //4 | 4
unsigned int length; //4 | 4
unsigned int refCount; //4 | 4
unsigned int cachedHash; //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
struct {
Toy_ValueType type; //4 | 4
char data[]; //- | -
} name; //4 | 4
} as; //8 | 16
} Toy_String; //24 | 32
TOY_API Toy_String* Toy_createString(Toy_Bucket** bucketHandle, const char* cstring);
TOY_API Toy_String* Toy_createStringLength(Toy_Bucket** bucketHandle, const char* cstring, int length);
TOY_API Toy_String* Toy_createNameString(Toy_Bucket** bucketHandle, const char* cname); //for variable names
TOY_API Toy_String* Toy_copyString(Toy_Bucket** bucketHandle, Toy_String* str);
TOY_API Toy_String* Toy_deepCopyString(Toy_Bucket** bucketHandle, Toy_String* str);
TOY_API Toy_String* Toy_concatStrings(Toy_Bucket** bucketHandle, 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
TOY_API int Toy_compareStrings(Toy_String* left, Toy_String* right); //return value mimics strcmp()