aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/target/jvm/encoding/unsigned.lux
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib/source/lux/target/jvm/encoding/unsigned.lux')
-rw-r--r--stdlib/source/lux/target/jvm/encoding/unsigned.lux59
1 files changed, 59 insertions, 0 deletions
diff --git a/stdlib/source/lux/target/jvm/encoding/unsigned.lux b/stdlib/source/lux/target/jvm/encoding/unsigned.lux
new file mode 100644
index 000000000..f5db7a81a
--- /dev/null
+++ b/stdlib/source/lux/target/jvm/encoding/unsigned.lux
@@ -0,0 +1,59 @@
+(.module:
+ [lux (#- nat)
+ [abstract
+ [equivalence (#+ Equivalence)]]
+ [control
+ ["." parser ("#;." functor)]]
+ [data
+ [number
+ ["." i64]]
+ [format
+ ["." binary (#+ Format)]]]
+ [macro
+ ["." template]]
+ [type
+ abstract]])
+
+(abstract: #export (Unsigned brand)
+ {}
+ (I64 Any)
+
+ (def: #export nat
+ (-> (Unsigned Any) (I64 Any))
+ (|>> :representation))
+
+ (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 <size> Nat <bytes>)
+
+ (def: #export <max>
+ <name>
+ (|> <bytes> (n/* i64.bits-per-byte) i64.mask :abstraction))
+
+ (def: #export <constructor>
+ (-> (I64 Any) <name>)
+ (|>> (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> <post-read>]
+ [(def: #export <name>
+ (Format <type>)
+ (binary.adapt <post-read> ..nat <format>))]
+
+ [u1-format U1 binary.bits/8 ..u1]
+ [u2-format U2 binary.bits/16 ..u2]
+ [u4-format U4 binary.bits/32 ..u4]
+ )