aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/io.jvm.lux
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--new-luxc/source/luxc/io.jvm.lux93
1 files changed, 93 insertions, 0 deletions
diff --git a/new-luxc/source/luxc/io.jvm.lux b/new-luxc/source/luxc/io.jvm.lux
new file mode 100644
index 000000000..ab62b8f43
--- /dev/null
+++ b/new-luxc/source/luxc/io.jvm.lux
@@ -0,0 +1,93 @@
+(;module:
+ lux
+ (lux (control monad)
+ [io #- run]
+ (concurrency ["P" promise])
+ (data ["E" error]
+ [text "T/" Eq<Text>]
+ text/format)
+ [macro]
+ host)
+ (luxc ["&" base]))
+
+(jvm-import java.io.File
+ (new [String String])
+ (exists [] #io #try boolean))
+
+(jvm-import java.io.Reader
+ (close [] #io #try void))
+
+(jvm-import java.io.FileReader
+ (new [File]))
+
+(jvm-import java.io.BufferedReader
+ (new [Reader])
+ (readLine [] #io #try #? String))
+
+(def: host-extension Text ".jvm")
+
+(def: (find-in-sources path source-dirs)
+ (-> &;Path (List &;Path) (P;Promise (Maybe File)))
+ (loop [source-dirs source-dirs]
+ (case source-dirs
+ #;Nil
+ (:: P;Monad<Promise> wrap #;None)
+
+ (#;Cons dir source-dirs')
+ (do P;Monad<Promise>
+ [#let [file (File.new [dir path])]
+ ?? (P;future (File.exists [] file))]
+ (case ??
+ (#;Right true)
+ (wrap (#;Some file))
+
+ _
+ (recur source-dirs'))))))
+
+(def: (read-source-code lux-file)
+ (-> File (P;Promise (E;Error Text)))
+ (P;future
+ (let [reader (|> lux-file FileReader.new BufferedReader.new)]
+ (loop [total ""]
+ (do Monad<IO>
+ [?line (BufferedReader.readLine [] reader)]
+ (case ?line
+ (#E;Error error)
+ (wrap (#E;Error error))
+
+ (#E;Success #;None)
+ (wrap (#E;Success total))
+
+ (#E;Success (#;Some line))
+ (if (T/= "" total)
+ (recur line)
+ (recur (format total "\n" line)))))))))
+
+(def: #export (read-module source-dirs module-name)
+ (-> (List &;Path) Text (P;Promise (E;Error [&;Path Text])))
+ (let [host-path (format module-name host-extension ".lux")
+ lux-path (format module-name ".lux")]
+ (let% [<tries> (do-template [<path>]
+ [(do P;Monad<Promise>
+ [?file (find-in-sources <path> source-dirs)])
+ (case ?file
+ (#;Some file)
+ (do @
+ [?code (read-source-code file)]
+ (case ?code
+ (#E;Error error)
+ (wrap (#E;Error error))
+
+ (#E;Success code)
+ (wrap (#E;Success [<path> code]))))
+
+ #;None)]
+
+ [host-path]
+ [lux-path])]
+ (<| <tries>
+ (wrap (#E;Error (format "Module cannot be found: " module-name)))))))
+
+(def: #export (write-module module-name module-descriptor)
+ (-> Text Text (P;Promise Unit))
+ (undefined))