WIP: Implementing arrays into the script, read more

I had intended to solve the Advent of Code puzzles in Toy, but they
don't work without arrays. I wasn't able to enable arrays in time, so
I need to focus on doing things correctly.

The most immediate tasks are marked with 'URGENT', and a lot of tests
need writing.
This commit is contained in:
2024-12-02 11:58:03 +11:00
parent 12c6ac938c
commit 62ca7a1fb7
16 changed files with 599 additions and 241 deletions

View File

@@ -23,7 +23,8 @@ int test_sizeof_ast_64bit() {
TEST_SIZEOF(Toy_AstBinary, 24);
TEST_SIZEOF(Toy_AstCompare, 24);
TEST_SIZEOF(Toy_AstGroup, 16);
TEST_SIZEOF(Toy_AstCompound, 24);
TEST_SIZEOF(Toy_AstCompound, 16);
TEST_SIZEOF(Toy_AstAggregate, 24);
TEST_SIZEOF(Toy_AstAssert, 24);
TEST_SIZEOF(Toy_AstIfThenElse, 32);
TEST_SIZEOF(Toy_AstWhileThen, 24);
@@ -61,7 +62,8 @@ int test_sizeof_ast_32bit() {
TEST_SIZEOF(Toy_AstBinary, 16);
TEST_SIZEOF(Toy_AstCompare, 16);
TEST_SIZEOF(Toy_AstGroup, 8);
TEST_SIZEOF(Toy_AstCompound, 16);
TEST_SIZEOF(Toy_AstCompound, 12);
TEST_SIZEOF(Toy_AstAggregate, 16);
TEST_SIZEOF(Toy_AstAssert, 12);
TEST_SIZEOF(Toy_AstIfThenElse, 16);
TEST_SIZEOF(Toy_AstWhileThen, 12);
@@ -194,33 +196,33 @@ int test_type_emission(Toy_Bucket** bucketHandle) {
}
}
//emit compound
//emit aggregate
{
//build the AST
Toy_Ast* ast = NULL;
Toy_Ast* right = NULL;
Toy_private_emitAstValue(bucketHandle, &ast, TOY_VALUE_FROM_INTEGER(42));
Toy_private_emitAstValue(bucketHandle, &right, TOY_VALUE_FROM_INTEGER(69));
Toy_private_emitAstCompound(bucketHandle, &ast, TOY_AST_FLAG_COMPOUND_COLLECTION, right);
Toy_private_emitAstAggregate(bucketHandle, &ast, TOY_AST_FLAG_COLLECTION, right);
//check if it worked
if (
ast == NULL ||
ast->type != TOY_AST_COMPOUND ||
ast->type != TOY_AST_AGGREGATE ||
ast->compound.left == NULL ||
ast->compound.left->type != TOY_AST_VALUE ||
TOY_VALUE_IS_INTEGER(ast->compound.left->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->compound.left->value.value) != 42 ||
ast->aggregate.left == NULL ||
ast->aggregate.left->type != TOY_AST_VALUE ||
TOY_VALUE_IS_INTEGER(ast->aggregate.left->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->aggregate.left->value.value) != 42 ||
ast->compound.right == NULL ||
ast->compound.right->type != TOY_AST_VALUE ||
TOY_VALUE_IS_INTEGER(ast->compound.right->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->compound.right->value.value) != 69 ||
ast->aggregate.right == NULL ||
ast->aggregate.right->type != TOY_AST_VALUE ||
TOY_VALUE_IS_INTEGER(ast->aggregate.right->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->aggregate.right->value.value) != 69 ||
false)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to emit a compound as 'Toy_Ast', state unknown\n" TOY_CC_RESET);
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to emit an aggregate as 'Toy_Ast', state unknown\n" TOY_CC_RESET);
return -1;
}
}

View File

