aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/pipe.lux
blob: 47666434428316c9d28486925ac9574873fbb9f2 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
(.using
 [library
  [lux {"-" let cond if exec case}
   [abstract
    ["[0]" monad]]
   [control
    ["[0]" try]
    ["<>" parser
     ["<[0]>" code {"+" Parser}]]]
   [data
    ["[0]" identity]
    [collection
     ["[0]" list ("[1]#[0]" monad)]]]
   [macro {"+" with_symbols}
    [syntax {"+" syntax:}]
    ["[0]" code]]
   [math
    [number
     ["n" nat]
     ["i" int]]]]])

(def: body
  (Parser (List Code))
  (<code>.tuple (<>.some <code>.any)))

(syntax: .public (new [start <code>.any
                       body ..body
                       prev <code>.any])
  (in (list (` (|> (~ start) (~+ body))))))

(syntax: .public (let [binding <code>.any
                       body <code>.any
                       prev <code>.any])
  (in (list (` (.let [(~ binding) (~ prev)]
                 (~ body))))))

(def: _reversed_
  (Parser Any)
  (function (_ tokens)
    {try.#Success [(list.reversed tokens) []]}))

(syntax: .public (cond [_ _reversed_
                        prev <code>.any
                        else ..body
                        _ _reversed_
                        branches (<>.some (<>.and ..body ..body))])
  (with_symbols [g!temp]
    (in (list (` (.let [(~ g!temp) (~ prev)]
                   (.cond (~+ (monad.do list.monad
                                [[test then] branches]
                                (list (` (|> (~ g!temp) (~+ test)))
                                      (` (|> (~ g!temp) (~+ then))))))
                          (|> (~ g!temp) (~+ else)))))))))

(syntax: .public (if [test ..body
                      then ..body
                      else ..body
                      prev <code>.any])
  (in (list (` (..cond [(~+ test)] [(~+ then)]
                       [(~+ else)]
                       (~ prev))))))

(syntax: .public (when [test ..body
                        then ..body
                        prev <code>.any])
  (in (list (` (..cond [(~+ test)] [(~+ then)]
                       []
                       (~ prev))))))

(syntax: .public (while [test ..body
                         then ..body
                         prev <code>.any])
  (with_symbols [g!temp g!again]
    (in (list (` (.loop ((~ g!again) [(~ g!temp) (~ prev)])
                   (.if (|> (~ g!temp) (~+ test))
                     ((~ g!again) (|> (~ g!temp) (~+ then)))
                     (~ g!temp))))))))

(syntax: .public (do [monad <code>.any
                      steps (<>.some ..body)
                      prev <code>.any])
  (with_symbols [g!temp]
    (.case (list.reversed steps)
      (pattern (partial_list last_step prev_steps))
      (.let [step_bindings (monad.do list.monad
                             [step (list.reversed prev_steps)]
                             (list g!temp (` (|> (~ g!temp) (~+ step)))))]
        (in (list (` ((~! monad.do) (~ monad)
                      [.let [(~ g!temp) (~ prev)]
                       (~+ step_bindings)]
                      (|> (~ g!temp) (~+ last_step)))))))

      _
      (in (list prev)))))

(syntax: .public (exec [body ..body
                        prev <code>.any])
  (with_symbols [g!temp]
    (in (list (` (.let [(~ g!temp) (~ prev)]
                   (.exec (|> (~ g!temp) (~+ body))
                     (~ g!temp))))))))

(syntax: .public (tuple [paths (<>.many ..body)
                         prev <code>.any])
  (with_symbols [g!temp]
    (in (list (` (.let [(~ g!temp) (~ prev)]
                   [(~+ (list#each (function (_ body) (` (|> (~ g!temp) (~+ body))))
                                   paths))]))))))

(syntax: .public (case [branches (<>.many (<>.and <code>.any <code>.any))
                        prev <code>.any])
  (in (list (` (.case (~ prev)
                 (~+ (|> branches
                         (list#each (function (_ [pattern body]) (list pattern body)))
                         list#conjoint)))))))