aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/php/common.lux
blob: 9a06cd9809cc09478242ac1cb78aea5066a62218 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
(.module:
  [library
   [lux "*"
    [abstract
     ["[0]" monad {"+" [do]}]]
    [control
     ["[0]" function]
     ["[0]" try]
     ["<>" parser
      ["<s>" synthesis {"+" [Parser]}]]]
    [data
     ["[0]" product]
     ["[0]" text
      ["%" format {"+" [format]}]]
     [collection
      ["[0]" dictionary]
      ["[0]" set]
      ["[0]" list ("[1]\[0]" functor mix)]]]
    [math
     [number
      ["f" frac]]]
    ["@" target
     ["_" php {"+" [Expression]}]]]]
  ["[0]" //// "_"
   ["/" bundle]
   ["/[1]" // "_"
    ["[0]" extension]
    [generation
     [extension {"+" [Nullary Unary Binary Trinary
                      nullary unary binary trinary]}]
     ["[0]" reference]
     ["//" php "_"
      ["[1][0]" runtime {"+" [Operation Phase Handler Bundle Generator]}]
      ["[1][0]" case]]]
    [//
     ["[0]" synthesis {"+" [%synthesis]}]
     ["[0]" generation]
     [///
      ["[1]" phase]]]]])

(def: .public (custom [parser handler])
  (All (_ s)
    (-> [(Parser s)
         (-> Text (Generator s))]
        Handler))
  (function (_ extension_name phase archive input)
    (case (<s>.result parser input)
      {#try.Success input'}
      (handler extension_name phase archive input')

      {#try.Failure error}
      (/////.except extension.invalid_syntax [extension_name %synthesis input]))))

(template: (!unary function)
  (|>> list _.apply/* (|> (_.constant function))))

... TODO: Get rid of this ASAP
(def: lux::syntax_char_case!
  (..custom [($_ <>.and
                 <s>.any
                 <s>.any
                 (<>.some (<s>.tuple ($_ <>.and
                                         (<s>.tuple (<>.many <s>.i64))
                                         <s>.any))))
             (function (_ extension_name phase archive [input else conditionals])
               (do [! /////.monad]
                 [inputG (phase archive input)
                  [[context_module context_artifact] elseG] (generation.with_new_context archive
                                                              (phase archive else))
                  @input (\ ! each _.var (generation.identifier "input"))
                  conditionalsG (: (Operation (List [Expression Expression]))
                                   (monad.each ! (function (_ [chars branch])
                                                   (do !
                                                     [branchG (phase archive branch)]
                                                     (in [(|> chars
                                                              (list\each (|>> .int _.int (_.=== @input)))
                                                              (list\mix (function (_ clause total)
                                                                          (if (same? _.null total)
                                                                            clause
                                                                            (_.or clause total)))
                                                                        _.null))
                                                          branchG])))
                                               conditionals))
                  .let [foreigns (|> conditionals
                                     (list\each (|>> product.right synthesis.path/then //case.dependencies))
                                     (list& (//case.dependencies (synthesis.path/then else)))
                                     list.together
                                     (set.of_list _.hash)
                                     set.list)
                        @expression (_.constant (reference.artifact [context_module context_artifact]))
                        directive (_.define_function @expression (list& (_.parameter @input) (list\each _.reference foreigns))
                                                     (list\mix (function (_ [test then] else)
                                                                 (_.if test (_.return then) else))
                                                               (_.return elseG)
                                                               conditionalsG))]
                  _ (generation.execute! directive)
                  _ (generation.save! context_artifact directive)]
                 (in (_.apply/* (list& inputG foreigns) @expression))))]))

(def: lux_procs
  Bundle
  (|> /.empty
      (/.install "syntax char case!" lux::syntax_char_case!)
      (/.install "is" (binary (product.uncurried _.===)))
      (/.install "try" (unary //runtime.lux//try))
      ))

(def: (left_shifted [parameter subject])
  (Binary Expression)
  (_.bit_shl (_.% (_.int +64) parameter) subject))

(def: i64_procs
  Bundle
  (<| (/.prefix "i64")
      (|> /.empty
          (/.install "and" (binary (product.uncurried _.bit_and)))
          (/.install "or" (binary (product.uncurried _.bit_or)))
          (/.install "xor" (binary (product.uncurried _.bit_xor)))
          (/.install "left-shift" (binary ..left_shifted))
          (/.install "right-shift" (binary (product.uncurried //runtime.i64//right_shifted)))
          (/.install "=" (binary (product.uncurried _.==)))
          (/.install "<" (binary (product.uncurried _.<)))
          (/.install "+" (binary (product.uncurried //runtime.i64//+)))
          (/.install "-" (binary (product.uncurried //runtime.i64//-)))
          (/.install "*" (binary (product.uncurried //runtime.i64//*)))
          (/.install "/" (binary (function (_ [parameter subject])
                                   (_.intdiv/2 [subject parameter]))))
          (/.install "%" (binary (product.uncurried _.%)))
          (/.install "f64" (unary (_./ (_.float +1.0))))
          (/.install "char" (unary //runtime.i64//char))
          )))

(def: (f64//% [parameter subject])
  (Binary Expression)
  (_.fmod/2 [subject parameter]))

(def: (f64//encode subject)
  (Unary Expression)
  (_.number_format/2 [subject (_.int +17)]))

(def: f64_procs
  Bundle
  (<| (/.prefix "f64")
      (|> /.empty
          (/.install "=" (binary (product.uncurried _.==)))
          (/.install "<" (binary (product.uncurried _.<)))
          (/.install "+" (binary (product.uncurried _.+)))
          (/.install "-" (binary (product.uncurried _.-)))
          (/.install "*" (binary (product.uncurried _.*)))
          (/.install "/" (binary (product.uncurried _./)))
          (/.install "%" (binary ..f64//%))
          (/.install "i64" (unary _.intval/1))
          (/.install "encode" (unary ..f64//encode))
          (/.install "decode" (unary //runtime.f64//decode)))))

(def: (text//clip [paramO extraO subjectO])
  (Trinary Expression)
  (//runtime.text//clip paramO extraO subjectO))

(def: (text//index [startO partO textO])
  (Trinary Expression)
  (//runtime.text//index textO partO startO))

(def: text_procs
  Bundle
  (<| (/.prefix "text")
      (|> /.empty
          (/.install "=" (binary (product.uncurried _.==)))
          (/.install "<" (binary (product.uncurried _.<)))
          (/.install "concat" (binary (product.uncurried (function.flipped _.concat))))
          (/.install "index" (trinary ..text//index))
          (/.install "size" (unary //runtime.text//size))
          (/.install "char" (binary (product.uncurried //runtime.text//char)))
          (/.install "clip" (trinary ..text//clip))
          )))

(def: io_procs
  Bundle
  (<| (/.prefix "io")
      (|> /.empty
          (/.install "log" (unary //runtime.io//log!))
          (/.install "error" (unary //runtime.io//throw!)))))

(def: .public bundle
  Bundle
  (<| (/.prefix "lux")
      (|> /.empty
          (dictionary.merged lux_procs)
          (dictionary.merged i64_procs)
          (dictionary.merged f64_procs)
          (dictionary.merged text_procs)
          (dictionary.merged io_procs))))