@@ -78,7 +78,7 @@ function typecheck(
78
78
let imported = [ ] ;
79
79
let strayvar = [ ] ;
80
80
let scope = [ { } ] ;
81
- let scopestarts = [ 0 ] ;
81
+ let scopestarts = [ { pos : 0 , op : "global" } ] ;
82
82
let signature = [ ] ;
83
83
let funstack = [ ] ;
84
84
let funretcnt = [ ] ;
@@ -178,15 +178,20 @@ function typecheck(
178
178
return t . fields [ x ] ;
179
179
}
180
180
181
- function scopepush ( pos ) {
181
+ function scopepush ( tok ) {
182
182
scope . push ( { } ) ;
183
- scopestarts . push ( pos ) ;
183
+ scopestarts . push ( tok ) ;
184
184
}
185
185
186
- function scopepop ( pos ) {
186
+ function scopepop ( tok , ... acceptScopeStartOps ) {
187
187
let ss = scopestarts . pop ( ) ;
188
188
let s = scope . pop ( ) ;
189
- signature . push ( [ [ ss , pos , scope . length ] , s ] ) ;
189
+ assert (
190
+ `[Type] Unexpected '${ tok . op } ' in '${ ss . op } ' block` ,
191
+ tok . pos ,
192
+ acceptScopeStartOps . indexOf ( ss . op ) >= 0
193
+ ) ;
194
+ signature . push ( [ [ ss . pos , tok . pos , scope . length ] , s ] ) ;
190
195
}
191
196
192
197
function logscope ( ) {
@@ -303,11 +308,11 @@ function typecheck(
303
308
let ptr = scope [ scope . length - 1 ] [ funstack [ funstack . length - 1 ] ] ;
304
309
ptr . in = inittype ( "nil" ) ;
305
310
}
306
- scopepush ( a . pos ) ;
311
+ scopepush ( a ) ;
307
312
} else if ( a . op == "funend" ) {
308
313
var f = funstack . pop ( ) ;
309
314
var n = funretcnt . pop ( ) ;
310
- scopepop ( a . pos ) ;
315
+ scopepop ( a , "funbody" ) ;
311
316
312
317
if ( n == 0 ) {
313
318
var ptr = scope [ scope . length - 1 ] [ f ] ;
@@ -325,12 +330,12 @@ function typecheck(
325
330
a . type
326
331
) ;
327
332
} else if ( a . op == "end" ) {
328
- scopepop ( a . pos ) ;
333
+ scopepop ( a , "if" , "else" , "for" , "whiletrue" , "whilen" ) ;
329
334
} else if ( a . op == "if" ) {
330
- scopepush ( a . pos ) ;
335
+ scopepush ( a ) ;
331
336
} else if ( a . op == "else" ) {
332
- scopepop ( a . pos ) ;
333
- scopepush ( a . pos ) ;
337
+ scopepop ( a , "if" ) ;
338
+ scopepush ( a ) ;
334
339
} else if ( a . op == "return" ) {
335
340
funretcnt [ funretcnt . length - 1 ] ++ ;
336
341
let ptr ;
@@ -536,14 +541,14 @@ function typecheck(
536
541
537
542
gettype ( a . container ) . element = Object . assign ( { } , typ ) ;
538
543
} else if ( a . op == "for" ) {
539
- scopepush ( a . pos ) ;
544
+ scopepush ( a ) ;
540
545
typeassert ( `For-each LHS` , [ inittype ( "arr" ) ] , a . container , a . pos ) ;
541
546
scope [ scope . length - 1 ] [ a . iterator ] =
542
547
gettype ( a . container ) . element || inittype ( "any" ) ;
543
548
} else if ( a . op == "whiletrue" ) {
544
- scopepush ( a . pos ) ;
549
+ scopepush ( a ) ;
545
550
} else if ( a . op == "whilen" ) {
546
- scopepush ( a . pos ) ;
551
+ scopepush ( a ) ;
547
552
} else if ( a . op == "break" ) {
548
553
//pass
549
554
} else if ( a . op == "not" ) {
@@ -657,22 +662,25 @@ function typecheck(
657
662
} else if ( a . op == "import" ) {
658
663
imported = imported . concat ( a . iden ) ;
659
664
} else if ( a . op == "try" ) {
660
- scopepush ( a . pos ) ;
665
+ scopepush ( a ) ;
661
666
} else if ( a . op == "catch" ) {
662
- scopepop ( a . pos ) ;
663
- scopepush ( a . pos ) ;
667
+ scopepop ( a , "try" ) ;
668
+ scopepush ( a ) ;
664
669
} else if ( a . op == "catcherr" ) {
665
- scopepop ( a . pos ) ;
666
- scopepush ( a . pos ) ;
670
+ scopepop ( a , "catch" , "catcherr" ) ;
671
+ scopepush ( a ) ;
672
+ if ( a . error === undefined ) {
673
+ strayvar . push ( inittype ( "str" ) ) ;
674
+ }
667
675
} else if ( a . op == "tryend" ) {
668
- scopepop ( a . pos ) ;
676
+ scopepop ( a , "catch" , "catcherr" ) ;
669
677
} else if ( a . op == "throw" ) {
670
678
} else if ( a . op == "comment" ) {
671
679
//pass
672
680
} else {
673
681
}
674
682
}
675
- scopepop ( a . pos ) ;
683
+ scopepop ( { pos : a . pos , op : "EOF" } , "global" ) ;
676
684
// console.log(scope.length)
677
685
// console.dir(signature,{maxArrayLength:null,depth:null})
678
686
0 commit comments