From 5a26c40dc215dfb22a77cad28455deff28ca9976 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Fri, 11 Sep 2015 19:46:30 -0400 Subject: - Implemented the with-open macro. - Cleaned-up a bit the tag-generation macro "deftags". --- source/lux/host/io.lux | 31 ++++++++++++++++++++++++++++--- source/lux/host/jvm.lux | 4 ++-- 2 files changed, 30 insertions(+), 5 deletions(-) (limited to 'source') 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 [ ] [(def #export @@ -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 ) (try$ ))] + (@io (let [output (: (Either Text ) (try ))] (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 [])))))))))) -- cgit v1.2.3