aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/ffi/export.rb.lux
blob: 275a095a599bbfceabc98b796db2d2b2637d1b07 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
(.using
 [library
  [lux (.except global)
   [extension (.only directive:)]
   ["[0]" meta]
   ["[0]" static]
   ["[0]" type]
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["<>" parser
     ["<[0]>" code]
     ["<[0]>" text (.only Parser)]]]
   [data
    [text
     ["%" format]]
    [collection
     ["[0]" list ("[1]#[0]" monad mix)]
     ["[0]" set]]]
   ["[0]" macro
    [syntax (.only syntax:)]
    ["[0]" code]]
   [math
    ["[0]" random]]
   [target
    ["/" ruby]]
   [tool
    [compiler
     ["[0]" phase]
     [meta
      [cache
       ["[0]" dependency "_"
        ["[1]" artifact]]]]
     [language
      [lux
       ["[0]" generation]
       ["[0]" directive]
       ["[0]" analysis "_"
        ["[1]" type]]]]]]]])

(def: upper! (<text>.one_of! "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
(def: lower! (<text>.one_of! "abcdefghijklmnopqrstuvwxyz"))
(def: decimal! (<text>.one_of! "0123456789"))
(def: sigil! (<text>.one_of! "_"))

(def: tail!
  (all <>.either
       ..upper!
       ..lower!
       ..decimal!
       ..sigil!
       ))

(template [<name> <head>]
  [(def: <name>
     (Parser Text)
     (<| <text>.slice
         (<text>.and! <head>)
         (<text>.some! ..tail!)))]

  [method ..lower!]
  [global (<text>.one_of! "$")]
  [constant ..upper!]
  )

(type: Name
  (Variant
   {#Method Text}
   {#Global Text}))

(def: name
  (Parser Name)
  (<>.or ..method
         (<>.either ..global
                    ..constant)))

(def: definition
  (-> Code (Meta [Name Code]))
  (|>> (list)
       (<code>.result (<| <code>.form
                          (<>.after (<code>.this_text "lux def"))
                          (<>.before <code>.any)
                          (all <>.and
                               (<text>.then ..name <code>.local)
                               <code>.any)))
       meta.lifted))

(with_expansions [<extension> (static.random (|>> %.nat (%.format "ruby export ") code.text)
                                             random.nat)]
  (directive: (<extension> self phase archive [global? <code>.bit
                                               name <code>.text
                                               term <code>.any])
    (do [! phase.monad]
      [next directive.analysis
       [type term] (<| directive.lifted_analysis
                       analysis.inferring
                       (next archive term))

       next directive.synthesis
       term (directive.lifted_synthesis
             (next archive term))

       dependencies (directive.lifted_generation
                     (dependency.dependencies archive term))

       next directive.generation
       [interim_artifacts term] (directive.lifted_generation
                                 (generation.with_interim_artifacts archive
                                   (next archive term)))

       _ (directive.lifted_generation
          (do !
            [@self (generation.learn_custom name (list#mix set.has dependencies interim_artifacts))
             .let [[:input:/* :output:] (type.flat_function type)
                   code (if global?
                          (/.set (list (/.manual name)) term)
                          (case :input:/*
                            {.#End}
                            (/.function (/.manual name) (list)
                              (/.return term))

                            _
                            (/.statement (/.apply (list (/.string name) term) {.#None}
                                                  (/.manual "define_method")))))]
             _ (generation.execute! code)
             _ (generation.save! @self {.#None} code)]
            (generation.log! (%.format "Export " (%.text name)))))]
      (in directive.no_requirements)))

  (syntax: .public (export: [exports (<>.many <code>.any)])
    (let [! meta.monad]
      (|> exports
          (monad.each ! macro.expansion)
          (# ! each (|>> list#conjoint
                         (monad.each ! ..definition)))
          (# ! conjoint)
          (# ! each (list#each (function (_ [name term])
                                 (` (<extension> (~+ (case name
                                                       {#Method name}
                                                       (list (code.bit #0) (code.text name))
                                                       
                                                       {#Global name}
                                                       (list (code.bit #1) (code.text name))))
                                                 (~ term))))))))))