aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/php/common.lux
blob: 7dbc8bacc6d8a1259f1bafe658436ffafd6506ac (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
(.module:
  [lux #*
   [abstract
    ["." monad (#+ do)]]
   [control
    ["." function]
    ["." try]
    ["<>" parser
     ["<s>" synthesis (#+ Parser)]]]
   [data
    ["." product]
    ["." text
     ["%" format (#+ format)]]
    [collection
     ["." dictionary]
     ["." set]
     ["." list ("#\." functor fold)]]]
   [math
    [number
     ["f" frac]]]
   ["@" target
    ["_" php (#+ Expression)]]]
  ["." //// #_
   ["/" bundle]
   ["/#" // #_
    ["." extension]
    [generation
     [extension (#+ Nullary Unary Binary Trinary
                    nullary unary binary trinary)]
     ["." reference]
     ["//" php #_
      ["#." runtime (#+ Operation Phase Handler Bundle Generator)]
      ["#." case]]]
    [//
     ["." synthesis (#+ %synthesis)]
     ["." generation]
     [///
      ["#" phase]]]]])

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

      (#try.Failure error)
      (/////.throw 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 (\ ! map _.var (generation.gensym "input"))
                  conditionalsG (: (Operation (List [Expression Expression]))
                                   (monad.map ! (function (_ [chars branch])
                                                  (do !
                                                    [branchG (phase archive branch)]
                                                    (wrap [(|> chars
                                                               (list\map (|>> .int _.int (_.=== @input)))
                                                               (list\fold (function (_ clause total)
                                                                            (if (is? _.null total)
                                                                              clause
                                                                              (_.or clause total)))
                                                                          _.null))
                                                           branchG])))
                                              conditionals))
                  #let [foreigns (|> conditionals
                                     (list\map (|>> product.right synthesis.path/then //case.dependencies))
                                     (list& (//case.dependencies (synthesis.path/then else)))
                                     list.concat
                                     (set.from_list _.hash)
                                     set.to_list)
                        @expression (_.constant (reference.artifact [context_module context_artifact]))
                        directive (_.define_function @expression (list& (_.parameter @input) (list\map _.reference foreigns))
                                                     (list\fold (function (_ [test then] else)
                                                                  (_.if test (_.return then) else))
                                                                (_.return elseG)
                                                                conditionalsG))]
                  _ (generation.execute! directive)
                  _ (generation.save! (%.nat context_artifact) directive)]
                 (wrap (_.apply/* (list& inputG foreigns) @expression))))]))

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

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

(def: (f64//% [parameter subject])
  (Binary Expression)
  (_./ (_.float +1.0) (_.% parameter subject)))

(def: f64_procs
  Bundle
  (<| (/.prefix "f64")
      (|> /.empty
          (/.install "+" (binary (product.uncurry _.+)))
          (/.install "-" (binary (product.uncurry _.-)))
          (/.install "*" (binary (product.uncurry _.*)))
          (/.install "/" (binary (product.uncurry _./)))
          (/.install "%" (binary ..f64//%))
          (/.install "=" (binary (product.uncurry _.==)))
          (/.install "<" (binary (product.uncurry _.<)))
          (/.install "i64" (unary _.intval/1))
          (/.install "encode" (unary _.strval/1))
          (/.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.uncurry _.==)))
          (/.install "<" (binary (product.uncurry _.<)))
          (/.install "concat" (binary (product.uncurry (function.flip _.concat))))
          (/.install "index" (trinary ..text//index))
          (/.install "size" (unary //runtime.text//size))
          (/.install "char" (binary (product.uncurry //runtime.text//char)))
          (/.install "clip" (trinary ..text//clip))
          )))

(def: io//current-time
  (Nullary Expression)
  (|>> _.time/0
       (_.* (_.int +1,000))))

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

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