mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Reworked generic structures, read more
The following structures are now more independant: - Toy_Array - Toy_Stack - Toy_Bucket - Toy_String I reworked a lot of the memory allocation, so now there are more direct calls to malloc() or realloc(), rather than relying on the macros from toy_memory.h. I've also split toy_memory into proper array and bucket files, because it makes more sense this way, rather than having them both jammed into one file. This means the eventual hashtable structure can also stand on its own. Toy_Array is a new wrapper around raw array pointers, and all of the structures have their metadata embedded into their allocated memory now, using variable length array members. A lot of 'capacity' and 'count' variables were changed to 'size_t' types, but this doesn't seem to be a problem anywhere. If the workflow fails, then I'll leave it for tonight - I'm too tired, and I don't want to overdo myself.
This commit is contained in:
@@ -1,25 +1,25 @@
|
||||
#include "toy_ast.h"
|
||||
|
||||
void Toy_private_initAstBlock(Toy_Bucket** bucket, Toy_Ast** handle) {
|
||||
(*handle) = (Toy_Ast*)Toy_partBucket(bucket, sizeof(Toy_Ast));
|
||||
Toy_Ast* tmp = (Toy_Ast*)Toy_partitionBucket(bucket, sizeof(Toy_Ast));
|
||||
|
||||
(*handle)->block.type = TOY_AST_BLOCK;
|
||||
(*handle)->block.child = NULL;
|
||||
(*handle)->block.next = NULL;
|
||||
(*handle)->block.tail = NULL;
|
||||
tmp->type = TOY_AST_BLOCK;
|
||||
tmp->block.child = NULL;
|
||||
tmp->block.next = NULL;
|
||||
tmp->block.tail = NULL;
|
||||
|
||||
(*handle) = tmp;
|
||||
}
|
||||
|
||||
void Toy_private_appendAstBlock(Toy_Bucket** bucket, Toy_Ast** handle, Toy_Ast* child) {
|
||||
//type check
|
||||
|
||||
void Toy_private_appendAstBlock(Toy_Bucket** bucket, Toy_Ast* block, Toy_Ast* child) {
|
||||
//first, check if we're an empty head
|
||||
if ((*handle)->block.child == NULL) {
|
||||
(*handle)->block.child = child;
|
||||
if (block->block.child == NULL) {
|
||||
block->block.child = child;
|
||||
return; //NOTE: first call on an empty head skips any memory allocations
|
||||
}
|
||||
|
||||
//run (or jump) until we hit the current tail
|
||||
Toy_Ast* iter = (*handle)->block.tail ? (*handle)->block.tail : (*handle);
|
||||
Toy_Ast* iter = block->block.tail ? block->block.tail : block;
|
||||
|
||||
while(iter->block.next != NULL) {
|
||||
iter = iter->block.next;
|
||||
@@ -30,21 +30,23 @@ void Toy_private_appendAstBlock(Toy_Bucket** bucket, Toy_Ast** handle, Toy_Ast*
|
||||
|
||||
//store the child in the new link, prep the tail pointer
|
||||
iter->block.next->block.child = child;
|
||||
(*handle)->block.tail = iter->block.next;
|
||||
block->block.tail = iter->block.next;
|
||||
}
|
||||
|
||||
void Toy_private_emitAstValue(Toy_Bucket** bucket, Toy_Ast** handle, Toy_Value value) {
|
||||
(*handle) = (Toy_Ast*)Toy_partBucket(bucket, sizeof(Toy_Ast));
|
||||
Toy_Ast* tmp = (Toy_Ast*)Toy_partitionBucket(bucket, sizeof(Toy_Ast));
|
||||
|
||||
(*handle)->value.type = TOY_AST_VALUE;
|
||||
(*handle)->value.value = value;
|
||||
tmp->type = TOY_AST_VALUE;
|
||||
tmp->value.value = value;
|
||||
|
||||
(*handle) = tmp;
|
||||
}
|
||||
|
||||
//TODO: flag range checks
|
||||
void Toy_private_emitAstUnary(Toy_Bucket** bucket, Toy_Ast** handle, Toy_AstFlag flag) {
|
||||
Toy_Ast* tmp = (Toy_Ast*)Toy_partBucket(bucket, sizeof(Toy_Ast));
|
||||
Toy_Ast* tmp = (Toy_Ast*)Toy_partitionBucket(bucket, sizeof(Toy_Ast));
|
||||
|
||||
tmp->unary.type = TOY_AST_UNARY;
|
||||
tmp->type = TOY_AST_UNARY;
|
||||
tmp->unary.flag = flag;
|
||||
tmp->unary.child = *handle;
|
||||
|
||||
@@ -52,9 +54,9 @@ void Toy_private_emitAstUnary(Toy_Bucket** bucket, Toy_Ast** handle, Toy_AstFlag
|
||||
}
|
||||
|
||||
void Toy_private_emitAstBinary(Toy_Bucket** bucket, Toy_Ast** handle, Toy_AstFlag flag, Toy_Ast* right) {
|
||||
Toy_Ast* tmp = (Toy_Ast*)Toy_partBucket(bucket, sizeof(Toy_Ast));
|
||||
Toy_Ast* tmp = (Toy_Ast*)Toy_partitionBucket(bucket, sizeof(Toy_Ast));
|
||||
|
||||
tmp->binary.type = TOY_AST_BINARY;
|
||||
tmp->type = TOY_AST_BINARY;
|
||||
tmp->binary.flag = flag;
|
||||
tmp->binary.left = *handle; //left-recursive
|
||||
tmp->binary.right = right;
|
||||
@@ -63,28 +65,34 @@ void Toy_private_emitAstBinary(Toy_Bucket** bucket, Toy_Ast** handle, Toy_AstFla
|
||||
}
|
||||
|
||||
void Toy_private_emitAstGroup(Toy_Bucket** bucket, Toy_Ast** handle) {
|
||||
Toy_Ast* tmp = (Toy_Ast*)Toy_partBucket(bucket, sizeof(Toy_Ast));
|
||||
Toy_Ast* tmp = (Toy_Ast*)Toy_partitionBucket(bucket, sizeof(Toy_Ast));
|
||||
|
||||
tmp->group.type = TOY_AST_GROUP;
|
||||
tmp->type = TOY_AST_GROUP;
|
||||
tmp->group.child = (*handle);
|
||||
|
||||
(*handle) = tmp;
|
||||
}
|
||||
|
||||
void Toy_private_emitAstPass(Toy_Bucket** bucket, Toy_Ast** handle) {
|
||||
(*handle) = (Toy_Ast*)Toy_partBucket(bucket, sizeof(Toy_Ast));
|
||||
Toy_Ast* tmp = (Toy_Ast*)Toy_partitionBucket(bucket, sizeof(Toy_Ast));
|
||||
|
||||
(*handle)->pass.type = TOY_AST_PASS;
|
||||
tmp->type = TOY_AST_PASS;
|
||||
|
||||
(*handle) = tmp;
|
||||
}
|
||||
|
||||
void Toy_private_emitAstError(Toy_Bucket** bucket, Toy_Ast** handle) {
|
||||
(*handle) = (Toy_Ast*)Toy_partBucket(bucket, sizeof(Toy_Ast));
|
||||
Toy_Ast* tmp = (Toy_Ast*)Toy_partitionBucket(bucket, sizeof(Toy_Ast));
|
||||
|
||||
(*handle)->error.type = TOY_AST_ERROR;
|
||||
tmp->type = TOY_AST_ERROR;
|
||||
|
||||
(*handle) = tmp;
|
||||
}
|
||||
|
||||
void Toy_private_emitAstEnd(Toy_Bucket** bucket, Toy_Ast** handle) {
|
||||
(*handle) = (Toy_Ast*)Toy_partBucket(bucket, sizeof(Toy_Ast));
|
||||
Toy_Ast* tmp = (Toy_Ast*)Toy_partitionBucket(bucket, sizeof(Toy_Ast));
|
||||
|
||||
(*handle)->error.type = TOY_AST_END;
|
||||
tmp->type = TOY_AST_END;
|
||||
|
||||
(*handle) = tmp;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user