Skip to content

Commit

Permalink
EMERGENCY: Computer is dying, this is an incomplete commit, do not use
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratstail91 committed Nov 22, 2024
1 parent b2b2ca7 commit b29a878
Show file tree
Hide file tree
Showing 11 changed files with 386 additions and 14 deletions.
Empty file removed scripts/.gitkeep
Empty file.
10 changes: 10 additions & 0 deletions scripts/big_if_true.toy
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
if (true) print "Correct"; else print "Error";
if (true) print "Correct"; else print "Error";
if (true) print "Correct"; else print "Error";
if (true) print "Correct"; else print "Error";
if (true) print "Correct"; else print "Error";
if (true) print "Correct"; else print "Error";
if (true) print "Correct"; else print "Error";
if (true) print "Correct"; else print "Error";
if (true) print "Correct"; else print "Error";
if (true) print "Correct"; else print "Error";
11 changes: 11 additions & 0 deletions scripts/brace_yourself.toy
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

if (true) { print "Correct"; } else { print "Error"; }
if (true) { print "Correct"; } else { print "Error"; }
if (true) { print "Correct"; } else { print "Error"; }
if (true) { print "Correct"; } else { print "Error"; }
if (true) { print "Correct"; } else { print "Error"; }
if (true) { print "Correct"; } else { print "Error"; }
if (true) { print "Correct"; } else { print "Error"; }
if (false) { print "Correct"; } else { print "Error"; }
if (true) { print "Correct"; } else { print "Error"; }
if (true) { print "Correct"; } else { print "Error"; }
10 changes: 10 additions & 0 deletions scripts/concat.toy
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
print "foo" .. "bar";
print "foo" .. "bar";
print "foo" .. "bar";
print "foo" .. "bar";
print "foo" .. "bar";
print "foo" .. "bar";
print "foo" .. "bar";
print "foo" .. "bar";
print "foo" .. "bar";
print "foo" .. "bar";
43 changes: 33 additions & 10 deletions scripts/cond.toy
Original file line number Diff line number Diff line change
@@ -1,44 +1,67 @@

//literals
if (true) {
print "Success";
print "Success 1";
}
else {
print "Failed";
print "Failure 1";
}

//false literals
if (false) {
print "Failed";
print "Failure 2";
}
else {
print "Success";
print "Success 2";
}

//conditionals
if (1 < 2) {
print "Success";
print "Success 3";
}
if (1 > 2) {
print "Failure";
print "Failure 3";
}


//variables
var a = 42;

if (a) {
print "Success";
print "Success 4";
}
else {
print "Failure";
print "Failure 4";
}


if (a == 42) {
print "Success";
print "Success 5";
}
else {
print "Failure";
print "Failure 5";
}

//concatenated strings
if ("foo" .. "bar" == "foobar") {
print "Success 6";
}
else {
print "Failure 6";
}


if ("foobar" == "foo" .. "bar") {
print "Success 7";
}
else {
print "Failure 7";
}

if ("fizz" .. "le" == "fi" .. "zzle") {
print "Success 8";
}
else {
print "Failure 8";
}

6 changes: 3 additions & 3 deletions source/toy_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ Toy_String* Toy_concatStrings(Toy_Bucket** bucketHandle, Toy_String* left, Toy_S
ret->as.node.left = left;
ret->as.node.right = right;

incrementRefCount(left);
incrementRefCount(left);//URGENT: improve
incrementRefCount(right);

return ret;
Expand Down Expand Up @@ -253,7 +253,7 @@ static int deepCompareUtil(Toy_String* left, Toy_String* right, const char** lef
}

//BUGFIX: if we're not currently iterating through the left leaf (and leftHead is not null), skip out
if (left->type == TOY_STRING_LEAF && (*leftHead) != NULL && (**leftHead) != '\0' && ((*leftHead) < left->as.leaf.data || (*leftHead) > (left->as.leaf.data + strlen(left->as.leaf.data))) ) {
if (left->type == TOY_STRING_LEAF && (*leftHead) != NULL && (**leftHead) != '\0' && ((*leftHead) < left->as.leaf.data || (*leftHead) > (left->as.leaf.data + strlen(left->as.leaf.data))) ) { //URGENT: replace strlen with the stored lengths
return result;
}

Expand Down Expand Up @@ -328,7 +328,7 @@ int Toy_compareStrings(Toy_String* left, Toy_String* right) {
exit(-1);
}

return strcmp(left->as.name.data, right->as.name.data);
return strcmp(left->as.name.data, right->as.name.data); //URGENT: strncmp
}

//util pointers
Expand Down
75 changes: 75 additions & 0 deletions tests/cases/test_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,81 @@ int test_type_emission(Toy_Bucket** bucketHandle) {
}
}

