aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/target/jvm/encoding
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib/source/library/lux/target/jvm/encoding')
-rw-r--r--stdlib/source/library/lux/target/jvm/encoding/name.lux40
-rw-r--r--stdlib/source/library/lux/target/jvm/encoding/signed.lux107
-rw-r--r--stdlib/source/library/lux/target/jvm/encoding/unsigned.lux121
3 files changed, 268 insertions, 0 deletions
diff --git a/stdlib/source/library/lux/target/jvm/encoding/name.lux b/stdlib/source/library/lux/target/jvm/encoding/name.lux
new file mode 100644
index 000000000..5a1982d3e
--- /dev/null
+++ b/stdlib/source/library/lux/target/jvm/encoding/name.lux
@@ -0,0 +1,40 @@
+(.module:
+ [library
+ [lux #*
+ [data
+ ["." text
+ ["%" format (#+ format)]]]
+ [type
+ abstract]]])
+
+(def: #export internal_separator "/")
+(def: #export external_separator ".")
+
+(type: #export External Text)
+
+(abstract: #export Internal
+ Text
+
+ (def: #export internal
+ (-> External Internal)
+ (|>> (text.replace_all ..external_separator
+ ..internal_separator)
+ :abstraction))
+
+ (def: #export read
+ (-> Internal Text)
+ (|>> :representation))
+
+ (def: #export external
+ (-> Internal External)
+ (|>> :representation
+ (text.replace_all ..internal_separator
+ ..external_separator))))
+
+(def: #export sanitize
+ (-> Text External)
+ (|>> ..internal ..external))
+
+(def: #export (qualify package class)
+ (-> Text External External)
+ (format (..sanitize package) ..external_separator class))
diff --git a/stdlib/source/library/lux/target/jvm/encoding/signed.lux b/stdlib/source/library/lux/target/jvm/encoding/signed.lux
new file mode 100644
index 000000000..a914dfc3c
--- /dev/null
+++ b/stdlib/source/library/lux/target/jvm/encoding/signed.lux
@@ -0,0 +1,107 @@
+(.module:
+ [library
+ [lux (#- int)
+ [abstract
+ [equivalence (#+ Equivalence)]
+ [order (#+ Order)]]
+ [control
+ ["." try (#+ Try)]
+ ["." exception (#+ exception:)]]
+ [data
+ [text
+ ["%" format (#+ format)]]
+ ["." format #_
+ ["#" binary (#+ Writer)]]]
+ [macro
+ ["." template]]
+ [math
+ [number
+ ["." i64]
+ ["n" nat]
+ ["i" int]]]
+ [type
+ abstract]]])
+
+(abstract: #export (Signed brand)
+ Int
+
+ (def: #export value
+ (-> (Signed Any) Int)
+ (|>> :representation))
+
+ (implementation: #export equivalence
+ (All [brand] (Equivalence (Signed brand)))
+ (def: (= reference sample)
+ (i.= (:representation reference) (:representation sample))))
+
+ (implementation: #export order
+ (All [brand] (Order (Signed brand)))
+
+ (def: &equivalence ..equivalence)
+ (def: (< reference sample)
+ (i.< (:representation reference) (:representation sample))))
+
+ (exception: #export (value_exceeds_the_scope {value Int}
+ {scope Nat})
+ (exception.report
+ ["Value" (%.int value)]
+ ["Scope (in bytes)" (%.nat scope)]))
+
+ (template [<bytes> <name> <size> <constructor> <maximum> <+> <->]
+ [(with_expansions [<raw> (template.identifier [<name> "'"])]
+ (abstract: #export <raw> Any)
+ (type: #export <name> (Signed <raw>)))
+
+ (def: #export <size> <bytes>)
+
+ (def: #export <maximum>
+ <name>
+ (|> <bytes> (n.* i64.bits_per_byte) dec i64.mask :abstraction))
+
+ (def: #export <constructor>
+ (-> Int (Try <name>))
+ (let [positive (|> <bytes> (n.* i64.bits_per_byte) i64.mask)
+ negative (|> positive .int (i.right_shift 1) i64.not)]
+ (function (_ value)
+ (if (i.= (if (i.< +0 value)
+ (i64.or negative value)
+ (i64.and positive value))
+ value)
+ (#try.Success (:abstraction value))
+ (exception.throw ..value_exceeds_the_scope [value <size>])))))
+
+ (template [<abstract_operation> <concrete_operation>]
+ [(def: #export (<abstract_operation> parameter subject)
+ (-> <name> <name> (Try <name>))
+ (<constructor>
+ (<concrete_operation> (:representation parameter)
+ (:representation subject))))]
+
+ [<+> i.+]
+ [<-> i.-]
+ )]
+
+ [1 S1 bytes/1 s1 maximum/1 +/1 -/1]
+ [2 S2 bytes/2 s2 maximum/2 +/2 -/2]
+ [4 S4 bytes/4 s4 maximum/4 +/4 -/4]
+ )
+
+ (template [<name> <from> <to>]
+ [(def: #export <name>
+ (-> <from> <to>)
+ (|>> :transmutation))]
+
+ [lift/2 S1 S2]
+ [lift/4 S2 S4]
+ )
+
+ (template [<writer_name> <type> <writer>]
+ [(def: #export <writer_name>
+ (Writer <type>)
+ (|>> :representation <writer>))]
+
+ [writer/1 S1 format.bits/8]
+ [writer/2 S2 format.bits/16]
+ [writer/4 S4 format.bits/32]
+ )
+ )
diff --git a/stdlib/source/library/lux/target/jvm/encoding/unsigned.lux b/stdlib/source/library/lux/target/jvm/encoding/unsigned.lux
new file mode 100644
index 000000000..d8299fa65
--- /dev/null
+++ b/stdlib/source/library/lux/target/jvm/encoding/unsigned.lux
@@ -0,0 +1,121 @@
+(.module:
+ [library
+ [lux (#- nat)
+ [abstract
+ [equivalence (#+ Equivalence)]
+ [order (#+ Order)]]
+ [control
+ ["." try (#+ Try)]
+ ["." exception (#+ exception:)]]
+ [data
+ [text
+ ["%" format (#+ format)]]
+ ["." format #_
+ ["#" binary (#+ Writer)]]]
+ [macro
+ ["." template]]
+ [math
+ [number
+ ["n" nat]
+ ["." i64]]]
+ [type
+ abstract]]])
+
+(abstract: #export (Unsigned brand)
+ Nat
+
+ (def: #export value
+ (-> (Unsigned Any) Nat)
+ (|>> :representation))
+
+ (implementation: #export equivalence
+ (All [brand] (Equivalence (Unsigned brand)))
+ (def: (= reference sample)
+ (n.= (:representation reference)
+ (:representation sample))))
+
+ (implementation: #export order
+ (All [brand] (Order (Unsigned brand)))
+
+ (def: &equivalence ..equivalence)
+ (def: (< reference sample)
+ (n.< (:representation reference)
+ (:representation sample))))
+
+ (exception: #export (value_exceeds_the_maximum {type Name}
+ {value Nat}
+ {maximum (Unsigned Any)})
+ (exception.report
+ ["Type" (%.name type)]
+ ["Value" (%.nat value)]
+ ["Maximum" (%.nat (:representation maximum))]))
+
+ (exception: #export [brand] (subtraction_cannot_yield_negative_value
+ {type Name}
+ {parameter (Unsigned brand)}
+ {subject (Unsigned brand)})
+ (exception.report
+ ["Type" (%.name type)]
+ ["Parameter" (%.nat (:representation parameter))]
+ ["Subject" (%.nat (:representation subject))]))
+
+ (template [<bytes> <name> <size> <constructor> <maximum> <+> <-> <max>]
+ [(with_expansions [<raw> (template.identifier [<name> "'"])]
+ (abstract: #export <raw> Any)
+ (type: #export <name> (Unsigned <raw>)))
+
+ (def: #export <size> <bytes>)
+
+ (def: #export <maximum>
+ <name>
+ (|> <bytes> (n.* i64.bits_per_byte) i64.mask :abstraction))
+
+ (def: #export (<constructor> value)
+ (-> Nat (Try <name>))
+ (if (n.<= (:representation <maximum>) value)
+ (#try.Success (:abstraction value))
+ (exception.throw ..value_exceeds_the_maximum [(name_of <name>) value <maximum>])))
+
+ (def: #export (<+> parameter subject)
+ (-> <name> <name> (Try <name>))
+ (<constructor>
+ (n.+ (:representation parameter)
+ (:representation subject))))
+
+ (def: #export (<-> parameter subject)
+ (-> <name> <name> (Try <name>))
+ (let [parameter' (:representation parameter)
+ subject' (:representation subject)]
+ (if (n.<= subject' parameter')
+ (#try.Success (:abstraction (n.- parameter' subject')))
+ (exception.throw ..subtraction_cannot_yield_negative_value [(name_of <name>) parameter subject]))))
+
+ (def: #export (<max> left right)
+ (-> <name> <name> <name>)
+ (:abstraction (n.max (:representation left)
+ (:representation right))))]
+
+ [1 U1 bytes/1 u1 maximum/1 +/1 -/1 max/1]
+ [2 U2 bytes/2 u2 maximum/2 +/2 -/2 max/2]
+ [4 U4 bytes/4 u4 maximum/4 +/4 -/4 max/4]
+ )
+
+ (template [<name> <from> <to>]
+ [(def: #export <name>
+ (-> <from> <to>)
+ (|>> :transmutation))]
+
+ [lift/2 U1 U2]
+ [lift/4 U2 U4]
+ )
+
+ (template [<writer_name> <type> <writer>]
+ [(def: #export <writer_name>
+ (Writer <type>)
+ (|>> :representation <writer>))]
+
+ [writer/1 U1 format.bits/8]
+ [writer/2 U2 format.bits/16]
+ [writer/4 U4 format.bits/32]
+ )
+ )