@@ -455,7 +455,7 @@ int test_binary(Toy_Bucket** bucketHandle) {
return 0;
}
int test_compound(Toy_Bucket** bucketHandle) {
int test_aggregate(Toy_Bucket** bucketHandle) {
//test collections (do it first, because it's used below)
{
char* source = "1, 2, 3;";
@@ -467,27 +467,27 @@ int test_compound(Toy_Bucket** bucketHandle) {
ast->type != TOY_AST_BLOCK ||
ast->block.child == NULL ||
ast->block.child->type != TOY_AST_COMPOUND ||
ast->block.child->compound.flag != TOY_AST_FLAG_COMPOUND_COLLECTION ||
ast->block.child->type != TOY_AST_AGGREGATE ||
ast->block.child->aggregate.flag != TOY_AST_FLAG_COLLECTION ||
ast->block.child->compound.left == NULL ||
ast->block.child->compound.left->type != TOY_AST_VALUE ||
TOY_VALUE_IS_INTEGER(ast->block.child->compound.left->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->block.child->compound.left->value.value) != 1 ||
ast->block.child->aggregate.left == NULL ||
ast->block.child->aggregate.left->type != TOY_AST_VALUE ||
TOY_VALUE_IS_INTEGER(ast->block.child->aggregate.left->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->block.child->aggregate.left->value.value) != 1 ||
ast->block.child->compound.right == NULL ||
ast->block.child->compound.right->type != TOY_AST_COMPOUND ||
ast->block.child->compound.right->compound.flag != TOY_AST_FLAG_COMPOUND_COLLECTION ||
ast->block.child->aggregate.right == NULL ||
ast->block.child->aggregate.right->type != TOY_AST_AGGREGATE ||
ast->block.child->aggregate.right->aggregate.flag != TOY_AST_FLAG_COLLECTION ||
ast->block.child->compound.right->compound.left == NULL ||
ast->block.child->compound.right->compound.left->type != TOY_AST_VALUE ||
TOY_VALUE_IS_INTEGER(ast->block.child->compound.right->compound.left->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->block.child->compound.right->compound.left->value.value) != 2 ||
ast->block.child->aggregate.right->aggregate.left == NULL ||
ast->block.child->aggregate.right->aggregate.left->type != TOY_AST_VALUE ||
TOY_VALUE_IS_INTEGER(ast->block.child->aggregate.right->aggregate.left->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->block.child->aggregate.right->aggregate.left->value.value) != 2 ||
ast->block.child->compound.right->compound.right == NULL ||
ast->block.child->compound.right->compound.right->type != TOY_AST_VALUE ||
TOY_VALUE_IS_INTEGER(ast->block.child->compound.right->compound.right->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->block.child->compound.right->compound.right->value.value) != 3 ||
ast->block.child->aggregate.right->aggregate.right == NULL ||
ast->block.child->aggregate.right->aggregate.right->type != TOY_AST_VALUE ||
TOY_VALUE_IS_INTEGER(ast->block.child->aggregate.right->aggregate.right->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->block.child->aggregate.right->aggregate.right->value.value) != 3 ||
false)
{
@@ -507,17 +507,17 @@ int test_compound(Toy_Bucket** bucketHandle) {
ast->type != TOY_AST_BLOCK ||
ast->block.child == NULL ||
ast->block.child->type != TOY_AST_COMPOUND ||
ast->block.child->compound.flag != TOY_AST_FLAG_COMPOUND_INDEX ||
ast->block.child->type != TOY_AST_AGGREGATE ||
ast->block.child->aggregate.flag != TOY_AST_FLAG_INDEX ||
ast->block.child->compound.left == NULL ||
ast->block.child->compound.left->type != TOY_AST_VAR_ACCESS ||
ast->block.child->compound.left->varAccess.name->type != TOY_STRING_NAME ||
strcmp(ast->block.child->compound.left->varAccess.name->as.name.data, "name") != 0 ||
ast->block.child->aggregate.left == NULL ||
ast->block.child->aggregate.left->type != TOY_AST_VAR_ACCESS ||
ast->block.child->aggregate.left->varAccess.name->type != TOY_STRING_NAME ||
strcmp(ast->block.child->aggregate.left->varAccess.name->as.name.data, "name") != 0 ||
ast->block.child->compound.right->type != TOY_AST_VALUE ||
TOY_VALUE_IS_INTEGER(ast->block.child->compound.right->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->block.child->compound.right->value.value) != 0 ||
ast->block.child->aggregate.right->type != TOY_AST_VALUE ||
TOY_VALUE_IS_INTEGER(ast->block.child->aggregate.right->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->block.child->aggregate.right->value.value) != 0 ||
false)
{
@@ -537,23 +537,23 @@ int test_compound(Toy_Bucket** bucketHandle) {
ast->type != TOY_AST_BLOCK ||
ast->block.child == NULL ||
ast->block.child->type != TOY_AST_COMPOUND ||
ast->block.child->compound.flag != TOY_AST_FLAG_COMPOUND_INDEX ||
ast->block.child->type != TOY_AST_AGGREGATE ||
ast->block.child->aggregate.flag != TOY_AST_FLAG_INDEX ||
ast->block.child->compound.left == NULL ||
ast->block.child->compound.left->type != TOY_AST_VAR_ACCESS ||
ast->block.child->compound.left->varAccess.name->type != TOY_STRING_NAME ||
strcmp(ast->block.child->compound.left->varAccess.name->as.name.data, "name") != 0 ||
ast->block.child->aggregate.left == NULL ||
ast->block.child->aggregate.left->type != TOY_AST_VAR_ACCESS ||
ast->block.child->aggregate.left->varAccess.name->type != TOY_STRING_NAME ||
strcmp(ast->block.child->aggregate.left->varAccess.name->as.name.data, "name") != 0 ||
ast->block.child->compound.right->type != TOY_AST_COMPOUND ||
ast->block.child->compound.right->compound.flag != TOY_AST_FLAG_COMPOUND_COLLECTION ||
ast->block.child->compound.right->compound.left->type != TOY_AST_VALUE ||
ast->block.child->aggregate.right->type != TOY_AST_AGGREGATE ||
ast->block.child->aggregate.right->aggregate.flag != TOY_AST_FLAG_COLLECTION ||
ast->block.child->aggregate.right->aggregate.left->type != TOY_AST_VALUE ||
TOY_VALUE_IS_INTEGER(ast->block.child->compound.right->compound.left->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->block.child->compound.right->compound.left->value.value) != 0 ||
TOY_VALUE_IS_INTEGER(ast->block.child->aggregate.right->aggregate.left->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->block.child->aggregate.right->aggregate.left->value.value) != 0 ||
TOY_VALUE_IS_INTEGER(ast->block.child->compound.right->compound.right->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->block.child->compound.right->compound.right->value.value) != 1 ||
TOY_VALUE_IS_INTEGER(ast->block.child->aggregate.right->aggregate.right->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->block.child->aggregate.right->aggregate.right->value.value) != 1 ||
false)
{
@@ -927,7 +927,7 @@ int main() {
{
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
res = test_compound(&bucket);
res = test_aggregate(&bucket);
Toy_freeBucket(&bucket);
if (res == 0) {
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);

View File

@@ -329,7 +329,7 @@ int test_scope_elements() {
return -1;
}
Toy_Value result = Toy_accessScope(scope, hello2);
Toy_Value* result = Toy_accessScopeAsPointer(scope, hello2);
//check integer
if (scope == NULL ||
@@ -338,8 +338,8 @@ int test_scope_elements() {
scope->table->capacity != 8 ||
scope->refCount != 1 ||
TOY_VALUE_IS_INTEGER(result) != true ||
TOY_VALUE_AS_INTEGER(result) != 42 ||
TOY_VALUE_IS_INTEGER(*result) != true ||
TOY_VALUE_AS_INTEGER(*result) != 42 ||
false)
{
@@ -354,7 +354,7 @@ int test_scope_elements() {
//assign values
Toy_assignScope(scope, hello1, TOY_VALUE_FROM_FLOAT(3.1415f));
Toy_Value resultTwo = Toy_accessScope(scope, hello2);
Toy_Value* resultTwo = Toy_accessScopeAsPointer(scope, hello2);
//check float
if (scope == NULL ||
@@ -363,8 +363,8 @@ int test_scope_elements() {
scope->table->capacity != 8 ||
scope->refCount != 1 ||
TOY_VALUE_IS_FLOAT(resultTwo) != true ||
TOY_VALUE_AS_FLOAT(resultTwo) != 3.1415f ||
TOY_VALUE_IS_FLOAT(*resultTwo) != true ||
TOY_VALUE_AS_FLOAT(*resultTwo) != 3.1415f ||
false)
{
@@ -399,10 +399,10 @@ int test_scope_elements() {
{
//check it's accessible
Toy_Value result1 = Toy_accessScope(scope, hello);
Toy_Value* result1 = Toy_accessScopeAsPointer(scope, hello);
if (TOY_VALUE_IS_INTEGER(result1) != true ||
TOY_VALUE_AS_INTEGER(result1) != 42)
if (TOY_VALUE_IS_INTEGER(*result1) != true ||
TOY_VALUE_AS_INTEGER(*result1) != 42)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to access from an ancestor Toy_Scope\n" TOY_CC_RESET);
Toy_freeString(hello);
@@ -416,10 +416,10 @@ int test_scope_elements() {
{
//check it's shadowed correctly
Toy_Value result2 = Toy_accessScope(scope, hello);
Toy_Value* result2 = Toy_accessScopeAsPointer(scope, hello);
if (TOY_VALUE_IS_FLOAT(result2) != true ||
TOY_VALUE_AS_FLOAT(result2) != 3.1415f)
if (TOY_VALUE_IS_FLOAT(*result2) != true ||
TOY_VALUE_AS_FLOAT(*result2) != 3.1415f)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to shadow an entry in Toy_Scope\n" TOY_CC_RESET);
Toy_freeString(hello);
@@ -433,10 +433,10 @@ int test_scope_elements() {
{
//check it's recovered correctly
Toy_Value result3 = Toy_accessScope(scope, hello);
Toy_Value* result3 = Toy_accessScopeAsPointer(scope, hello);
if (TOY_VALUE_IS_INTEGER(result3) != true ||
TOY_VALUE_AS_INTEGER(result3) != 42)
if (TOY_VALUE_IS_INTEGER(*result3) != true ||
TOY_VALUE_AS_INTEGER(*result3) != 42)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to recover an entry in Toy_Scope\n" TOY_CC_RESET);
Toy_freeString(hello);
@@ -450,10 +450,10 @@ int test_scope_elements() {
{
//check it's assigned correctly
Toy_Value result4 = Toy_accessScope(scope, hello);
Toy_Value* result4 = Toy_accessScopeAsPointer(scope, hello);
if (TOY_VALUE_IS_INTEGER(result4) != true ||
TOY_VALUE_AS_INTEGER(result4) != 8891)
if (TOY_VALUE_IS_INTEGER(*result4) != true ||
TOY_VALUE_AS_INTEGER(*result4) != 8891)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to assign to an ancestor in Toy_Scope\n" TOY_CC_RESET);
Toy_freeString(hello);
@@ -467,10 +467,10 @@ int test_scope_elements() {
{
//check it's in the correct state
Toy_Value result5 = Toy_accessScope(scope, hello);
Toy_Value* result5 = Toy_accessScopeAsPointer(scope, hello);
if (TOY_VALUE_IS_INTEGER(result5) != true ||
TOY_VALUE_AS_INTEGER(result5) != 8891)
if (TOY_VALUE_IS_INTEGER(*result5) != true ||
TOY_VALUE_AS_INTEGER(*result5) != 8891)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to access an altered entry of an ancestor in Toy_Scope\n" TOY_CC_RESET);
Toy_freeString(hello);

View File

@@ -663,8 +663,8 @@ int test_scope(Toy_Bucket** bucketHandle) {
vm.scope == NULL ||
Toy_isDeclaredScope(vm.scope, key) == false ||
TOY_VALUE_IS_INTEGER(Toy_accessScope(vm.scope, key)) != true ||
TOY_VALUE_AS_INTEGER(Toy_accessScope(vm.scope, key)) != 42
TOY_VALUE_IS_INTEGER(*Toy_accessScopeAsPointer(vm.scope, key)) != true ||
TOY_VALUE_AS_INTEGER(*Toy_accessScopeAsPointer(vm.scope, key)) != 42
)
{
@@ -712,7 +712,7 @@ int test_scope(Toy_Bucket** bucketHandle) {
vm.scope == NULL ||
Toy_isDeclaredScope(vm.scope, key) == false ||
TOY_VALUE_IS_NULL(Toy_accessScope(vm.scope, key)) != true
TOY_VALUE_IS_NULL(*Toy_accessScopeAsPointer(vm.scope, key)) != true
)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: Unexpected result in 'Toy_VM' when testing scope, source: %s\n" TOY_CC_RESET, source);