//emit keyword if-then
{
//build the AST
Toy_Ast* ast = NULL;
Toy_Ast* condBranch = NULL;
Toy_Ast* thenBranch = NULL;

Toy_private_emitAstValue(bucketHandle, &condBranch, TOY_VALUE_FROM_INTEGER(42));
Toy_private_emitAstValue(bucketHandle, &thenBranch, TOY_VALUE_FROM_INTEGER(69));

Toy_private_emitAstIfThenElse(bucketHandle, &ast, condBranch, thenBranch, NULL);

//check if it worked
if (
ast == NULL ||
ast->type != TOY_AST_IF_THEN_ELSE ||
ast->ifThenElse.condBranch == NULL ||
ast->ifThenElse.condBranch->type != TOY_AST_VALUE ||
TOY_VALUE_IS_INTEGER(ast->ifThenElse.condBranch->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->ifThenElse.condBranch->value.value) != 42 ||

ast->ifThenElse.thenBranch == NULL ||
ast->ifThenElse.thenBranch->type != TOY_AST_VALUE ||
TOY_VALUE_IS_INTEGER(ast->ifThenElse.thenBranch->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->ifThenElse.thenBranch->value.value) != 69 ||

ast->ifThenElse.elseBranch != NULL ||

false)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to emit a keyword 'if-then' as 'Toy_Ast', state unknown\n" TOY_CC_RESET);
return -1;
}
}

//emit keyword if-then-else
{
//build the AST
Toy_Ast* ast = NULL;
Toy_Ast* condBranch = NULL;
Toy_Ast* thenBranch = NULL;
Toy_Ast* elseBranch = NULL;

Toy_private_emitAstValue(bucketHandle, &condBranch, TOY_VALUE_FROM_INTEGER(42));
Toy_private_emitAstValue(bucketHandle, &thenBranch, TOY_VALUE_FROM_INTEGER(69));
Toy_private_emitAstValue(bucketHandle, &elseBranch, TOY_VALUE_FROM_INTEGER(420));

Toy_private_emitAstIfThenElse(bucketHandle, &ast, condBranch, thenBranch, elseBranch);

//check if it worked
if (
ast == NULL ||
ast->type != TOY_AST_IF_THEN_ELSE ||
ast->ifThenElse.condBranch == NULL ||
ast->ifThenElse.condBranch->type != TOY_AST_VALUE ||
TOY_VALUE_IS_INTEGER(ast->ifThenElse.condBranch->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->ifThenElse.condBranch->value.value) != 42 ||

ast->ifThenElse.thenBranch == NULL ||
ast->ifThenElse.thenBranch->type != TOY_AST_VALUE ||
TOY_VALUE_IS_INTEGER(ast->ifThenElse.thenBranch->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->ifThenElse.thenBranch->value.value) != 69 ||

ast->ifThenElse.elseBranch == NULL ||
ast->ifThenElse.elseBranch->type != TOY_AST_VALUE ||
TOY_VALUE_IS_INTEGER(ast->ifThenElse.elseBranch->value.value) != true ||
TOY_VALUE_AS_INTEGER(ast->ifThenElse.elseBranch->value.value) != 420 ||

false)
{
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to emit a keyword 'if-then-else' as 'Toy_Ast', state unknown\n" TOY_CC_RESET);
return -1;
}
}

//emit keyword print
{
//build the AST
Expand Down
Loading

0 comments on commit b29a878

Please sign in to comment.