diff options
Diffstat (limited to 'stdlib/source')
-rw-r--r-- | stdlib/source/lux/data/format/binary.lux | 86 | ||||
-rw-r--r-- | stdlib/source/lux/data/text/encoding.lux | 6 | ||||
-rw-r--r-- | stdlib/source/lux/language/compiler/meta/io.lux | 11 | ||||
-rw-r--r-- | stdlib/source/lux/language/compiler/meta/io/archive.lux | 4 | ||||
-rw-r--r-- | stdlib/source/lux/language/compiler/meta/io/context.lux | 6 | ||||
-rw-r--r-- | stdlib/source/lux/world/binary.lux (renamed from stdlib/source/lux/world/blob.jvm.lux) | 122 | ||||
-rw-r--r-- | stdlib/source/lux/world/file.lux | 8 | ||||
-rw-r--r-- | stdlib/source/lux/world/net/tcp.jvm.lux | 6 | ||||
-rw-r--r-- | stdlib/source/lux/world/net/udp.jvm.lux | 6 |
9 files changed, 124 insertions, 131 deletions
diff --git a/stdlib/source/lux/data/format/binary.lux b/stdlib/source/lux/data/format/binary.lux index 592aba473..27a510b44 100644 --- a/stdlib/source/lux/data/format/binary.lux +++ b/stdlib/source/lux/data/format/binary.lux @@ -12,11 +12,11 @@ ["." encoding] [format (#+ %n)]]] [world - ["." blob (#+ Blob)]]]) + ["." binary (#+ Binary)]]]) ## Exceptions -(exception: #export (blob-was-not-fully-read {length Nat} {read Nat}) - (ex.report ["Blob length" (%n length)] +(exception: #export (binary-was-not-fully-read {length Nat} {read Nat}) + (ex.report ["Binary length" (%n length)] ["Read bytes" (%n read)])) (exception: #export (invalid-tag {range Nat} {byte Nat}) @@ -34,10 +34,10 @@ (def: #export size/64 +8) (type: #export Read - (p.Parser [Offset Blob])) + (p.Parser [Offset Binary])) (type: #export (Write a) - (-> a [Size (-> Offset Blob Blob)])) + (-> a [Size (-> Offset Binary Binary)])) (type: #export (Format a) {#read (Read a) @@ -45,44 +45,44 @@ ## Operators (def: #export (read format input) - (All [a] (-> (Format a) Blob (Error a))) + (All [a] (-> (Format a) Binary (Error a))) (case ((get@ #read format) [+0 input]) (#error.Error msg) (#error.Error msg) (#error.Success [[end _] output]) - (let [length (blob.size input)] + (let [length (binary.size input)] (if (n/= end length) (#error.Success output) - (ex.throw blob-was-not-fully-read [length end]))))) + (ex.throw binary-was-not-fully-read [length end]))))) (def: #export (write format value) - (All [a] (-> (Format a) a Blob)) + (All [a] (-> (Format a) a Binary)) (let [[valueS valueT] ((get@ #write format) value)] - (|> valueS blob.create (valueT +0)))) + (|> valueS binary.create (valueT +0)))) ## Primitives (do-template [<name> <size> <read> <write>] [(def: <name> (Format (I64 Any)) - {#read (function (_ [offset blob]) - (case (<read> offset blob) + {#read (function (_ [offset binary]) + (case (<read> offset binary) (#error.Success data) - (#error.Success [(n/+ <size> offset) blob] data) + (#error.Success [(n/+ <size> offset) binary] data) (#error.Error error) (#error.Error error))) #write (function (_ value) [<size> - (function (_ offset blob) - (|> blob + (function (_ offset binary) + (|> binary (<write> offset value) error.assume))])})] - [bits/8 size/8 blob.read/8 blob.write/8] - [bits/16 size/16 blob.read/16 blob.write/16] - [bits/32 size/32 blob.read/32 blob.write/32] - [bits/64 size/64 blob.read/64 blob.write/64] + [bits/8 size/8 binary.read/8 binary.write/8] + [bits/16 size/16 binary.read/16 binary.write/16] + [bits/32 size/32 binary.read/32 binary.write/32] + [bits/64 size/64 binary.read/64 binary.write/64] ) ## Combinators @@ -99,18 +99,18 @@ (#.Left leftV) (let [[leftS leftT] ((get@ #write leftB) leftV)] [(.inc leftS) - (function (_ offset blob) - (|> blob - (blob.write/8 offset +0) + (function (_ offset binary) + (|> binary + (binary.write/8 offset +0) error.assume (leftT (.inc offset))))]) (#.Right rightV) (let [[rightS rightT] ((get@ #write rightB) rightV)] [(.inc rightS) - (function (_ offset blob) - (|> blob - (blob.write/8 offset +1) + (function (_ offset binary) + (|> binary + (binary.write/8 offset +1) error.assume (rightT (.inc offset))))]) ))}) @@ -142,8 +142,8 @@ (#error.Success [input default])) #write (function (_ value) [+0 - (function (_ offset blob) - blob)])}) + (function (_ offset binary) + binary)])}) (def: #export any (Format Any) @@ -151,12 +151,12 @@ (def: #export bit (Format Bit) - {#read (function (_ [offset blob]) - (case (blob.read/8 offset blob) + {#read (function (_ [offset binary]) + (case (binary.read/8 offset binary) (#error.Success data) (case (: Nat data) (^template [<nat> <bit>] - <nat> (#error.Success [(inc offset) blob] <bit>)) + <nat> (#error.Success [(inc offset) binary] <bit>)) ([+0 #0] [+1 #1]) @@ -167,9 +167,9 @@ (#error.Error error))) #write (function (_ value) [+1 - (function (_ offset blob) - (|> blob - (blob.write/8 offset (if value +1 +0)) + (function (_ offset binary) + (|> binary + (binary.write/8 offset (if value +1 +0)) error.assume))])}) (def: #export nat (Format Nat) (:assume ..bits/64)) @@ -182,27 +182,27 @@ {#read (:: p.Monad<Parser> map number.bits-to-frac read) #write (|>> number.frac-to-bits write)})) -(def: #export blob - (Format Blob) +(def: #export binary + (Format Binary) {#read (do p.Monad<Parser> [size (get@ #read nat)] - (function (_ [offset blob]) + (function (_ [offset binary]) (do error.Monad<Error> [#let [end (n/+ size offset)] - output (blob.slice offset end blob)] - (wrap [[end blob] output])))) + output (binary.slice offset end binary)] + (wrap [[end binary] output])))) #write (function (_ value) - (let [size (blob.size value)] + (let [size (binary.size value)] [(n/+ size/64 size) - (function (_ offset blob) + (function (_ offset binary) (error.assume (do error.Monad<Error> - [_ (blob.write/64 offset size blob)] - (blob.copy size +0 value (n/+ size/64 offset) blob))))]))}) + [_ (binary.write/64 offset size binary)] + (binary.copy size +0 value (n/+ size/64 offset) binary))))]))}) (def: #export text (Format Text) - (let [(^slots [#read #write]) ..blob] + (let [(^slots [#read #write]) ..binary] {#read (do p.Monad<Parser> [utf8 read] (p.lift (encoding.from-utf8 utf8))) diff --git a/stdlib/source/lux/data/text/encoding.lux b/stdlib/source/lux/data/text/encoding.lux index d5246e382..661e0bbf9 100644 --- a/stdlib/source/lux/data/text/encoding.lux +++ b/stdlib/source/lux/data/text/encoding.lux @@ -3,7 +3,7 @@ [data ["." error (#+ Error)]] [world - [blob (#+ Blob)]] + [binary (#+ Binary)]] [language ["_" host]] [host (#+ import:)]]) @@ -16,11 +16,11 @@ (getBytes [String] (Array byte))))})) (def: #export (to-utf8 value) - (-> Text Blob) + (-> Text Binary) (`` (for {(~~ (static _.jvm)) (String::getBytes [..utf8] value)}))) (def: #export (from-utf8 value) - (-> Blob (Error Text)) + (-> Binary (Error Text)) (`` (for {(~~ (static _.jvm)) (#error.Success (String::new [value ..utf8]))}))) diff --git a/stdlib/source/lux/language/compiler/meta/io.lux b/stdlib/source/lux/language/compiler/meta/io.lux index 3ba31fa82..a46f78d5a 100644 --- a/stdlib/source/lux/language/compiler/meta/io.lux +++ b/stdlib/source/lux/language/compiler/meta/io.lux @@ -1,16 +1,9 @@ (.module: [lux (#- Module) - [control - monad - ["ex" exception (#+ exception:)]] [data - [error] - ["." text - format - [encoding]]] + ["." text]] [world - [file (#+ File System)] - [blob (#+ Blob)]]]) + [file (#+ File System)]]]) (type: #export Context File) diff --git a/stdlib/source/lux/language/compiler/meta/io/archive.lux b/stdlib/source/lux/language/compiler/meta/io/archive.lux index 4a6e3fb39..5a7789a95 100644 --- a/stdlib/source/lux/language/compiler/meta/io/archive.lux +++ b/stdlib/source/lux/language/compiler/meta/io/archive.lux @@ -9,7 +9,7 @@ format]] [world ["." file (#+ File System)] - [blob (#+ Blob)]]] + [binary (#+ Binary)]]] ["." /////host] ["." // (#+ Module)]) @@ -57,7 +57,7 @@ (:: System<m> throw cannot-prepare [archive module])))))) (def: #export (write System<m> root content name) - (All [m] (-> (System m) File Blob Text (m Any))) + (All [m] (-> (System m) File Binary Text (m Any))) (:: System<m> write content (..document System<m> root name))) (def: #export (module System<m> root document) diff --git a/stdlib/source/lux/language/compiler/meta/io/context.lux b/stdlib/source/lux/language/compiler/meta/io/context.lux index eab4b284a..8288718aa 100644 --- a/stdlib/source/lux/language/compiler/meta/io/context.lux +++ b/stdlib/source/lux/language/compiler/meta/io/context.lux @@ -10,7 +10,7 @@ ["." encoding]]] [world ["." file (#+ File System)] - [blob (#+ Blob)]]] + [binary (#+ Binary)]]] ["." // (#+ Context Module) [//// ["." host]]]) @@ -85,8 +85,8 @@ (list (find-source' (format host-extension lux-extension)) (find-source' lux-extension)) module-not-found [name]) - blob (:: System<m> read file)] - (case (encoding.from-utf8 blob) + binary (:: System<m> read file)] + (case (encoding.from-utf8 binary) (#error.Success code) (wrap [path code]) diff --git a/stdlib/source/lux/world/blob.jvm.lux b/stdlib/source/lux/world/binary.lux index c4c659ccf..d89b7ae3a 100644 --- a/stdlib/source/lux/world/blob.jvm.lux +++ b/stdlib/source/lux/world/binary.lux @@ -19,7 +19,7 @@ (exception: #export (inverted-range {description Text}) description) -(type: #export Blob (host.type (Array byte))) +(type: #export Binary (host.type (Array byte))) (import: java/lang/System (#static arraycopy [Object int Object int int] #try void)) @@ -41,79 +41,79 @@ (|>> .int host.long-to-byte)) (def: #export (create size) - (-> Nat Blob) + (-> Nat Binary) (host.array byte size)) -(def: #export (read/8 idx blob) - (-> Nat Blob (Error I64)) - (if (n/< (host.array-length blob) idx) - (|> (host.array-read idx blob) ..i64 #error.Success) +(def: #export (read/8 idx binary) + (-> Nat Binary (Error I64)) + (if (n/< (host.array-length binary) idx) + (|> (host.array-read idx binary) ..i64 #error.Success) (ex.throw index-out-of-bounds (%n idx)))) -(def: #export (read/16 idx blob) - (-> Nat Blob (Error I64)) - (if (n/< (host.array-length blob) (n/+ +1 idx)) +(def: #export (read/16 idx binary) + (-> Nat Binary (Error I64)) + (if (n/< (host.array-length binary) (n/+ +1 idx)) (#error.Success ($_ i64.or - (i64.left-shift +8 (..i64 (host.array-read idx blob))) - (..i64 (host.array-read (n/+ +1 idx) blob)))) + (i64.left-shift +8 (..i64 (host.array-read idx binary))) + (..i64 (host.array-read (n/+ +1 idx) binary)))) (ex.throw index-out-of-bounds (%n idx)))) -(def: #export (read/32 idx blob) - (-> Nat Blob (Error I64)) - (if (n/< (host.array-length blob) (n/+ +3 idx)) +(def: #export (read/32 idx binary) + (-> Nat Binary (Error I64)) + (if (n/< (host.array-length binary) (n/+ +3 idx)) (#error.Success ($_ i64.or - (i64.left-shift +24 (..i64 (host.array-read idx blob))) - (i64.left-shift +16 (..i64 (host.array-read (n/+ +1 idx) blob))) - (i64.left-shift +8 (..i64 (host.array-read (n/+ +2 idx) blob))) - (..i64 (host.array-read (n/+ +3 idx) blob)))) + (i64.left-shift +24 (..i64 (host.array-read idx binary))) + (i64.left-shift +16 (..i64 (host.array-read (n/+ +1 idx) binary))) + (i64.left-shift +8 (..i64 (host.array-read (n/+ +2 idx) binary))) + (..i64 (host.array-read (n/+ +3 idx) binary)))) (ex.throw index-out-of-bounds (%n idx)))) -(def: #export (read/64 idx blob) - (-> Nat Blob (Error I64)) - (if (n/< (host.array-length blob) (n/+ +7 idx)) +(def: #export (read/64 idx binary) + (-> Nat Binary (Error I64)) + (if (n/< (host.array-length binary) (n/+ +7 idx)) (#error.Success ($_ i64.or - (i64.left-shift +56 (..i64 (host.array-read idx blob))) - (i64.left-shift +48 (..i64 (host.array-read (n/+ +1 idx) blob))) - (i64.left-shift +40 (..i64 (host.array-read (n/+ +2 idx) blob))) - (i64.left-shift +32 (..i64 (host.array-read (n/+ +3 idx) blob))) - (i64.left-shift +24 (..i64 (host.array-read (n/+ +4 idx) blob))) - (i64.left-shift +16 (..i64 (host.array-read (n/+ +5 idx) blob))) - (i64.left-shift +8 (..i64 (host.array-read (n/+ +6 idx) blob))) - (..i64 (host.array-read (n/+ +7 idx) blob)))) + (i64.left-shift +56 (..i64 (host.array-read idx binary))) + (i64.left-shift +48 (..i64 (host.array-read (n/+ +1 idx) binary))) + (i64.left-shift +40 (..i64 (host.array-read (n/+ +2 idx) binary))) + (i64.left-shift +32 (..i64 (host.array-read (n/+ +3 idx) binary))) + (i64.left-shift +24 (..i64 (host.array-read (n/+ +4 idx) binary))) + (i64.left-shift +16 (..i64 (host.array-read (n/+ +5 idx) binary))) + (i64.left-shift +8 (..i64 (host.array-read (n/+ +6 idx) binary))) + (..i64 (host.array-read (n/+ +7 idx) binary)))) (ex.throw index-out-of-bounds (%n idx)))) -(def: #export (write/8 idx value blob) - (-> Nat (I64 Any) Blob (Error Blob)) - (if (n/< (host.array-length blob) idx) - (exec (|> blob +(def: #export (write/8 idx value binary) + (-> Nat (I64 Any) Binary (Error Binary)) + (if (n/< (host.array-length binary) idx) + (exec (|> binary (host.array-write idx (..byte value))) - (#error.Success blob)) + (#error.Success binary)) (ex.throw index-out-of-bounds (%n idx)))) -(def: #export (write/16 idx value blob) - (-> Nat (I64 Any) Blob (Error Blob)) - (if (n/< (host.array-length blob) (n/+ +1 idx)) - (exec (|> blob +(def: #export (write/16 idx value binary) + (-> Nat (I64 Any) Binary (Error Binary)) + (if (n/< (host.array-length binary) (n/+ +1 idx)) + (exec (|> binary (host.array-write idx (..byte (i64.logical-right-shift +8 value))) (host.array-write (n/+ +1 idx) (..byte value))) - (#error.Success blob)) + (#error.Success binary)) (ex.throw index-out-of-bounds (%n idx)))) -(def: #export (write/32 idx value blob) - (-> Nat (I64 Any) Blob (Error Blob)) - (if (n/< (host.array-length blob) (n/+ +3 idx)) - (exec (|> blob +(def: #export (write/32 idx value binary) + (-> Nat (I64 Any) Binary (Error Binary)) + (if (n/< (host.array-length binary) (n/+ +3 idx)) + (exec (|> binary (host.array-write idx (..byte (i64.logical-right-shift +24 value))) (host.array-write (n/+ +1 idx) (..byte (i64.logical-right-shift +16 value))) (host.array-write (n/+ +2 idx) (..byte (i64.logical-right-shift +8 value))) (host.array-write (n/+ +3 idx) (..byte value))) - (#error.Success blob)) + (#error.Success binary)) (ex.throw index-out-of-bounds (%n idx)))) -(def: #export (write/64 idx value blob) - (-> Nat (I64 Any) Blob (Error Blob)) - (if (n/< (host.array-length blob) (n/+ +7 idx)) - (exec (|> blob +(def: #export (write/64 idx value binary) + (-> Nat (I64 Any) Binary (Error Binary)) + (if (n/< (host.array-length binary) (n/+ +7 idx)) + (exec (|> binary (host.array-write idx (..byte (i64.logical-right-shift +56 value))) (host.array-write (n/+ +1 idx) (..byte (i64.logical-right-shift +48 value))) (host.array-write (n/+ +2 idx) (..byte (i64.logical-right-shift +40 value))) @@ -122,17 +122,17 @@ (host.array-write (n/+ +5 idx) (..byte (i64.logical-right-shift +16 value))) (host.array-write (n/+ +6 idx) (..byte (i64.logical-right-shift +8 value))) (host.array-write (n/+ +7 idx) (..byte value))) - (#error.Success blob)) + (#error.Success binary)) (ex.throw index-out-of-bounds (%n idx)))) -(def: #export (size blob) - (-> Blob Nat) - (host.array-length blob)) +(def: #export (size binary) + (-> Binary Nat) + (host.array-length binary)) -(def: #export (slice from to blob) - (-> Nat Nat Blob (Error Blob)) +(def: #export (slice from to binary) + (-> Nat Nat Binary (Error Binary)) (with-expansions [<description> (as-is (format "from = " (%n from) " | " "to = " (%n to)))] - (let [size (host.array-length blob)] + (let [size (host.array-length binary)] (cond (not (n/<= to from)) (ex.throw inverted-range <description>) @@ -141,18 +141,18 @@ (ex.throw index-out-of-bounds <description>) ## else - (#error.Success (Arrays::copyOfRange [blob (:coerce Int from) (:coerce Int (inc to))])))))) + (#error.Success (Arrays::copyOfRange [binary (:coerce Int from) (:coerce Int (inc to))])))))) -(def: #export (slice' from blob) - (-> Nat Blob (Error Blob)) - (slice from (dec (host.array-length blob)) blob)) +(def: #export (slice' from binary) + (-> Nat Binary (Error Binary)) + (slice from (dec (host.array-length binary)) binary)) -(structure: #export _ (eq.Equivalence Blob) +(structure: #export _ (eq.Equivalence Binary) (def: (= reference sample) (Arrays::equals [reference sample]))) (def: #export (copy bytes source-offset source target-offset target) - (-> Nat Nat Blob Nat Blob (Error Blob)) + (-> Nat Nat Binary Nat Binary (Error Binary)) (do error.Monad<Error> [_ (System::arraycopy [source (.int source-offset) target (.int target-offset) (.int bytes)])] (wrap target))) diff --git a/stdlib/source/lux/world/file.lux b/stdlib/source/lux/world/file.lux index acaa2c244..5c359f26b 100644 --- a/stdlib/source/lux/world/file.lux +++ b/stdlib/source/lux/world/file.lux @@ -14,7 +14,7 @@ ["." instant (#+ Instant)] ["." duration]] [world - ["." blob (#+ Blob)]] + ["." binary (#+ Binary)]] ["." io (#+ Process)] [host (#+ import:)] ["." language/host]]) @@ -40,7 +40,7 @@ lift) (do-template [<name>] - [(: (-> Blob File (m Any)) + [(: (-> Binary File (m Any)) <name>)] [append] [write]) @@ -49,7 +49,7 @@ [(: (-> File (m <output>)) <name>)] - [read Blob] + [read Binary] [size Nat] [files (List File)] [last-modified Instant]) @@ -164,7 +164,7 @@ (do io.Monad<Process> [#let [file' (java/io/File::new file)] size (java/io/File::length [] file') - #let [data (blob.create (.nat size))] + #let [data (binary.create (.nat size))] stream (FileInputStream::new [file']) bytes-read (InputStream::read [data] stream) _ (AutoCloseable::close [] stream)] diff --git a/stdlib/source/lux/world/net/tcp.jvm.lux b/stdlib/source/lux/world/net/tcp.jvm.lux index 6c65a92ca..ee866203e 100644 --- a/stdlib/source/lux/world/net/tcp.jvm.lux +++ b/stdlib/source/lux/world/net/tcp.jvm.lux @@ -11,7 +11,7 @@ [type abstract] [world - [blob (#+ Blob)]] + [binary (#+ Binary)]] ["." io (#+ Process)] [host (#+ import:)]] ["." //]) @@ -47,7 +47,7 @@ #out OutputStream} (def: #export (read data offset length self) - (-> Blob Nat Nat TCP (Task Nat)) + (-> Binary Nat Nat TCP (Task Nat)) (promise.future (do io.Monad<Process> [bytes-read (InputStream::read [data (.int offset) (.int length)] @@ -55,7 +55,7 @@ (wrap (.nat bytes-read))))) (def: #export (write data offset length self) - (-> Blob Nat Nat TCP (Task Any)) + (-> Binary Nat Nat TCP (Task Any)) (let [out (get@ #out (:representation self))] (promise.future (do io.Monad<Process> diff --git a/stdlib/source/lux/world/net/udp.jvm.lux b/stdlib/source/lux/world/net/udp.jvm.lux index 82614842c..6974af0ba 100644 --- a/stdlib/source/lux/world/net/udp.jvm.lux +++ b/stdlib/source/lux/world/net/udp.jvm.lux @@ -14,7 +14,7 @@ [type abstract] [world - [blob (#+ Blob)]] + [binary (#+ Binary)]] ["." io] [host (#+ import:)]] ["." //]) @@ -66,7 +66,7 @@ {#socket DatagramSocket} (def: #export (read data offset length self) - (-> Blob Nat Nat UDP (T.Task [Nat //.Address //.Port])) + (-> Binary Nat Nat UDP (T.Task [Nat //.Address //.Port])) (let [(^open ".") (:representation self) packet (DatagramPacket::new|receive [data (.int offset) (.int length)])] (P.future @@ -78,7 +78,7 @@ (.nat (DatagramPacket::getPort [] packet))]))))) (def: #export (write address port data offset length self) - (-> //.Address //.Port Blob Nat Nat UDP (T.Task Any)) + (-> //.Address //.Port Binary Nat Nat UDP (T.Task Any)) (P.future (do (e.ErrorT io.Monad<IO>) [address (resolve address) |