aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/program.lux
blob: 3f88d5de39bf58ea0aa72f702a130f153026b6c5 (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
(.using
 [library
  [lux "*"
   ["@" target]
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" io]
    [concurrency
     ["[0]" thread]]
    ["<>" parser
     ["<[0]>" code]
     ["<[0]>" cli]]]
   [macro (.only with_symbols)
    [syntax (.only syntax:)]
    ["[0]" code]]]])

(type: Arguments
  (Variant
   {#Raw Text}
   {#Parsed (List Code)}))

(def: arguments^
  (<code>.Parser Arguments)
  (<>.or <code>.local
         (<code>.tuple (<>.some <code>.any))))

(syntax: .public (program: [args ..arguments^
                            body <code>.any])
  (with_symbols [g!program g!args g!_ g!output g!message]
    (let [initialization+event_loop (for @.old body
                                         @.jvm body
                                         @.js body
                                         @.python body
                                         (` ((~! do) (~! io.monad)
                                             [(~ g!output) (~ body)
                                              (~ g!_) (~! thread.run!)]
                                             ((~' in) (~ g!output)))))]
      (in (list (` ("lux def program"
                    (~ (case args
                         {#Raw args}
                         (` (.function ((~ g!program) (~ (code.symbol ["" args])))
                              (~ initialization+event_loop)))
                         
                         {#Parsed args}
                         (` (.function ((~ g!program) (~ g!args))
                              (case ((~! <cli>.result) (.is (~! (<cli>.Parser (io.IO .Any)))
                                                            ((~! do) (~! <>.monad)
                                                             [(~+ args)
                                                              (~ g!_) (~! <cli>.end)]
                                                             ((~' in) (~ initialization+event_loop))))
                                     (~ g!args))
                                {.#Right (~ g!output)}
                                (~ g!output)

                                {.#Left (~ g!message)}
                                (.panic! (~ g!message))))))))))))))