aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/function/inline.lux
blob: bbdbdd4346394dea8bdd6c99a8861fd832fe04b9 (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
(.require
 [library
  [lux (.except Declaration)
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["<>" parser]]
   [data
    [collection
     ["[0]" list (.use "[1]#[0]" monad)]]]
   ["[0]" meta (.only)
    ["[0]" code (.only)
     ["<[1]>" \\parser (.only Parser)]]
    ["[0]" macro (.only with_symbols)
     [syntax (.only syntax)]]]]])

(type Declaration
  [Text (List Code)])

(def declaration
  (Parser Declaration)
  (<code>.form (<>.and <code>.local (<>.some <code>.any))))

(def inline
  (Parser [Declaration Code Code])
  (all <>.and
       ..declaration
       <code>.any
       <code>.any
       ))

(def .public inlined
  (syntax (_ [[[name parameters] type term] ..inline])
    (with_symbols [g!_]
      (do [! meta.monad]
        [@ meta.current_module_name
         g!parameters (|> (macro.symbol "parameter")
                          (list.repeated (list.size parameters))
                          (monad.all !))
         .let [inlined (` (("lux in-module"
                            (, (code.text @))
                            (.is (, type)
                                 (.function ((, (code.local name)) (,* parameters))
                                   (, term))))
                           (,* (list#each (function (_ g!parameter)
                                            (` ((,' ,) (, g!parameter))))
                                          g!parameters))))
               g!parameters (|> g!parameters
                                (list#each (function (_ parameter)
                                             (list parameter (` <code>.any))))
                                list#conjoint)]]
        (in (list (` (syntax ((, g!_) [(,* g!parameters)])
                       (.at meta.monad (,' in) (.list (.`' (, inlined))))))))))))