Lower typed Float32 math helpers to Kotlin
ober
03750fc9c4bb289943479edcbcb2e8738f4a24b6
--- a/lib/jerboa/typed/checker.ss +++ b/lib/jerboa/typed/checker.ss @@ -406,6 +406,12 @@ (cons 'int->string (make-typed-call-sig (list 'Int) 'String '() 'jvm-to-string '())) + (cons 'float32-abs + (make-typed-call-sig (list 'Float32) 'Float32 '() + 'jvm-float32-abs '())) + (cons 'float32-round->int32 + (make-typed-call-sig (list 'Float32) 'Int32 '() + 'jvm-float32-round-to-int32 '())) (cons 'string-take (make-typed-call-sig (list 'String 'Int32) 'String '() 'jvm-string-take '())) --- a/lib/jerboa/typed/kotlin/lower.ss +++ b/lib/jerboa/typed/kotlin/lower.ss @@ -228,6 +228,10 @@ (make-kt-member-call (car args) 'toLong '()))] [(jvm-to-string) (make-kt-member-call (car args) 'toString '())] + [(jvm-float32-abs) + (make-kt-call (make-kt-name '(kotlin math abs)) args)] + [(jvm-float32-round-to-int32) + (make-kt-call (make-kt-name '(kotlin math roundToInt)) args)] [(string-length) (make-kt-member-call (make-kt-member-get (car args) 'length) --- a/tests/test-typed-kotlin.ss +++ b/tests/test-typed-kotlin.ss @@ -300,6 +300,22 @@ (test-contains "mod lowers to Kotlin remainder operator" duration-kotlin "((safe / 100L) % 10L)") +(define float32-math-form + '(typed-library (sample typed floatmath) + (export near) + (type Float32) + (type Int32) + (def (near (a : Float32) (b : Float32)) : Bool + (<= (float32-abs (- a b)) + (float32 (float32-round->int32 (* (float32 0.25) a))))))) + +(define float32-math-kotlin (typed-library-form->kotlin-string float32-math-form)) + +(test-contains "Float32 abs lowers to Kotlin math abs" float32-math-kotlin + "kotlin.math.abs((a - b))") +(test-contains "Float32 round to Int32 lowers to Kotlin math roundToInt" float32-math-kotlin + "kotlin.math.roundToInt((0.25f * a))") + (define note-form '(typed-library (sample typed notes) (export appendNote)