diff options
author | Eduardo Julian | 2015-09-02 08:11:14 -0400 |
---|---|---|
committer | Eduardo Julian | 2015-09-02 08:11:14 -0400 |
commit | a0eb061edbbb8bca666add620e4c82c4f3bc5fdc (patch) | |
tree | 8528f1020939a01bd5217a747268480e7ad8e67c /source/lux/host | |
parent | f270a49ce40829dc28c6254d7ed4eeb19f360f59 (diff) |
- Added a new (albeit small) I/O library with host-dependent functions.
Diffstat (limited to 'source/lux/host')
-rw-r--r-- | source/lux/host/io.lux | 35 | ||||
-rw-r--r-- | source/lux/host/jvm.lux | 2 |
2 files changed, 36 insertions, 1 deletions
diff --git a/source/lux/host/io.lux b/source/lux/host/io.lux new file mode 100644 index 000000000..7611e41b7 --- /dev/null +++ b/source/lux/host/io.lux @@ -0,0 +1,35 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +## If a copy of the MPL was not distributed with this file, +## You can obtain one at http://mozilla.org/MPL/2.0/. + +(;import lux + lux/data/io + (.. jvm)) + +## [Functions] +(do-template [<name> <method> <type> <class>] + [(def #export (<name> x) + (-> <type> (IO (,))) + (@io (.! (<method> [<class>] [x]) + (..? out java.lang.System))))] + + [write-char print Char char] + [write print Text java.lang.String] + [write-line println Text java.lang.String]) + +(do-template [<name> <type> <op>] + [(def #export <name> + (IO (Maybe <type>)) + (let [in (..? in java.lang.System) + reader (new java.io.InputStreamReader [java.io.InputStream] [in]) + buff-reader (new java.io.BufferedReader [java.io.Reader] [reader])] + (@io (let [output (: (Either Text <type>) (try$ <op>))] + (exec (.! (close [] []) buff-reader) + (case output + (#;Left _) #;None + (#;Right input) (#;Some input)))))))] + + [read-char Char (_jvm_i2c (.! (read [] []) buff-reader))] + [read-line Text (.! (readLine [] []) buff-reader)] + ) diff --git a/source/lux/host/jvm.lux b/source/lux/host/jvm.lux index 7a564826c..eddedfdc5 100644 --- a/source/lux/host/jvm.lux +++ b/source/lux/host/jvm.lux @@ -247,4 +247,4 @@ (defsyntax #export (try$ expr) (emit (@list (` (try (#;Right (~ expr)) (~ (' (catch java.lang.Exception e - (#;Left (.! (getMessage [] []) e)))))))))) + (#;Left (_jvm_invokevirtual "java.lang.Throwable" "getMessage" [] e [])))))))))) |