aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/program.lux
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--stdlib/source/library/lux/program.lux83
1 files changed, 83 insertions, 0 deletions
diff --git a/stdlib/source/library/lux/program.lux b/stdlib/source/library/lux/program.lux
new file mode 100644
index 000000000..bd486796b
--- /dev/null
+++ b/stdlib/source/library/lux/program.lux
@@ -0,0 +1,83 @@
+(.module:
+ [library
+ [lux #*
+ ["@" target]
+ [abstract
+ [monad (#+ do)]]
+ [control
+ ["." io]
+ [concurrency
+ ["." thread]]
+ ["<>" parser
+ ["<.>" code]
+ ["<.>" cli]]]
+ [data
+ ["." text]
+ [collection
+ ["." list ("#\." monad)]]]
+ [macro (#+ with_gensyms)
+ [syntax (#+ syntax:)]
+ ["." code]]]])
+
+(type: Arguments
+ (#Raw Text)
+ (#Parsed (List [Code Code])))
+
+(def: arguments^
+ (<code>.Parser Arguments)
+ (<>.or <code>.local_identifier
+ (<code>.tuple (<>.some (<>.either (do <>.monad
+ [name <code>.local_identifier]
+ (wrap [(code.identifier ["" name]) (` (~! <cli>.any))]))
+ (<code>.record (<>.and <code>.any <code>.any)))))))
+
+(syntax: #export (program:
+ {args ..arguments^}
+ body)
+ {#.doc (doc "Defines the entry-point to a program (similar to the 'main' function/method in other programming languages)."
+ "Can take a list of all the input parameters to the program."
+ "Or, can destructure them using CLI-option combinators from the lux/control/parser/cli module."
+ (program: all_args
+ (do io.monad
+ [foo init_program
+ bar (do_something all_args)]
+ (wrap [])))
+
+ (program: [name]
+ (io (log! (\ text.monoid compose "Hello, " name))))
+
+ (program: [{config configuration_parser}]
+ (do io.monad
+ [data (init_program config)]
+ (do_something data))))}
+ (with_gensyms [g!program g!args g!_ g!output g!message]
+ (let [initialization+event_loop
+ (` ((~! do) (~! io.monad)
+ [(~ g!output) (~ body)
+ (~+ (for {@.old (list)
+ @.jvm (list)
+ @.js (list)
+ @.python (list)}
+ (list g!_ (` (~! thread.run!)))))]
+ ((~' wrap) (~ g!output))))]
+ (wrap (list (` ("lux def program"
+ (~ (case args
+ (#Raw args)
+ (` (.function ((~ g!program) (~ (code.identifier ["" args])))
+ (~ initialization+event_loop)))
+
+ (#Parsed args)
+ (` (.function ((~ g!program) (~ g!args))
+ (case ((~! <cli>.run) (: (~! (<cli>.Parser (io.IO .Any)))
+ ((~! do) (~! <>.monad)
+ [(~+ (|> args
+ (list\map (function (_ [binding parser])
+ (list binding parser)))
+ list\join))]
+ ((~' wrap) (~ initialization+event_loop))))
+ (~ g!args))
+ (#.Right (~ g!output))
+ (~ g!output)
+
+ (#.Left (~ g!message))
+ (.error! (~ g!message))))))))))))))