@@ -3,7 +3,7 @@ import { cloneDeep } from "lodash";
3
3
import {
4
4
Assignment ,
5
5
BinaryExpression ,
6
- Block ,
6
+ Block , BreakStatement ,
7
7
ClassInstanceCreationExpression ,
8
8
ExplicitConstructorInvocation ,
9
9
Expression ,
@@ -58,7 +58,7 @@ import {
58
58
ResConOverloadInstr ,
59
59
ResOverrideInstr ,
60
60
ResTypeContInstr ,
61
- StructType , CondInstr , SwitchInstr
61
+ StructType , BranchInstr , SwitchInstr
62
62
} from './types'
63
63
import {
64
64
defaultValues ,
@@ -419,7 +419,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
419
419
control : Control ,
420
420
_stash : Stash ,
421
421
) => {
422
- control . push ( instr . condInstr ( command . consequent , command . alternate , command ) ) ;
422
+ control . push ( instr . branchInstr ( command . consequent , command . alternate , command ) ) ;
423
423
control . push ( command . condition ) ;
424
424
} ,
425
425
@@ -429,10 +429,23 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
429
429
control : Control ,
430
430
_stash : Stash ,
431
431
) => {
432
+ control . push ( instr . markerInstr ( command ) ) ;
432
433
control . push ( instr . switchInstr ( command . cases , command . expression , command ) ) ;
433
434
control . push ( command . expression ) ;
434
435
} ,
435
436
437
+ BreakStatement : (
438
+ _command : BreakStatement ,
439
+ _environment : Environment ,
440
+ control : Control ,
441
+ _stash : Stash ,
442
+ ) => {
443
+ while ( ( control . peek ( ) as Instr ) . instrType != InstrType . MARKER ) {
444
+ control . pop ( ) ;
445
+ }
446
+
447
+ control . pop ( ) ; // pop the marker
448
+ } ,
436
449
437
450
[ InstrType . POP ] : (
438
451
_command : Instr ,
@@ -863,8 +876,8 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
863
876
// No post-processing required for constructor.
864
877
} ,
865
878
866
- [ InstrType . COND ] : (
867
- command : CondInstr ,
879
+ [ InstrType . BRANCH ] : (
880
+ command : BranchInstr ,
868
881
_environment : Environment ,
869
882
control : Control ,
870
883
stash : Stash ,
@@ -895,34 +908,40 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
895
908
const discValue = stash . pop ( ) as Literal ;
896
909
897
910
let matchedCase : SwitchCase | null = null ;
911
+ let matchedIndex = - 1 ;
898
912
899
913
// Iterate over each switch case.
900
- for ( const swCase of command . cases ) {
914
+ for ( let i = 0 ; i < command . cases . length ; i ++ ) {
915
+ const swCase = command . cases [ i ] ;
901
916
// Check all labels for this case.
902
917
for ( const label of swCase . labels ) {
903
918
if ( label . kind === "CaseLabel" ) {
904
919
// Assume the case label's expression is a literal.
905
920
const caseLiteral = label . expression as Literal ;
906
921
if ( discValue . literalType . value === caseLiteral . literalType . value ) {
907
922
matchedCase = swCase ;
923
+ matchedIndex = i ;
908
924
break ;
909
925
}
910
- } else if ( label . kind === "DefaultLabel" ) {
926
+ } else if ( label . kind === "DefaultLabel" && ! matchedCase ) {
911
927
// Save default case (only one default should exist).
912
928
matchedCase = swCase ;
929
+ matchedIndex = i ;
913
930
}
914
931
}
915
- if ( matchedCase ) break ;
916
932
}
917
933
918
- // Determine which case to use.
919
- if ( matchedCase ) {
920
- if ( matchedCase && matchedCase . statements && matchedCase . statements . length > 0 ) {
934
+ if ( ! matchedCase ) {
935
+ return // do nothing if no matching case found.
936
+ }
937
+
938
+ for ( let i = command . cases . length ; i >= matchedIndex ; i -- ) {
939
+ const swCase = command . cases [ i ] ;
940
+
941
+ if ( swCase && swCase . statements && swCase . statements . length > 0 ) {
921
942
// Push the statements in reverse order to the control stack.
922
- for ( let i = matchedCase . statements . length - 1 ; i >= 0 ; i -- ) {
923
- if ( matchedCase . statements [ i ] . kind == "BreakStatement" )
924
- continue
925
- control . push ( matchedCase . statements [ i ] ) ;
943
+ for ( let j = swCase . statements . length - 1 ; j >= 0 ; j -- ) {
944
+ control . push ( swCase . statements [ j ] ) ;
926
945
}
927
946
}
928
947
}
0 commit comments