From 5a619fc3978d1ded629f7c255d1c1c672033ad54 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Thu, 23 Nov 2017 19:10:57 -0400 Subject: - Added console I/O. --- stdlib/source/lux/world/console.lux | 74 +++++++++++++++++++++++++++++++++++++ stdlib/test/tests.lux | 3 +- 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 stdlib/source/lux/world/console.lux diff --git a/stdlib/source/lux/world/console.lux b/stdlib/source/lux/world/console.lux new file mode 100644 index 000000000..b479aa3e4 --- /dev/null +++ b/stdlib/source/lux/world/console.lux @@ -0,0 +1,74 @@ +(;module: + [lux #- open] + (lux (control [monad #+ do]) + (data ["e" error] + [text]) + (concurrency [promise] + [task #+ Task]) + (type object) + [io #+ IO Process io] + [host])) + +(interface: #export Console + (read-char [] (Task Text)) + (read-line [] (Task Text)) + (write [Text] (Task Unit)) + (close [] (Task Unit))) + +(for {"JVM" + (as-is (host;import java.lang.AutoCloseable + (close [] #io #try void)) + + (host;import java.io.InputStream) + + (host;import java.io.Reader) + + (host;import java.io.InputStreamReader + (new [InputStream])) + + (host;import java.io.BufferedReader + (new [Reader]) + (read [] #io #try int) + (readLine [] #io #try String)) + + (host;import java.io.PrintStream + (print [String] #io #try void)) + + (host;import java.lang.System + (#static in java.io.InputStream) + (#static out java.io.PrintStream)) + + (class: JVM-Console Console + {#input BufferedReader + #output PrintStream} + + (def: read-char + (|>. get@Console + (get@ #input) + (BufferedReader.read []) + (:: io;Functor map (|>. int-to-nat text;from-code)) + promise;future)) + + (def: read-line + (|>. get@Console + (get@ #input) + (BufferedReader.readLine []) + promise;future)) + + (def: (write message) + (|>. get@Console + (get@ #output) + (PrintStream.print [message]) + promise;future)) + + (def: (close self) + (promise;future + (do io;Monad + [_ (AutoCloseable.close [] (|> self get@Console (get@ #input)))] + (AutoCloseable.close [] (|> self get@Console (get@ #output))))))) + + (def: #export open + (Process Console) + (io (#e;Success (new@JVM-Console {#input (|> System.in InputStreamReader.new BufferedReader.new) + #output System.out}))))) + }) diff --git a/stdlib/test/tests.lux b/stdlib/test/tests.lux index 3f02bf694..26dfa0ac7 100644 --- a/stdlib/test/tests.lux +++ b/stdlib/test/tests.lux @@ -90,7 +90,8 @@ (math [random]) [macro] (type [unit]) - [world/env]) + [world/env] + [world/console]) ) (program: args -- cgit v1.2.3