aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source
diff options
context:
space:
mode:
authorEduardo Julian2021-02-16 02:08:55 -0400
committerEduardo Julian2021-02-16 02:08:55 -0400
commitcfa75870e67e7759bba47f25b3fd7dd252f9341e (patch)
tree6ce8a77d9ebbbceeefb6eb0e153ce054b041fd8e /stdlib/source
parentee7721f3a9c0b899ab282dda120b0854a5cc0bd4 (diff)
Done with Lua.
Diffstat (limited to 'stdlib/source')
-rw-r--r--stdlib/source/lux/target/lua.lux13
-rw-r--r--stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/lua/common.lux15
-rw-r--r--stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/python/common.lux2
-rw-r--r--stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/runtime.lux54
-rw-r--r--stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/runtime.lux17
-rw-r--r--stdlib/source/test/lux/extension.lux6
6 files changed, 63 insertions, 44 deletions
diff --git a/stdlib/source/lux/target/lua.lux b/stdlib/source/lux/target/lua.lux
index c557c7feb..29d4b82b3 100644
--- a/stdlib/source/lux/target/lua.lux
+++ b/stdlib/source/lux/target/lua.lux
@@ -297,6 +297,13 @@
(..nest (:representation body!))
text.new_line "end")))
+ (def: #export (repeat until body!)
+ (-> Expression Statement Statement)
+ (:abstraction
+ (format "repeat"
+ (..nest (:representation body!))
+ text.new_line "until " (:representation until))))
+
(def: #export (for_in vars source body!)
(-> (List Var) Expression Statement Statement)
(:abstraction
@@ -396,10 +403,12 @@
[["error"]
["print"]
["require"]
- ["type"]]]
+ ["type"]
+ ["ipairs"]]]
[2
- [["print"]]]
+ [["print"]
+ ["error"]]]
[3
[["print"]]]
diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/lua/common.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/lua/common.lux
index ba12035c7..29d3704fe 100644
--- a/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/lua/common.lux
+++ b/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/lua/common.lux
@@ -104,13 +104,10 @@
(/.install "-" (binary (product.uncurry _.-)))
(/.install "<" (binary (product.uncurry _.<)))
(/.install "*" (binary (product.uncurry _.*)))
- (/.install "/" (binary (product.uncurry _.//)))
- (/.install "%" (binary (product.uncurry _.%)))
+ (/.install "/" (binary (product.uncurry //runtime.i64//division)))
+ (/.install "%" (binary (product.uncurry //runtime.i64//remainder)))
(/.install "f64" (unary (_./ (_.float +1.0))))
- (/.install "char" (unary //runtime.i64//char))
- ## TODO: Use version below once the Lua compiler becomes self-hosted.
- ## (/.install "char" (unary (for {@.lua (!unary "utf8.char")}
- ## (!unary "string.char"))))
+ (/.install "char" (unary (_.apply/1 (_.var "utf8.char"))))
)))
(def: f64//decode
@@ -129,7 +126,7 @@
(/.install "=" (binary (product.uncurry _.=)))
(/.install "<" (binary (product.uncurry _.<)))
(/.install "i64" (unary (!unary "math.floor")))
- (/.install "encode" (unary (_.apply/2 (_.var "string.format") (_.string "%.17f"))))
+ (/.install "encode" (unary (_.apply/2 (_.var "string.format") (_.string "%.17g"))))
(/.install "decode" (unary ..f64//decode)))))
(def: (text//char [paramO subjectO])
@@ -171,7 +168,9 @@
(|> /.empty
(/.install "log" (unary ..io//log!))
(/.install "error" (unary (!unary "error")))
- (/.install "current-time" (nullary (function.constant (//runtime.io//current_time //runtime.unit)))))))
+ (/.install "current-time" (nullary (function.constant (|> (_.var "os.time")
+ (_.apply/* (list))
+ (_.* (_.int +1,000)))))))))
(def: #export bundle
Bundle
diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/python/common.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/python/common.lux
index 90cafc75b..b87390e9a 100644
--- a/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/python/common.lux
+++ b/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/python/common.lux
@@ -108,7 +108,7 @@
(/.install "+" (binary (product.uncurry (..capped _.+))))
(/.install "-" (binary (product.uncurry (..capped _.-))))
(/.install "*" (binary (product.uncurry (..capped _.*))))
- (/.install "/" (binary (product.uncurry _.//)))
+ (/.install "/" (binary (product.uncurry //runtime.i64//division)))
(/.install "%" (binary (product.uncurry //runtime.i64//remainder)))
(/.install "f64" (unary _.float/1))
(/.install "char" (unary //runtime.i64//char))
diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/runtime.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/runtime.lux
index 503969782..14d206e23 100644
--- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/runtime.lux
+++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/runtime.lux
@@ -274,25 +274,31 @@
(_.bit_shr param)
(_.bit_and mask))))))
-## TODO: Remove this once the Lua compiler becomes self-hosted.
-(def: on_rembulan?
- (_.= (_.string "Lua 5.3")
- (_.var "_VERSION")))
-
-(runtime: (i64//char subject)
- (with_expansions [<rembulan> (_.return (_.apply/1 (_.var "string.char") subject))
- <normal> (_.return (_.apply/1 (_.var "utf8.char") subject))]
- (for {@.lua (_.return <normal>)}
- (_.if ..on_rembulan?
- <rembulan>
- <normal>))))
+(runtime: (i64//division param subject)
+ (with_vars [floored]
+ ($_ _.then
+ (_.local/1 floored (_.// param subject))
+ (let [potentially_floored? (_.< (_.int +0) floored)
+ inexact? (|> floored
+ (_.* param)
+ (_.= subject)
+ _.not)]
+ (_.if (_.and potentially_floored?
+ inexact?)
+ (_.return (_.+ (_.int +1) floored))
+ (_.return floored))))))
+
+(runtime: (i64//remainder param subject)
+ (_.return (_.- (|> subject (..i64//division param) (_.* param))
+ subject)))
(def: runtime//i64
Statement
($_ _.then
@i64//left_shift
@i64//right_shift
- @i64//char
+ @i64//division
+ @i64//remainder
))
(def: (find_byte_index subject param start)
@@ -314,6 +320,11 @@
(-> Expression Expression)
(_.- (_.int +1)))
+## TODO: Remove this once the Lua compiler becomes self-hosted.
+(def: on_rembulan?
+ (_.= (_.string "Lua 5.3")
+ (_.var "_VERSION")))
+
(runtime: (text//index subject param start)
(with_expansions [<rembulan> ($_ _.then
(_.local/1 byte_index (|> start
@@ -398,22 +409,6 @@
@array//write
))
-(runtime: (io//current_time _)
- (with_expansions [<rembulan> (_.return (_.int +0))
- <normal> (_.return (|> (_.var "os.time")
- (_.apply/* (list))
- (_.* (_.int +1,000))))]
- (for {@.lua <normal>}
- (_.if ..on_rembulan?
- <rembulan>
- <normal>))))
-
-(def: runtime//io
- Statement
- ($_ _.then
- @io//current_time
- ))
-
(def: runtime
Statement
($_ _.then
@@ -422,7 +417,6 @@
..runtime//i64
..runtime//text
..runtime//array
- ..runtime//io
))
(def: #export artifact ..prefix)
diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/runtime.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/runtime.lux
index a2e18808a..1af62cf7e 100644
--- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/runtime.lux
+++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/runtime.lux
@@ -309,8 +309,22 @@
..as_nat
(_.bit_shr param))))
+(runtime: (i64//division param subject)
+ (with_vars [floored]
+ ($_ _.then
+ (_.set (list floored) (_.// param subject))
+ (_.return (let [potentially_floored? (_.< (_.int +0) floored)
+ inexact? (|> floored
+ (_.* param)
+ (_.= subject)
+ _.not)]
+ (_.? (_.and potentially_floored?
+ inexact?)
+ (_.+ (_.int +1) floored)
+ floored))))))
+
(runtime: (i64//remainder param subject)
- (_.return (_.- (|> subject (_.// param) (_.* param))
+ (_.return (_.- (|> subject (..i64//division param) (_.* param))
subject)))
(template [<runtime> <python>]
@@ -342,6 +356,7 @@
@i64//nat_top
@i64//left_shift
@i64//right_shift
+ @i64//division
@i64//remainder
@i64//and
@i64//or
diff --git a/stdlib/source/test/lux/extension.lux b/stdlib/source/test/lux/extension.lux
index d032a47b5..67abd0eca 100644
--- a/stdlib/source/test/lux/extension.lux
+++ b/stdlib/source/test/lux/extension.lux
@@ -4,7 +4,8 @@
["@" target
["." jvm]
["." js]
- ["." python]]
+ ["." python]
+ ["." lua]]
[abstract
[monad (#+ do)]]
[control
@@ -63,7 +64,8 @@
(row.row (#jvm.Constant (#jvm.LDC (#jvm.String self))))
@.js (js.string self)
- @.python (python.unicode self)})))))
+ @.python (python.unicode self)
+ @.lua (lua.string self)})))))
(for {@.old
(as_is)}