aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEduardo Julian2017-08-22 20:49:20 -0400
committerEduardo Julian2017-08-22 20:49:20 -0400
commit8c2432ad7ce90dc59f59660d9c893381036f9d7e (patch)
tree8ebcd709b4a9aee3b6b1c9649735586435ceee27
parentd99484697f4d5bd78441ab001f487289b9cdc44e (diff)
- Added context/config variables, and their parsers (a.k.a. properties).
- Added a way to obtain a program's environment variables.
Diffstat (limited to '')
-rw-r--r--stdlib/source/lux/data/format/context.lux34
-rw-r--r--stdlib/source/lux/world/env.jvm.lux47
2 files changed, 81 insertions, 0 deletions
diff --git a/stdlib/source/lux/data/format/context.lux b/stdlib/source/lux/data/format/context.lux
new file mode 100644
index 000000000..a515052c6
--- /dev/null
+++ b/stdlib/source/lux/data/format/context.lux
@@ -0,0 +1,34 @@
+(;module:
+ lux
+ (lux (control ["p" parser]
+ ["ex" exception #+ exception:]
+ [monad #+ do])
+ (data ["R" result]
+ (coll ["d" dict]))))
+
+(exception: #export Unknown-Property)
+
+(type: #export Context
+ (d;Dict Text Text))
+
+(type: #export (Property a)
+ (p;Parser Context a))
+
+(def: #export (property name)
+ (-> Text (Property Text))
+ (function [context]
+ (case (d;get name context)
+ (#;Some value)
+ (ex;return [context value])
+
+ #;None
+ (ex;throw Unknown-Property name))))
+
+(def: #export (run context property)
+ (All [a] (-> Context (Property a) (R;Result a)))
+ (case (property context)
+ (#R;Success [_ output])
+ (#R;Success output)
+
+ (#R;Error error)
+ (#R;Error error)))
diff --git a/stdlib/source/lux/world/env.jvm.lux b/stdlib/source/lux/world/env.jvm.lux
new file mode 100644
index 000000000..29266cc84
--- /dev/null
+++ b/stdlib/source/lux/world/env.jvm.lux
@@ -0,0 +1,47 @@
+(;module:
+ lux
+ (lux (data [text]
+ (format [context #+ Context])
+ (coll [list "L/" Functor<List>]
+ ["d" dict]))
+ [io #- run]
+ [host #+ jvm-import]))
+
+(jvm-import java.lang.String)
+
+(jvm-import (java.util.Map$Entry k v)
+ (getKey [] k)
+ (getValue [] v))
+
+(jvm-import (java.util.Iterator a)
+ (hasNext [] boolean)
+ (next [] a))
+
+(jvm-import (java.util.Set a)
+ (iterator [] (Iterator a)))
+
+(jvm-import (java.util.Map k v)
+ (entrySet [] (Set (Map$Entry k v))))
+
+(jvm-import java.lang.System
+ (#static getenv [] (java.util.Map String String)))
+
+(def: (consume-iterator f iterator)
+ (All [a b] (-> (-> a b) (Iterator a) (List b)))
+ (if (Iterator.hasNext [] iterator)
+ (#;Cons (f (Iterator.next [] iterator))
+ (consume-iterator f iterator))
+ #;Nil))
+
+(def: (entry-to-kv entry)
+ (All [k v] (-> (Map$Entry k v) [k v]))
+ [(Map$Entry.getKey [] entry)
+ (Map$Entry.getValue [] entry)])
+
+(def: #export read
+ (IO Context)
+ (io (|> (System.getenv [])
+ (Map.entrySet [])
+ (Set.iterator [])
+ (consume-iterator entry-to-kv)
+ (d;from-list text;Hash<Text>))))