aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/program.lux
blob: 51c22c701272429dc816002bc3ecaa570cec87a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(.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))
                                                           (~ g!_) (~! <cli>.end)]
                                                          ((~' wrap) (~ initialization+event_loop))))
                                       (~ g!args))
                                  (#.Right (~ g!output))
                                  (~ g!output)

                                  (#.Left (~ g!message))
                                  (.error! (~ g!message))))))))))))))