From 97ab1f076ac08992d6b64cd77bc0bef97b3fc50a Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Tue, 31 Jul 2018 22:23:07 -0400 Subject: Now implementing array functionality in stdlib instead of the compiler. --- .../default/phase/extension/analysis/common.lux | 37 ---------------------- .../translation/scheme/extension/common.jvm.lux | 29 ----------------- stdlib/source/lux/data/collection/array.lux | 28 ++++++++++++---- stdlib/source/lux/data/number.lux | 12 ++++--- 4 files changed, 28 insertions(+), 78 deletions(-) (limited to 'stdlib/source') diff --git a/stdlib/source/lux/compiler/default/phase/extension/analysis/common.lux b/stdlib/source/lux/compiler/default/phase/extension/analysis/common.lux index 3272f8a29..24f22df3c 100644 --- a/stdlib/source/lux/compiler/default/phase/extension/analysis/common.lux +++ b/stdlib/source/lux/compiler/default/phase/extension/analysis/common.lux @@ -9,7 +9,6 @@ format] [collection ["." list ("list/." Functor)] - ["." array] ["dict" dictionary (#+ Dictionary)]]] [type ["." check]] @@ -203,41 +202,6 @@ (bundle.install "clip" (trinary Text Nat Nat (type (Maybe Text)))) ))) -(def: array::get - Handler - (function (_ extension-name analyse args) - (do ////.Monad - [[var-id varT] (typeA.with-env check.var)] - ((binary (type (Array varT)) Nat (type (Maybe varT)) extension-name) - analyse args)))) - -(def: array::put - Handler - (function (_ extension-name analyse args) - (do ////.Monad - [[var-id varT] (typeA.with-env check.var)] - ((trinary (type (Array varT)) Nat varT (type (Array varT)) extension-name) - analyse args)))) - -(def: array::remove - Handler - (function (_ extension-name analyse args) - (do ////.Monad - [[var-id varT] (typeA.with-env check.var)] - ((binary (type (Array varT)) Nat (type (Array varT)) extension-name) - analyse args)))) - -(def: bundle::array - Bundle - (<| (bundle.prefix "array") - (|> bundle.empty - (bundle.install "new" (unary Nat Array)) - (bundle.install "get" array::get) - (bundle.install "put" array::put) - (bundle.install "remove" array::remove) - (bundle.install "size" (unary (type (Ex [a] (Array a))) Nat)) - ))) - (def: box::new Handler (function (_ extension-name analyse args) @@ -289,7 +253,6 @@ (dict.merge bundle::int) (dict.merge bundle::frac) (dict.merge bundle::text) - (dict.merge bundle::array) (dict.merge bundle::box) (dict.merge bundle::io) ))) diff --git a/stdlib/source/lux/compiler/default/phase/translation/scheme/extension/common.jvm.lux b/stdlib/source/lux/compiler/default/phase/translation/scheme/extension/common.jvm.lux index d1576248d..c46a5e82e 100644 --- a/stdlib/source/lux/compiler/default/phase/translation/scheme/extension/common.jvm.lux +++ b/stdlib/source/lux/compiler/default/phase/translation/scheme/extension/common.jvm.lux @@ -115,34 +115,6 @@ (bundle.install "arithmetic-right-shift" (binary bit::arithmetic-right-shift)) ))) -## [[Arrays]] -(def: (array::new size0) - Unary - (_.make-vector/2 size0 _.nil)) - -(def: (array::get [arrayO idxO]) - Binary - (runtime.array//get arrayO idxO)) - -(def: (array::put [arrayO idxO elemO]) - Trinary - (runtime.array//put arrayO idxO elemO)) - -(def: (array::remove [arrayO idxO]) - Binary - (runtime.array//put arrayO idxO _.nil)) - -(def: bundle::array - Bundle - (<| (bundle.prefix "array") - (|> bundle.empty - (bundle.install "new" (unary array::new)) - (bundle.install "get" (binary array::get)) - (bundle.install "put" (trinary array::put)) - (bundle.install "remove" (binary array::remove)) - (bundle.install "size" (unary _.vector-length/1)) - ))) - ## [[Numbers]] (import: java/lang/Double (#static MIN_VALUE Double) @@ -299,7 +271,6 @@ (dict.merge bundle::int) (dict.merge bundle::frac) (dict.merge bundle::text) - (dict.merge bundle::array) (dict.merge bundle::io) (dict.merge bundle::box) ))) diff --git a/stdlib/source/lux/data/collection/array.lux b/stdlib/source/lux/data/collection/array.lux index 1d64511a9..09fe50412 100644 --- a/stdlib/source/lux/data/collection/array.lux +++ b/stdlib/source/lux/data/collection/array.lux @@ -7,32 +7,46 @@ fold [predicate (#+ Predicate)]] [data + ["." product] [collection - ["." list ("list/." Fold)]] - ["." product]]]) + ["." list ("list/." Fold)]]] + [compiler + ["." host]]]) (def: #export (new size) (All [a] (-> Nat (Array a))) - ("lux array new" size)) + (`` (for {(~~ (static host.jvm)) + (:assume ("jvm anewarray" "(java.lang.Object )" size))}))) (def: #export (size xs) (All [a] (-> (Array a) Nat)) - ("lux array size" xs)) + (`` (for {(~~ (static host.jvm)) + ("jvm arraylength" xs)}))) (def: #export (read i xs) (All [a] (-> Nat (Array a) (Maybe a))) - ("lux array get" xs i)) + (if (n/< (size xs) i) + (`` (for {(~~ (static host.jvm)) + (let [value ("jvm aaload" xs i)] + (if ("jvm object null?" value) + #.None + (#.Some value)))})) + #.None)) (def: #export (write i x xs) (All [a] (-> Nat a (Array a) (Array a))) - ("lux array put" xs i x)) + (`` (for {(~~ (static host.jvm)) + ("jvm aastore" xs i x)}))) (def: #export (delete i xs) (All [a] (-> Nat (Array a) (Array a))) - ("lux array remove" xs i)) + (if (n/< (size xs) i) + (`` (for {(~~ (static host.jvm)) + (write i (:assume ("jvm object null")) xs)})) + xs)) (def: #export (copy length src-start src-array dest-start dest-array) (All [a] diff --git a/stdlib/source/lux/data/number.lux b/stdlib/source/lux/data/number.lux index 583a03b1f..2fc52ae41 100644 --- a/stdlib/source/lux/data/number.lux +++ b/stdlib/source/lux/data/number.lux @@ -12,7 +12,9 @@ [data ["." error (#+ Error)] ["." maybe] - ["." text]] + ["." text] + [collection + ["." array]]] ["." math]] [/ ["." i64]]) @@ -750,15 +752,15 @@ (def: (make-digits _) (-> Any Digits) - ("lux array new" i64.width)) + (array.new i64.width)) (def: (digits-get idx digits) (-> Nat Digits Nat) - (maybe.default 0 ("lux array get" digits idx))) + (|> digits (array.read idx) (maybe.default 0))) -(def: (digits-put idx digit digits) +(def: digits-put (-> Nat Nat Digits Digits) - ("lux array put" digits idx digit)) + array.write) (def: (prepend left right) (-> Text Text Text) -- cgit v1.2.3