@@ -12,38 +12,39 @@ import {
1212 resetFunctionBodies ,
1313 userDefinedFunctions ,
1414} from "./compiler/functions" ;
15- import type { AST } from "./parser" ;
1615import {
1716 clearScopes ,
17+ consumeScratchIndex ,
1818 createStringTable ,
1919 declareVar ,
2020 enterScope ,
2121 exitScope ,
2222 getLocalDecls ,
2323 getLocalVarsIndex ,
24- consumeScratchIndex ,
2524 getVar ,
26- setLocalVarsIndex ,
2725 scopes ,
26+ setLocalVarsIndex ,
2827 setScopes ,
2928} from "./compiler/state" ;
30- import type { Expression , Statement } from "./syntax" ;
3129import {
32- local ,
33- nativeBinOps ,
34- valType ,
30+ block ,
31+ control ,
3532 emitSection ,
36- unsignedLEB ,
3733 encodeString ,
38- i32 ,
3934 f64 ,
40- control ,
4135 fn ,
42- block ,
43- loop ,
36+ i32 ,
4437 if_ ,
38+ local ,
39+ loop ,
4540 misc ,
41+ nativeBinOps ,
42+ unsignedLEB ,
43+ valType ,
4644} from "./compiler/wasm" ;
45+ import type { AST } from "./parser" ;
46+ import type { Expression , Statement } from "./syntax" ;
47+
4748class CompilerError extends Error {
4849 line : number ;
4950 column : number ;
@@ -140,11 +141,10 @@ const _compile = (
140141 // Type section for all functions
141142 const typeSection = emitSection (
142143 1 ,
143- // biome-ignore format:
144144 new Uint8Array ( [
145- ...unsignedLEB ( functionTypes . length ) , // number of types
146- ...functionTypes . flat ( ) , // all type entries defined at the top
147- ] ) ,
145+ ...unsignedLEB ( functionTypes . length ) , // number of types
146+ ...functionTypes . flat ( ) , // all type entries defined at the top
147+ ] ) ,
148148 ) ;
149149
150150 // Import section (import print/println from env)
@@ -353,11 +353,11 @@ const compileStatement = (
353353
354354 const counterIndex = consumeScratchIndex ( ) ;
355355
356- // biome-ignore format:
356+ // biome-ignore format: keep loop structure
357357 return [
358- // Start counter at 0
359- ...i32 . const ( 0 ) ,
360- ...local . set ( counterIndex ) ,
358+ // Start counter at 0
359+ ...i32 . const ( 0 ) ,
360+ ...local . set ( counterIndex ) ,
361361 ...block . start ( ) ,
362362 ...loop . start ( ) ,
363363 // check if we reached max iterations
@@ -378,9 +378,9 @@ const compileStatement = (
378378 ...loop . br_if ( 1 ) , // break out of outer block
379379
380380 ...body ,
381- ...loop . br ( 0 ) , // loop back
382- ...loop . end ( ) ,
383- ...block . end ( ) ,
381+ ...loop . br ( 0 ) , // loop back
382+ ...loop . end ( ) ,
383+ ...block . end ( ) ,
384384 ] ;
385385 }
386386 case "ForStatement" : {
@@ -406,21 +406,21 @@ const compileStatement = (
406406 exitScope ( ) ;
407407 const isDescending = consumeScratchIndex ( ) ;
408408 const counterIndex = consumeScratchIndex ( ) ;
409- // biome-ignore format:
409+ // biome-ignore format: keep loop structure
410410 return [
411- // Start counter at 0
412- ...i32 . const ( 0 ) ,
413- ...local . set ( counterIndex ) ,
411+ // Start counter at 0
412+ ...i32 . const ( 0 ) ,
413+ ...local . set ( counterIndex ) ,
414414
415- ...init , // e.g. i := 0
416- ...step ,
417- ...fn . call ( func ( ) . unbox_number ) ,
418- ...f64 . const ( 0 ) ,
419- ...f64 . lt ( ) , // step < 0 ?
420- ...local . set ( isDescending ) ,
415+ ...init , // e.g. i := 0
416+ ...step ,
417+ ...fn . call ( func ( ) . unbox_number ) ,
418+ ...f64 . const ( 0 ) ,
419+ ...f64 . lt ( ) , // step < 0 ?
420+ ...local . set ( isDescending ) ,
421421
422- ...block . start ( ) ,
423- ...loop . start ( ) ,
422+ ...block . start ( ) ,
423+ ...loop . start ( ) ,
424424 // check if we reached max iterations
425425 ...local . get ( counterIndex ) ,
426426 ...i32 . const ( MAX_ITERATIONS ) ,
@@ -461,9 +461,9 @@ const compileStatement = (
461461 ...local . set ( loopVar . index ) ,
462462
463463 ...loop . br ( 0 ) , // loop back
464- ...loop . end ( ) ,
465- ...block . end ( ) ,
466- ] ;
464+ ...loop . end ( ) ,
465+ ...block . end ( ) ,
466+ ] ;
467467 }
468468 case "FunctionDeclStatement" : {
469469 const { name, params, body } = stmt ;
@@ -559,18 +559,18 @@ const compileExpression = (
559559 const nativeOp = nativeBinOps [ expr . operator ] ;
560560 // Special handling of + for string concatenation
561561 if ( nativeOp && expr . operator === "+" ) {
562- // biome-ignore format:
562+ // biome-ignore format: keep if structure
563563 return [
564- ...left ,
565- ...fn . call ( func ( ) . is_string ) ,
566- ...right ,
567- ...fn . call ( func ( ) . is_string ) ,
568- ...i32 . or ( ) , // is_string(left) || is_string(right)
569- ...if_ . start ( valType ( "i32" ) ) ,
564+ ...left ,
565+ ...fn . call ( func ( ) . is_string ) ,
566+ ...right ,
567+ ...fn . call ( func ( ) . is_string ) ,
568+ ...i32 . or ( ) , // is_string(left) || is_string(right)
569+ ...if_ . start ( valType ( "i32" ) ) ,
570570 ...left ,
571571 ...right ,
572572 ...fn . call ( func ( ) . concat ) ,
573- ...if_ . else ( ) ,
573+ ...if_ . else ( ) ,
574574 ...left ,
575575 ...fn . call ( func ( ) . is_bool ) ,
576576 ...right ,
@@ -594,7 +594,7 @@ const compileExpression = (
594594 ...fn . call ( func ( ) . box_number ) ,
595595 ...if_ . end ( ) , // bool
596596 ...if_ . end ( ) , // string
597- ] ;
597+ ] ;
598598 }
599599
600600 // Native operators supported for f64
@@ -636,33 +636,33 @@ const compileExpression = (
636636 ] ;
637637 case "and" : {
638638 const scratch = consumeScratchIndex ( ) ;
639- // biome-ignore format:
639+ // biome-ignore format: keep if structure
640640 return [
641- ...left , // evaluate A
642- ...local . set ( scratch ) ,
643- ...local . get ( scratch ) ,
644- ...fn . call ( func ( ) . is_truthy ) ,
645- ...if_ . start ( valType ( "i32" ) ) ,
646- ...right , // evaluate and return B
647- ...if_ . else ( ) ,
648- ...local . get ( scratch ) , // return A
649- ...if_ . end ( ) ,
650- ] ;
641+ ...left , // evaluate A
642+ ...local . set ( scratch ) ,
643+ ...local . get ( scratch ) ,
644+ ...fn . call ( func ( ) . is_truthy ) ,
645+ ...if_ . start ( valType ( "i32" ) ) ,
646+ ...right , // evaluate and return B
647+ ...if_ . else ( ) ,
648+ ...local . get ( scratch ) , // return A
649+ ...if_ . end ( ) ,
650+ ] ;
651651 }
652652 case "or" : {
653653 const scratch = consumeScratchIndex ( ) ;
654- // biome-ignore format:
654+ // biome-ignore format: keep if structure
655655 return [
656- ...left , // evaluate A
657- ...local . set ( scratch ) ,
658- ...local . get ( scratch ) ,
659- ...fn . call ( func ( ) . is_truthy ) ,
660- ...if_ . start ( valType ( "i32" ) ) ,
661- ...local . get ( scratch ) , //return A
662- ...if_ . else ( ) ,
663- ...right , // evaluate and return B
664- ...if_ . end ( ) ,
665- ] ;
656+ ...left , // evaluate A
657+ ...local . set ( scratch ) ,
658+ ...local . get ( scratch ) ,
659+ ...fn . call ( func ( ) . is_truthy ) ,
660+ ...if_ . start ( valType ( "i32" ) ) ,
661+ ...local . get ( scratch ) , //return A
662+ ...if_ . else ( ) ,
663+ ...right , // evaluate and return B
664+ ...if_ . end ( ) ,
665+ ] ;
666666 }
667667 default :
668668 throw new Error ( `Unsupported binary operator: ${ expr . operator } ` ) ;
0 commit comments