aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/host/jvm/encoding.lux
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--stdlib/source/lux/host/jvm/encoding.lux49
1 files changed, 49 insertions, 0 deletions
diff --git a/stdlib/source/lux/host/jvm/encoding.lux b/stdlib/source/lux/host/jvm/encoding.lux
new file mode 100644
index 000000000..6d8afe348
--- /dev/null
+++ b/stdlib/source/lux/host/jvm/encoding.lux
@@ -0,0 +1,49 @@
+(.module:
+ [lux #*
+ [control
+ [equivalence (#+ Equivalence)]
+ [parser ("parser/." Functor<Parser>)]]
+ [data
+ [number
+ ["." i64]]
+ [format
+ ["." binary (#+ Format)]]]
+ [type
+ abstract]])
+
+(do-template [<name> <bytes> <to> <from>]
+ [(abstract: #export <name>
+ {}
+
+ (I64 Any)
+
+ (def: #export <to>
+ (-> (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 <name>)
+ (def: (= reference sample)
+ ("lux i64 =" (:representation reference) (:representation sample))))
+ )]
+
+ [U1 1 to-u1 from-u1]
+ [U2 2 to-u2 from-u2]
+ [U4 4 to-u4 from-u4]
+ )
+
+(do-template [<name> <type> <format> <pre-write> <post-read>]
+ [(def: #export <name>
+ (Format <type>)
+ (binary.adapt <post-read> <pre-write> <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]
+ )