Add typed Kotlin Float32 JSON math helpers
ober
89ba85bd4e72c44f3ea3b714d64ee8785f59eec1
--- a/lib/jerboa/typed/checker.ss +++ b/lib/jerboa/typed/checker.ss @@ -431,6 +431,12 @@ (cons 'float32-abs (make-typed-call-sig (list 'Float32) 'Float32 '() 'jvm-float32-abs '())) + (cons 'float32-max + (make-typed-call-sig (list 'Float32 'Float32) 'Float32 '() + 'jvm-float32-max '())) + (cons 'float32-ln + (make-typed-call-sig (list 'Float32) 'Float32 '() + 'jvm-float32-ln '())) (cons 'float32-round->int32 (make-typed-call-sig (list 'Float32) 'Int32 '() 'jvm-float32-round-to-int32 '())) @@ -539,6 +545,9 @@ (cons 'json-object-opt-float32 (make-typed-call-sig (list 'JSONObject 'String) 'Float32 '() 'jvm-json-object-opt-float32 '())) + (cons 'json-object-opt-float32-default + (make-typed-call-sig (list 'JSONObject 'String 'Float32) 'Float32 '() + 'jvm-json-object-opt-float32-default '())) (cons 'json-object-opt-int32-default (make-typed-call-sig (list 'JSONObject 'String 'Int32) 'Int32 '() 'jvm-json-object-opt-int32-default '())) --- a/lib/jerboa/typed/core.ss +++ b/lib/jerboa/typed/core.ss @@ -158,6 +158,7 @@ jvm-json-object-opt-string jvm-json-object-opt-string-default jvm-json-object-opt-float32 + jvm-json-object-opt-float32-default jvm-json-object-opt-int32-default jvm-json-object-opt-json-array jvm-json-object-opt-json-object --- a/lib/jerboa/typed/kotlin/lower.ss +++ b/lib/jerboa/typed/kotlin/lower.ss @@ -247,6 +247,15 @@ (make-kt-call (kt-name1 'Pair) args)] [(jvm-float32-abs) (make-kt-call (make-kt-name '(kotlin math abs)) args)] + [(jvm-float32-max) + (make-kt-call (make-kt-name '(kotlin math max)) args)] + [(jvm-float32-ln) + (make-kt-member-call + (make-kt-call + (make-kt-name '(kotlin math ln)) + (list (make-kt-member-call (car args) 'toDouble '()))) + 'toFloat + '())] [(jvm-float32-round-to-int32) (make-kt-call (make-kt-name '(java lang Math round)) args)] [(string-length) @@ -480,6 +489,16 @@ (make-kt-member-call (car args) 'optDouble (cdr args)) 'toFloat '())] + [(jvm-json-object-opt-float32-default) + (make-kt-member-call + (make-kt-member-call + (car args) + 'optDouble + (list + (cadr args) + (make-kt-member-call (caddr args) 'toDouble '()))) + 'toFloat + '())] [(jvm-json-object-opt-int32-default) (make-kt-member-call (car args) 'optInt (cdr args))] [(jvm-json-object-opt-json-array) --- a/tests/test-typed-checker.ss +++ b/tests/test-typed-checker.ss @@ -756,6 +756,15 @@ (log2 n)))) '(argument-type-mismatch)) +(test "Float32 math helpers accept Float32 operands" + (error-kinds + '(typed-library (body float32-math-ok) + (export f) + (type Float32) + (def (f (a : Float32) (b : Float32)) : Float32 + (float32-ln (float32-max (float32-abs a) b))))) + '()) + (test "float arithmetic (division of casts) types as Float" (error-kinds '(typed-library (body float-div) @@ -927,7 +936,7 @@ (test "json-object primitives accept declared JSON types" (error-kinds '(typed-library (body json-object-ok) - (export make-json read-x) + (export make-json read-x read-x-default) (type JSONObject) (type JSONArray) (type Float32) @@ -943,7 +952,9 @@ (json-object-put-json-array! json "items" items) json)))) (def (read-x (json : JSONObject)) : Float32 - (json-object-opt-float32 json "x")))) + (json-object-opt-float32 json "x")) + (def (read-x-default (json : JSONObject)) : Float32 + (json-object-opt-float32-default json "x" (float32 1.0))))) '()) (test "json-object put rejects non-String key" --- a/tests/test-typed-kotlin.ss +++ b/tests/test-typed-kotlin.ss @@ -419,17 +419,23 @@ (define float32-math-form '(typed-library (sample typed floatmath) - (export near) + (export near spread) (type Float32) (type Int32) (def (near (a : Float32) (b : Float32)) : Bool (<= (float32-abs (- a b)) - (float32 (float32-round->int32 (* (float32 0.25) a))))))) + (float32 (float32-round->int32 (* (float32 0.25) a))))) + (def (spread (a : Float32) (b : Float32)) : Float32 + (float32-ln (float32-max a b))))) (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 max lowers to Kotlin math max" float32-math-kotlin + "kotlin.math.max(a, b)") +(test-contains "Float32 ln lowers through Double" float32-math-kotlin + "kotlin.math.ln(kotlin.math.max(a, b).toDouble()).toFloat()") (test-contains "Float32 round to Int32 lowers to Java Math.round" float32-math-kotlin "java.lang.Math.round((0.25f * a))") @@ -728,7 +734,7 @@ (define json-object-form '(typed-library (sample typed json-object) - (export makeCell readX readCount readItems readMeta readName readNameDefault) + (export makeCell readX readXDefault readCount readItems readMeta readName readNameDefault) (type JSONObject) (type JSONArray) (type Float32) @@ -747,6 +753,8 @@ json))))) (def (readX (json : JSONObject)) : Float32 (json-object-opt-float32 json "x")) + (def (readXDefault (json : JSONObject)) : Float32 + (json-object-opt-float32-default json "x" (float32 1.0))) (def (readCount (json : JSONObject)) : Int32 (json-object-opt-int32-default json "count" (int32 1))) (def (readItems (json : JSONObject)) : (Nullable JSONArray) @@ -781,6 +789,9 @@ (test-contains "JSONObject opt Float32 lowers through optDouble" json-object-kotlin "json.optDouble(\"x\").toFloat()") +(test-contains "JSONObject opt Float32 default lowers through optDouble default" + json-object-kotlin + "json.optDouble(\"x\", 1.0f.toDouble()).toFloat()") (test-contains "JSONObject opt Int32 default lowers to optInt" json-object-kotlin "json.optInt(\"count\", 1)")