diff options
Diffstat (limited to 'stdlib/source/library/lux/meta/compiler/target/jvm/encoding')
3 files changed, 276 insertions, 0 deletions
diff --git a/stdlib/source/library/lux/meta/compiler/target/jvm/encoding/name.lux b/stdlib/source/library/lux/meta/compiler/target/jvm/encoding/name.lux new file mode 100644 index 000000000..108ad8752 --- /dev/null +++ b/stdlib/source/library/lux/meta/compiler/target/jvm/encoding/name.lux @@ -0,0 +1,42 @@ +(.require + [library + [lux (.except) + [data + ["[0]" text (.only) + ["%" \\format (.only format)]]] + [meta + [type + ["[0]" nominal (.except def)]]]]]) + +(def .public internal_separator "/") +(def .public external_separator ".") + +(type .public External + Text) + +(nominal.def .public Internal + Text + + (def .public internal + (-> External Internal) + (|>> (text.replaced ..external_separator + ..internal_separator) + abstraction)) + + (def .public read + (-> Internal Text) + (|>> representation)) + + (def .public external + (-> Internal External) + (|>> representation + (text.replaced ..internal_separator + ..external_separator)))) + +(def .public safe + (-> Text External) + (|>> ..internal ..external)) + +(def .public (qualify package class) + (-> Text External External) + (format (..safe package) ..external_separator class)) diff --git a/stdlib/source/library/lux/meta/compiler/target/jvm/encoding/signed.lux b/stdlib/source/library/lux/meta/compiler/target/jvm/encoding/signed.lux new file mode 100644 index 000000000..428f5ef8a --- /dev/null +++ b/stdlib/source/library/lux/meta/compiler/target/jvm/encoding/signed.lux @@ -0,0 +1,114 @@ +(.require + [library + [lux (.except int) + [abstract + [equivalence (.only Equivalence)] + [order (.only Order)]] + [control + ["[0]" try (.only Try)] + ["[0]" exception (.only Exception)]] + [data + [text + ["%" \\format (.only format)]] + [binary + ["[0]" \\format (.only Format)]]] + [math + [number + ["[0]" i64] + ["n" nat] + ["i" int]]] + [meta + [macro + ["[0]" template]] + [type + ["[0]" nominal (.except def)]]]]]) + +(nominal.def .public (Signed brand) + Int + + (def .public value + (-> (Signed Any) Int) + (|>> representation)) + + (def .public equivalence + (All (_ brand) (Equivalence (Signed brand))) + (implementation + (def (= reference sample) + (i.= (representation reference) (representation sample))))) + + (def .public order + (All (_ brand) (Order (Signed brand))) + (implementation + (def equivalence ..equivalence) + (def (< reference sample) + (i.< (representation reference) (representation sample))))) + + (exception.def .public (value_exceeds_the_scope [value scope]) + (Exception [Int Nat]) + (exception.report + (list ["Value" (%.int value)] + ["Scope (in bytes)" (%.nat scope)]))) + + (with_template [<bytes> <name> <size> <constructor> <maximum> <minimum> <+> <->] + [(with_expansions [<raw> (template.symbol [<name> "'"])] + (nominal.def <raw> Any) + (type .public <name> (Signed <raw>))) + + (def .public <size> <bytes>) + + (def .public <maximum> + <name> + (|> <bytes> (n.* i64.bits_per_byte) -- i64.mask abstraction)) + + (def .public <minimum> + <name> + (let [it (representation <maximum>)] + (abstraction (-- (i.- it +0))))) + + (def .public <constructor> + (-> Int (Try <name>)) + (let [positive (representation <maximum>) + negative (i64.not positive)] + (function (_ value) + (if (i.= (if (i.< +0 value) + (i64.or negative value) + (i64.and positive value)) + value) + {try.#Success (abstraction value)} + (exception.except ..value_exceeds_the_scope [value <size>]))))) + + (with_template [<abstract_operation> <concrete_operation>] + [(def .public (<abstract_operation> parameter subject) + (-> <name> <name> (Try <name>)) + (<constructor> + (<concrete_operation> (representation parameter) + (representation subject))))] + + [<+> i.+] + [<-> i.-] + )] + + [1 S1 bytes/1 s1 maximum/1 minimum/1 +/1 -/1] + [2 S2 bytes/2 s2 maximum/2 minimum/2 +/2 -/2] + [4 S4 bytes/4 s4 maximum/4 minimum/4 +/4 -/4] + ) + + (with_template [<name> <from> <to>] + [(def .public <name> + (-> <from> <to>) + (|>> transmutation))] + + [lifted/2 S1 S2] + [lifted/4 S2 S4] + ) + + (with_template [<format_name> <type> <format>] + [(def .public <format_name> + (Format <type>) + (|>> representation <format>))] + + [format/1 S1 \\format.bits_8] + [format/2 S2 \\format.bits_16] + [format/4 S4 \\format.bits_32] + ) + ) diff --git a/stdlib/source/library/lux/meta/compiler/target/jvm/encoding/unsigned.lux b/stdlib/source/library/lux/meta/compiler/target/jvm/encoding/unsigned.lux new file mode 100644 index 000000000..afd21a166 --- /dev/null +++ b/stdlib/source/library/lux/meta/compiler/target/jvm/encoding/unsigned.lux @@ -0,0 +1,120 @@ +(.require + [library + [lux (.except nat) + [abstract + [equivalence (.only Equivalence)] + [order (.only Order)]] + [control + ["[0]" try (.only Try)] + ["[0]" exception (.only Exception)]] + [data + [text + ["%" \\format (.only format)]] + [binary + ["[0]" \\format (.only Format)]]] + [math + [number + ["n" nat] + ["[0]" i64]]] + [meta + [macro + ["[0]" template]] + [type + ["[0]" nominal (.except def)]]]]]) + +(nominal.def .public (Unsigned brand) + Nat + + (def .public value + (-> (Unsigned Any) Nat) + (|>> representation)) + + (def .public equivalence + (All (_ brand) (Equivalence (Unsigned brand))) + (implementation + (def (= reference sample) + (n.= (representation reference) + (representation sample))))) + + (def .public order + (All (_ brand) (Order (Unsigned brand))) + (implementation + (def equivalence ..equivalence) + (def (< reference sample) + (n.< (representation reference) + (representation sample))))) + + (exception.def .public (value_exceeds_the_maximum [type value maximum]) + (Exception [Symbol Nat (Unsigned Any)]) + (exception.report + (list ["Type" (%.symbol type)] + ["Value" (%.nat value)] + ["Maximum" (%.nat (representation maximum))]))) + + (exception.def .public (subtraction_cannot_yield_negative_value [type parameter subject]) + (All (_ brand) (Exception [Symbol (Unsigned brand) (Unsigned brand)])) + (exception.report + (list ["Type" (%.symbol type)] + ["Parameter" (%.nat (representation parameter))] + ["Subject" (%.nat (representation subject))]))) + + (with_template [<bytes> <name> <size> <constructor> <maximum> <+> <-> <max>] + [(with_expansions [<raw> (template.symbol [<name> "'"])] + (nominal.def .public <raw> Any) + (type .public <name> (Unsigned <raw>))) + + (def .public <size> <bytes>) + + (def .public <maximum> + <name> + (|> <bytes> (n.* i64.bits_per_byte) i64.mask abstraction)) + + (def .public (<constructor> value) + (-> Nat (Try <name>)) + (if (n.> (representation <maximum>) value) + (exception.except ..value_exceeds_the_maximum [(symbol <name>) value <maximum>]) + {try.#Success (abstraction value)})) + + (def .public (<+> parameter subject) + (-> <name> <name> (Try <name>)) + (<constructor> + (n.+ (representation parameter) + (representation subject)))) + + (def .public (<-> parameter subject) + (-> <name> <name> (Try <name>)) + (let [parameter' (representation parameter) + subject' (representation subject)] + (if (n.> subject' parameter') + (exception.except ..subtraction_cannot_yield_negative_value [(symbol <name>) parameter subject]) + {try.#Success (abstraction (n.- parameter' subject'))}))) + + (def .public (<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] + ) + + (with_template [<name> <from> <to>] + [(def .public <name> + (-> <from> <to>) + (|>> transmutation))] + + [lifted/2 U1 U2] + [lifted/4 U2 U4] + ) + + (with_template [<format_name> <type> <format>] + [(def .public <format_name> + (Format <type>) + (|>> representation <format>))] + + [format/1 U1 \\format.bits_8] + [format/2 U2 \\format.bits_16] + [format/4 U4 \\format.bits_32] + ) + ) |