From 90399879ee7cc61e6333f7e81141441d32fcdb2e Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Mon, 3 Aug 2015 01:29:48 -0400 Subject: Implemented text-interpolation through a macro ("<>") in lux/data/text --- source/lux/control/show.lux | 2 +- source/lux/control/stack.lux | 23 ++++++++++++++++++++++ source/lux/data/io.lux | 3 ++- source/lux/data/maybe.lux | 31 ++++++++++++++++------------- source/lux/data/text.lux | 47 +++++++++++++++++++++++++++++++++++++++----- source/program.lux | 7 ++++--- 6 files changed, 89 insertions(+), 24 deletions(-) create mode 100644 source/lux/control/stack.lux (limited to 'source') diff --git a/source/lux/control/show.lux b/source/lux/control/show.lux index f4e1cf762..adb5f911e 100644 --- a/source/lux/control/show.lux +++ b/source/lux/control/show.lux @@ -8,7 +8,7 @@ (;import lux) -## Signatures +## [Signatures] (defsig #export (Show a) (: (-> a Text) show)) diff --git a/source/lux/control/stack.lux b/source/lux/control/stack.lux new file mode 100644 index 000000000..1e5d086c5 --- /dev/null +++ b/source/lux/control/stack.lux @@ -0,0 +1,23 @@ +## Copyright (c) Eduardo Julian. All rights reserved. +## The use and distribution terms for this software are covered by the +## Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +## which can be found in the file epl-v10.html at the root of this distribution. +## By using this software in any fashion, you are agreeing to be bound by +## the terms of this license. +## You must not remove this notice, or any other, from this software. + +(;import lux) + +## [Signatures] +(defsig #export (Stack s) + (: (All [a] (s a)) + empty) + (: (All [a] (-> (s a) Bool)) + empty?) + (: (All [a] (-> a (s a) (s a))) + push) + (: (All [a] (-> (s a) (Maybe (s a)))) + pop) + (: (All [a] (-> (s a) (Maybe a))) + top) + ) diff --git a/source/lux/data/io.lux b/source/lux/data/io.lux index a194fc854..e5b265959 100644 --- a/source/lux/data/io.lux +++ b/source/lux/data/io.lux @@ -49,4 +49,5 @@ (def #export (println x) (-> Text (IO (,))) - (print (text:++ x "\n"))) + (io (_jvm_invokevirtual "java.io.PrintStream" "println" ["java.lang.Object"] + (_jvm_getstatic "java.lang.System" "out") [x]))) diff --git a/source/lux/data/maybe.lux b/source/lux/data/maybe.lux index 396ec470a..a6019e256 100644 --- a/source/lux/data/maybe.lux +++ b/source/lux/data/maybe.lux @@ -7,12 +7,11 @@ ## You must not remove this notice, or any other, from this software. (;import lux - (.. list) - (lux (control (monoid #as m #refer #all) + (lux (meta macro) + (control (monoid #as m #refer #all) (functor #as F #refer #all) - (monad #as M #refer #all)) - (meta lux - syntax))) + (monad #as M #refer #all))) + (.. list)) ## [Types] ## (deftype (Maybe a) @@ -45,12 +44,16 @@ (#;Some xs) xs))) ## [Syntax] -(defsyntax #export (? maybe else) - (do Lux/Monad - [g!value (gensym "")] - (M;wrap (list (` (case (~ maybe) - (#;Some (~ g!value)) - (~ g!value) - - _ - (~ else))))))) +(defmacro #export (? tokens state) + (case tokens + (\ (list maybe else)) + (let [g!value (symbol$ ["" "_"])] + (#;Right state (list (` (case (~ maybe) + (#;Some (~ g!value)) + (~ g!value) + + _ + (~ else)))))) + + _ + (#;Left "Wrong syntax for ?"))) diff --git a/source/lux/data/text.lux b/source/lux/data/text.lux index c3cb1ecfb..ae4f9974f 100644 --- a/source/lux/data/text.lux +++ b/source/lux/data/text.lux @@ -7,11 +7,15 @@ ## You must not remove this notice, or any other, from this software. (;import lux - (lux (control (monoid #as m) + (lux (meta macro) + (control (monoid #as m) (eq #as E) (ord #as O) - (show #as S)) - (data/number (int #open ("i" Int/Number Int/Ord Int/Eq))))) + (show #as S) + (monad #as M #refer #all)) + (data (number (int #open ("i" Int/Number Int/Ord Int/Eq))) + maybe + (list #refer (#only foldL list list&))))) ## [Functions] (def #export (size x) @@ -132,11 +136,44 @@ [O;>= i>=])) (defstruct #export Text/Show (S;Show Text) - (def (S;show x) - x)) + (def S;show id)) (defstruct #export Text/Monoid (m;Monoid Text) (def m;unit "") (def (m;++ x y) (_jvm_invokevirtual "java.lang.String" "concat" ["java.lang.String"] x [y]))) + +## [Syntax] +(def (extract-var template) + (-> Text (Maybe (, Text Text Text))) + (exec (_jvm_invokevirtual "java.io.PrintStream" "println" ["java.lang.Object"] + (_jvm_getstatic "java.lang.System" "out") [(:: Text/Monoid (m;++ "Template: " template))]) + (do Maybe/Monad + [pre-idx (index-of "#{" template) + [pre in] (split pre-idx template) + [_ in] (split 2 in) + post-idx (index-of "}" in) + [var post] (split post-idx in) + [_ post] (split 1 post)] + (M;wrap [pre var post])))) + +(def (unravel-template template) + (-> Text (List Syntax)) + (case (extract-var template) + (#;Some [pre var post]) + (list& (text$ pre) (symbol$ ["" var]) + (unravel-template post)) + + #;None + (list (text$ template)))) + +(defmacro #export (<> tokens state) + (case tokens + (\ (list (#;Meta _ (#;TextS template)))) + (let [++ (symbol$ ["" ""])] + (#;Right state (list (` (;let [(~ ++) (;:: Text/Monoid m;++)] + (;$ (~ ++) (~@ (unravel-template template)))))))) + + _ + (#;Left "Wrong syntax for <>"))) diff --git a/source/program.lux b/source/program.lux index ae3421078..b7cce5714 100644 --- a/source/program.lux +++ b/source/program.lux @@ -17,7 +17,8 @@ hash ord show - number) + number + stack) (data bool char (either #as e) @@ -28,7 +29,7 @@ maybe (number int real) - (text #as t #open ("text:" Text/Monoid)) + (text #as t #refer (#only <>) #open ("text:" Text/Monoid)) writer tuple) (codata (stream #as S) @@ -46,7 +47,7 @@ (program args (case args (\ (list name)) - (println ($ text:++ "Hello, " name "!")) + (println (<> "Hello, #{name}!")) _ (println "Hello, world!"))) -- cgit v1.2.3