Skip to content

Commit

Permalink
Keywords 'break' & 'continue' tested
Browse files Browse the repository at this point in the history
There were a couple bugs - I'm glad I'm so thorough with these tests.

See #152
  • Loading branch information
Ratstail91 committed Dec 30, 2024
1 parent b84a70c commit 23eb3e4
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 28 deletions.
106 changes: 79 additions & 27 deletions scripts/breakdancing.toy
Original file line number Diff line number Diff line change
@@ -1,48 +1,100 @@
//make sure it works with multiple repititions

//-------------------------

//test break
while (true) {
print "1";
break;
print "2";
assert false, "break failed";
}

print "3";
//test continue
var flag1: bool = true;
while (flag1) {
flag1 = false;
continue;
assert false, "continue failed";
}

print "done";

//-------------------------

//test break
while (true) {
print 1;
while (true) {
print 2;
if (true) {
print 3;
while (true) {
print 4;
break;
print 5;
}
print 6;
}
print 7;
}
print 8;
break;
assert false, "break failed";
}

print 9;
//test continue
var flag2: bool = true;
while (flag2) {
flag2 = false;
continue;
assert false, "continue failed";
}

print "done";

//-------------------------

//test break
while (true) {
print 1;
break;
assert false, "break failed";
}

//test continue
var flag3: bool = true;
while (flag3) {
flag3 = false;
continue;
assert false, "continue failed";
}

print "done";

//-------------------------

{
//test break
while (true) {
print 2;
if (true) {
print 3;
break;
assert false, "break failed";
}

//test continue
var flag4: bool = true;
while (flag4) {
flag4 = false;
continue;
assert false, "continue failed";
}

print "done";
}

//-------------------------

{
//test break
while (true) {
{
break;
print 6;
}
print 7;
assert false, "break failed";
}

//test continue
var flag5: bool = true;
while (flag5) {
flag5 = false;
{
continue;
}
assert false, "continue failed";
}
print 8;

print "done";
}

print 9;
2 changes: 1 addition & 1 deletion source/toy_routine.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ static unsigned int writeInstructionWhileThen(Toy_Routine** rt, Toy_AstWhileThen

unsigned int diff = depth - (*rt)->currentScopeDepth;

OVERWRITE_INT(rt, code, addr, addr - (CURRENT_ADDRESS(rt, code) + 8)); //tell continue to return to the start AFTER reading the instruction
OVERWRITE_INT(rt, code, addr, CURRENT_ADDRESS(rt, code) - (addr + 8)); //tell continue to return to the start AFTER reading the instruction
OVERWRITE_INT(rt, code, addr, diff);

//tick down
Expand Down
1 change: 1 addition & 0 deletions source/toy_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ static void processEscape(Toy_VM* vm) {

while (diff > 0 && vm->scope != NULL) {
vm->scope = Toy_popScope(vm->scope);
diff--;
}
}

Expand Down
100 changes: 100 additions & 0 deletions tests/integrations/test_keyword_while_break_continue.toy
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//make sure it works with multiple repititions

//-------------------------

//test break
while (true) {
break;
assert false, "break failed";
}

//test continue
var flag1: bool = true;
while (flag1) {
flag1 = false;
continue;
assert false, "continue failed";
}

print "done";

//-------------------------

//test break
while (true) {
break;
assert false, "break failed";
}

//test continue
var flag2: bool = true;
while (flag2) {
flag2 = false;
continue;
assert false, "continue failed";
}

print "done";

//-------------------------

//test break
while (true) {
break;
assert false, "break failed";
}

//test continue
var flag3: bool = true;
while (flag3) {
flag3 = false;
continue;
assert false, "continue failed";
}

print "done";

//-------------------------

{
//test break
while (true) {
break;
assert false, "break failed";
}

//test continue
var flag4: bool = true;
while (flag4) {
flag4 = false;
continue;
assert false, "continue failed";
}

print "done";
}

//-------------------------

{
//test break
while (true) {
{
break;
}
assert false, "break failed";
}

//test continue
var flag5: bool = true;
while (flag5) {
flag5 = false;
{
continue;
}
assert false, "continue failed";
}

print "done";
}

0 comments on commit 23eb3e4

Please sign in to comment.