diff options
Diffstat (limited to 'stdlib/source/lux/target/jvm/encoding.lux')
-rw-r--r-- | stdlib/source/lux/target/jvm/encoding.lux | 65 |
1 files changed, 36 insertions, 29 deletions
diff --git a/stdlib/source/lux/target/jvm/encoding.lux b/stdlib/source/lux/target/jvm/encoding.lux index 33ac427c2..f5db7a81a 100644 --- a/stdlib/source/lux/target/jvm/encoding.lux +++ b/stdlib/source/lux/target/jvm/encoding.lux @@ -1,5 +1,5 @@ (.module: - [lux #* + [lux (#- nat) [abstract [equivalence (#+ Equivalence)]] [control @@ -9,44 +9,51 @@ ["." i64]] [format ["." binary (#+ Format)]]] + [macro + ["." template]] [type abstract]]) -(template [<bytes> <name> <size> <to> <from> <equivalence>] - [(abstract: #export <name> - {} +(abstract: #export (Unsigned brand) + {} + (I64 Any) - (I64 Any) + (def: #export nat + (-> (Unsigned Any) (I64 Any)) + (|>> :representation)) - (def: #export <size> Nat <bytes>) + (structure: #export equivalence + (All [brand] (Equivalence (Unsigned brand))) + (def: (= reference sample) + ("lux i64 =" (:representation reference) (:representation sample)))) + + (template [<bytes> <name> <size> <constructor> <max>] + [(with-expansions [<raw> (template.identifier [<name> "'"])] + (abstract: #export <raw> {} Any) + (type: #export <name> (Unsigned <raw>))) - (def: #export <to> + (def: #export <size> Nat <bytes>) + + (def: #export <max> + <name> + (|> <bytes> (n/* i64.bits-per-byte) i64.mask :abstraction)) + + (def: #export <constructor> (-> (I64 Any) <name>) - (let [mask (|> <bytes> - (n/* i64.bits-per-byte) - i64.mask)] - (|>> (i64.and mask) :abstraction))) - - (def: #export <from> - (-> <name> (I64 Any)) - (|>> :representation)) - - (structure: #export <equivalence> (Equivalence <name>) - (def: (= reference sample) - ("lux i64 =" (:representation reference) (:representation sample)))) - )] - - [1 U1 u1-bytes to-u1 from-u1 u1-equivalence] - [2 U2 u2-bytes to-u2 from-u2 u2-equivalence] - [4 U4 u4-bytes to-u4 from-u4 u4-equivalence] + (|>> (i64.and (:representation <max>)) :abstraction))] + + [1 U1 u1-bytes u1 max-u1] + [2 U2 u2-bytes u2 max-u2] + [4 U4 u4-bytes u4 max-u4] + ) ) -(template [<name> <type> <format> <pre-write> <post-read>] +(template [<name> <type> <format> <post-read>] [(def: #export <name> (Format <type>) - (binary.adapt <post-read> <pre-write> <format>))] + (binary.adapt <post-read> ..nat <format>))] - [u1-format U1 binary.bits/8 ..from-u1 ..to-u1] - [u2-format U2 binary.bits/16 ..from-u2 ..to-u2] - [u4-format U4 binary.bits/32 ..from-u4 ..to-u4] + [u1-format U1 binary.bits/8 ..u1] + [u2-format U2 binary.bits/16 ..u2] + [u4-format U4 binary.bits/32 ..u4] ) |