aboutsummaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorEduardo Julian2015-09-11 19:46:30 -0400
committerEduardo Julian2015-09-11 19:46:30 -0400
commit5a26c40dc215dfb22a77cad28455deff28ca9976 (patch)
tree2b2af63d2d6b5a68df72f65f4f570f8f0531d347 /source
parent113143d5d2e86185a8fca5214cfa57b4456bfbbb (diff)
- Implemented the with-open macro.
- Cleaned-up a bit the tag-generation macro "deftags".
Diffstat (limited to 'source')
-rw-r--r--source/lux/host/io.lux31
-rw-r--r--source/lux/host/jvm.lux4
2 files changed, 30 insertions, 5 deletions
diff --git a/source/lux/host/io.lux b/source/lux/host/io.lux
index 7c017a62e..4542b0519 100644
--- a/source/lux/host/io.lux
+++ b/source/lux/host/io.lux
@@ -4,7 +4,12 @@
## You can obtain one at http://mozilla.org/MPL/2.0/.
(;import lux
- lux/data/io
+ (lux (data io
+ (list #refer #all #open ("" List/Fold)))
+ (meta ast
+ syntax
+ lux)
+ control/monad)
(.. jvm))
## [Functions]
@@ -16,7 +21,8 @@
[write-char "print" Char "char"]
[write "print" Text "java.lang.String"]
- [write-line "println" Text "java.lang.String"])
+ [write-line "println" Text "java.lang.String"]
+ )
(do-template [<name> <type> <op>]
[(def #export <name>
@@ -24,7 +30,7 @@
(let [in (_jvm_getstatic "java.lang.System" "in")
reader (_jvm_new "java.io.InputStreamReader" ["java.io.InputStream"] [in])
buff-reader (_jvm_new "java.io.BufferedReader" ["java.io.Reader"] [reader])]
- (@io (let [output (: (Either Text <type>) (try$ <op>))]
+ (@io (let [output (: (Either Text <type>) (try <op>))]
(exec (_jvm_invokeinterface "java.io.Closeable" "close" [] buff-reader [])
(case output
(#;Left _) #;None
@@ -33,3 +39,22 @@
[read-char Char (_jvm_i2c (_jvm_invokevirtual "java.io.BufferedReader" "read" [] buff-reader []))]
[read-line Text (_jvm_invokevirtual "java.io.BufferedReader" "readLine" [] buff-reader [])]
)
+
+## [Syntax]
+(def simple-bindings^
+ (Parser (List (, Text AST)))
+ (tuple^ (*^ (&^ local-symbol^ id^))))
+
+(defsyntax #export (with-open [bindings simple-bindings^] body)
+ (do Lux/Monad
+ [g!output (gensym "output")
+ #let [code (foldL (: (-> AST (, Text AST) AST)
+ (lambda [body [res-name res-value]]
+ (let [g!res-name (symbol$ ["" res-name])]
+ (` (let [(~ g!res-name) (~ res-value)
+ (~ g!output) (~ body)]
+ (exec (;_jvm_invokeinterface "java.io.Closeable" "close" [] (~ g!res-name) [])
+ (~ g!output)))))))
+ body
+ (reverse bindings))]]
+ (wrap (@list code))))
diff --git a/source/lux/host/jvm.lux b/source/lux/host/jvm.lux
index 6f121a633..c1e122bb6 100644
--- a/source/lux/host/jvm.lux
+++ b/source/lux/host/jvm.lux
@@ -95,7 +95,7 @@
(emit (@list (` (;_jvm_program (~ (symbol$ args))
(~ body))))))
-(defsyntax #export (->maybe expr)
+(defsyntax #export (??? expr)
(do Lux/Monad
[g!val (gensym "")]
(emit (@list (` (let [(~ g!val) (~ expr)]
@@ -103,7 +103,7 @@
#;None
(#;Some (~ g!val)))))))))
-(defsyntax #export (try$ expr)
+(defsyntax #export (try expr)
(emit (@list (` (;_jvm_try (#;Right (~ expr))
(~ (' (_jvm_catch "java.lang.Exception" e
(#;Left (_jvm_invokevirtual "java.lang.Throwable" "getMessage" [] e []))))))))))