Skip to content

Commit a50ef4a

Browse files
committed
fixed bug with switch
1 parent 9ad9b2a commit a50ef4a

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

README.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -768,10 +768,23 @@ undefined
768768
The *ife()* function works similarly to the *ifs()* function except it executes one and only one statement. Meaning, it will go through each boolean expression, checking if it is true, and if it is, will execute that statement and stop looking through the other pairs. If none of the pairs are true, it executes the else call.
769769

770770
```
771-
ife(
772-
>(len(lst), 2), put(lst, true),
773-
==(len(lst), 0), append(lst, list(2)),
774-
!=(len(lst), 0), append(lst, 3),
775-
$(`all false`)
776-
)
771+
>> =(lst, list())
772+
>> ife(
773+
.. >(len(lst), 2), put(lst, true),
774+
.. ==(len(lst), 0), append(lst, list(2)),
775+
.. !=(len(lst), 0), append(lst, 3),
776+
.. $(`all false`)
777+
.. )
778+
>> do(lst)
779+
[[2]]
780+
```
781+
782+
###switch([value], case, call\_exp...)
783+
784+
The *switch()* function in RoyalScript facilitates pattern matching, its similar to a series of *==()* functions, but is faster, especcially for numbers.
785+
786+
The switch statements do not fall through between each case pair, meaning the first case that matches will executes, terminating the progression to further cases. There is also no default block for RoyalScript switch statements.
787+
788+
```
789+
777790
```

Tests/testsconds.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ t.compileTest("ifs(true, return(1, 2), ==(3, 3), $(4))", "if(true){return [1, 2]
1616
t.compileTest("switch(b, 4, $(3), 5, $(4), true, $(1, 2))", "switch(b){case 4: console.log(3);break;case 5: console.log(4);break;case true: console.log(1, 2);break;};", 11);
1717
t.compileTest("ife(==(3, 3), return(3), !=(3, true), $(1, 2, 3, 4), return(list(1, 2, 3)))", "if(3 === 3){return 3;};else if(3 !== true){console.log(1, 2, 3, 4);};else{return [1, 2, 3];};", 12);
1818
t.compileTest("?(true, ?(3, $(1, 2, 3)))", "if(true){if(3){console.log(1, 2, 3);};};", 13);
19-
t.compileTest("ife(>(len(lst), 2), put(lst, true),==(len(lst), 0), append(lst, list(2)),!=(len(lst), 0), append(lst, 3),$(`all false`))", "", 15);
19+
t.compileTest("switch(e, 0, `good`, `bar`, 5,`foobar`, list(1, 2, 3), 1, `r`)", "", 15);

builds/WebMain.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,12 @@ var STD = {
479479
casemode = false;
480480
}
481481
else {
482-
str += callLib(this, args[i], args[i+1]) + "break;";
482+
str += callLib(this, args[i], args[i+1]) + ";break;";
483483
casemode = true;
484484
}
485485
}
486486
};
487-
if(!(casemode)) str += "break;";
487+
if(!(casemode)) str += ";break;";
488488
return str + "};";
489489
},
490490
//IF ELSE CHAIN FUNCTION

src/stdlib.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,12 @@ var STD = {
357357
casemode = false;
358358
}
359359
else {
360-
str += callLib(this, args[i], args[i+1]) + "break;";
360+
str += callLib(this, args[i], args[i+1]) + ";break;";
361361
casemode = true;
362362
}
363363
}
364364
};
365-
if(!(casemode)) str += "break;";
365+
if(!(casemode)) str += ";break;";
366366
return str + "};";
367367
},
368368
//IF ELSE CHAIN FUNCTION

0 commit comments

Comments
 (0)