@@ -347,15 +347,37 @@ export class BuilderGenerator
347347
348348 const type = expr . operator . type ;
349349
350- const boolised = i64 . eqz ( wasm . call ( BOOLISE_FX ) . args ( left ) ) ;
351-
350+ // not a wasm function as it needs to short-circuit
352351 if ( type === TokenType . AND ) {
353- return wasm . raw `(if (result i32 i64) ${ boolised } (then ${ left } ) (else ${ right } ))` ;
352+ // if x is false, then x else y
353+ return wasm
354+ . if ( i64 . eqz ( wasm . call ( BOOLISE_FX ) . args ( left ) ) )
355+ . results ( i32 , i64 )
356+ . then ( left )
357+ . else ( right ) as unknown as WasmNumeric ; // these WILL return WasmNumeric
354358 } else if ( type === TokenType . OR ) {
355- return wasm . raw `(if (result i32 i64) ${ boolised } (then ${ right } ) (else ${ left } ))` ;
359+ // if x is false, then y else x
360+ return wasm
361+ . if ( i64 . eqz ( wasm . call ( BOOLISE_FX ) . args ( left ) ) )
362+ . results ( i32 , i64 )
363+ . then ( right )
364+ . else ( left ) as unknown as WasmNumeric ;
356365 } else throw new Error ( `Unsupported boolean binary operator: ${ type } ` ) ;
357366 }
358367
368+ visitTernaryExpr ( expr : ExprNS . Ternary ) : WasmNumeric {
369+ const consequent = this . visit ( expr . consequent ) ;
370+ const alternative = this . visit ( expr . alternative ) ;
371+
372+ const predicate = this . visit ( expr . predicate ) ;
373+
374+ return wasm
375+ . if ( i32 . wrap_i64 ( wasm . call ( BOOLISE_FX ) . args ( predicate ) ) )
376+ . results ( i32 , i64 )
377+ . then ( consequent )
378+ . else ( alternative ) as unknown as WasmNumeric ;
379+ }
380+
359381 visitNoneExpr ( expr : ExprNS . None ) : WasmNumeric {
360382 return wasm . call ( MAKE_NONE_FX ) ;
361383 }
@@ -511,9 +533,6 @@ ${args.map(
511533 }
512534
513535 // UNIMPLEMENTED PYTHON CONSTRUCTS
514- visitTernaryExpr ( expr : ExprNS . Ternary ) : WasmNumeric {
515- throw new Error ( "Method not implemented." ) ;
516- }
517536 visitLambdaExpr ( expr : ExprNS . Lambda ) : WasmNumeric {
518537 throw new Error ( "Method not implemented." ) ;
519538 }
0 commit comments