aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/tool/compiler/phase/generation/common-lisp/runtime.lux
blob: 843db713d65a5a6df183fdb60f34a6dc582dc79c (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
(.module:
  [lux (#- inc)
   [abstract
    [monad (#+ do)]]
   [control
    ["." function]
    ["p" parser
     ["s" code]]]
   [data
    [number (#+ hex)
     ["." i64]]
    ["." text
     format]
    [collection
     ["." list ("#@." functor)]]]
   ["." macro
    ["." code]
    [syntax (#+ syntax:)]]
   [target
    ["_" common-lisp (#+ Expression Var/1 Computation Literal)]]]
  ["." ///
   ["//." //
    [//
     ["/////." name]
     ["." synthesis]]]]
  )

(template [<name> <base>]
  [(type: #export <name>
     (<base> Var/1 (Expression Any) (Expression Any)))]

  [Operation ///.Operation]
  [Phase ///.Phase]
  [Handler ///.Handler]
  [Bundle ///.Bundle]
  )

(def: prefix "LuxRuntime")

(def: #export unit (_.string synthesis.unit))

(def: (flag value)
  (-> Bit Literal)
  (if value
    (_.string "")
    _.nil))

(def: (variant' tag last? value)
  (-> (Expression Any) (Expression Any) (Expression Any) (Computation Any))
  (_.list/* (list tag last? value)))

(def: #export (variant tag last? value)
  (-> Nat Bit (Expression Any) (Computation Any))
  (variant' (_.int (.int tag))
            (flag last?)
            value))

(def: #export none
  (Computation Any)
  (..variant 0 false ..unit))

(def: #export some
  (-> (Expression Any) (Computation Any))
  (..variant 1 true))

(def: #export left
  (-> (Expression Any) (Computation Any))
  (..variant 0 false))

(def: #export right
  (-> (Expression Any) (Computation Any))
  (..variant 1 true))

(def: runtime-name
  (-> Text Var/1)
  (|>> /////name.normalize
       (format ..prefix "_")
       _.var))

(def: (feature name definition)
  (-> Var/1 (-> Var/1 (Expression Any)) (Expression Any))
  (definition name))

(syntax: #export (with-vars {vars (s.tuple (p.some s.local-identifier))}
                   body)
  (wrap (list (` (let [(~+ (|> vars
                               (list@map (function (_ var)
                                           (list (code.local-identifier var)
                                                 (` (_.var (~ (code.text (/////name.normalize var))))))))
                               list.concat))]
                   (~ body))))))

(syntax: (runtime: {declaration (p.or s.local-identifier
                                      (s.form (p.and s.local-identifier
                                                     (p.some s.local-identifier))))}
           code)
  (macro.with-gensyms [g!_ g!L]
    (case declaration
      (#.Left name)
      (let [code-nameC (code.local-identifier (format "@" name))
            runtime-nameC (` (runtime-name (~ (code.text name))))]
        (wrap (list (` (def: #export (~ (code.local-identifier name)) _.Var/1 (~ runtime-nameC)))
                    (` (def: (~ code-nameC)
                         (_.Expression Any)
                         (..feature (~ runtime-nameC)
                                    (function ((~ g!_) (~ g!L))
                                      (_.defparameter (~ g!L) (~ code)))))))))
      
      (#.Right [name inputs])
      (let [code-nameC (code.local-identifier (format "@" name))
            runtime-nameC (` (runtime-name (~ (code.text name))))
            inputsC (list@map code.local-identifier inputs)
            inputs-typesC (list@map (function.constant (` (_.Expression Any)))
                                    inputs)]
        (wrap (list (` (def: #export ((~ (code.local-identifier name)) (~+ inputsC))
                         (-> (~+ inputs-typesC) (_.Computation Any))
                         (_.call/* (~ runtime-nameC) (list (~+ inputsC)))))
                    (` (def: (~ code-nameC)
                         (_.Expression Any)
                         (..feature (~ runtime-nameC)
                                    (function ((~ g!_) (~ g!L))
                                      (..with-vars [(~+ inputsC)]
                                        (_.defun (~ g!L) (_.args (list (~+ inputsC)))
                                                 (~ code)))))))))))))

(runtime: (lux//try op)
  (with-vars [error]
    (_.handler-case
     (list [(_.bool true) error
            (..left (_.format/3 [_.nil (_.string "~A") error]))])
     (..right (_.funcall/+ [op (list ..unit)])))))

## TODO: Use Common Lisp's swiss-army loop macro instead.
(runtime: (lux//program-args inputs)
  (with-vars [loop input tail]
    (_.labels (list [loop [(_.args (list input tail))
                           (_.if (_.null/1 input)
                             tail
                             (_.funcall/+ [(_.function/1 loop)
                                           (list (_.cdr/1 input)
                                                 (..some (_.vector/* (list (_.car/1 input) tail))))]))]])
              (_.funcall/+ [(_.function/1 loop)
                            (list (_.reverse/1 inputs)
                                  ..none)]))))

(def: runtime//lux
  ($_ _.progn
      @lux//try
      @lux//program-args))

(def: last-index
  (|>> _.length/1 (_.- (_.int +1))))

(with-expansions [<recur> (as-is ($_ _.then
                                     (_.; (_.set lefts (_.- last-index-right lefts)))
                                     (_.; (_.set tuple (_.nth last-index-right tuple)))))]
  (template: (!recur <side>)
    (<side> (|> lefts (_.- last-index-right))
            (_.elt/2 [tuple last-index-right])))
  
  (runtime: (tuple//left lefts tuple)
    (with-vars [last-index-right]
      (_.let (list [last-index-right (..last-index tuple)])
        (_.if (_.> lefts last-index-right)
          ## No need for recursion
          (_.elt/2 [tuple lefts])
          ## Needs recursion
          (!recur tuple//left)))))

  (runtime: (tuple//right lefts tuple)
    (with-vars [last-index-right right-index]
      (_.let (list [last-index-right (..last-index tuple)]
                   [right-index (_.+ (_.int +1) lefts)])
        (_.cond (list [(_.= last-index-right right-index)
                       (_.elt/2 [tuple right-index])]
                      [(_.> last-index-right right-index)
                       ## Needs recursion.
                       (!recur tuple//right)])
                (_.subseq/3 [tuple right-index (_.length/1 tuple)]))
        ))))

## TODO: Find a way to extract parts of the sum without "nth", which
## does a linear search, and is thus expensive.
(runtime: (sum//get sum wantsLast wantedTag)
  (with-vars [sum-tag sum-flag]
    (let [@exit (_.label "exit")
          return! (_.return-from @exit)
          no-match! (return! sum)
          sum-value (_.nth/2 [(_.int +2) sum])
          test-recursion! (_.if sum-flag
                            ## Must iterate.
                            ($_ _.progn
                                (_.setq sum sum-value)
                                (_.setq wantedTag (_.- sum-tag wantedTag)))
                            no-match!)]
      (<| (_.progn (_.setq sum-tag (_.nth/2 [(_.int +0) sum])))
          (_.progn (_.setq sum-flag (_.nth/2 [(_.int +1) sum])))
          (_.block @exit)
          (_.while (_.bool true))
          (_.cond (list [(_.= sum-tag wantedTag)
                         (_.if (_.equal wantsLast sum-flag)
                           (return! sum-value)
                           test-recursion!)]

                        [(_.> sum-tag wantedTag)
                         test-recursion!]

                        [(_.and (_.< sum-tag wantedTag)
                                wantsLast)
                         (return! (variant' (_.- wantedTag sum-tag) sum-flag sum-value))])

                  no-match!)))))

(def: runtime//adt
  ($_ _.progn
      @tuple//left
      @tuple//right
      @sum//get))

(runtime: (i64//logic-right-shift shift input)
  (_.if (_.= (_.int +0) shift)
    input
    (|> input
        (_.ash (_.* (_.int -1) shift))
        (_.logand (_.int (hex "+7FFFFFFFFFFFFFFF"))))))

(def: runtime//i64
  ($_ _.progn
      @i64//logic-right-shift))

(runtime: (text//clip from to text)
  (_.subseq/3 [text from to]))

(runtime: (text//index reference start space)
  (with-vars [index]
    (_.let (list [index (_.search/3 [reference space start])])
      (_.if index
        (..some index)
        ..none))))

(def: runtime//text
  ($_ _.progn
      @text//index
      @text//clip))

(runtime: (io//exit code)
  ($_ _.progn
      (_.conditional+ (list "sbcl")
                      (_.call/* (_.var "sb-ext:quit") (list code)))
      (_.conditional+ (list "clisp")
                      (_.call/* (_.var "ext:exit") (list code)))
      (_.conditional+ (list "ccl")
                      (_.call/* (_.var "ccl:quit") (list code)))
      (_.conditional+ (list "allegro")
                      (_.call/* (_.var "excl:exit") (list code)))
      (_.call/* (_.var "cl-user::quit") (list code))))

(runtime: (io//current-time _)
  (|> (_.get-universal-time/0 [])
      (_.* (_.int +1,000))))

(def: runtime//io
  ($_ _.progn
      @io//exit
      @io//current-time))

(def: runtime
  ($_ _.progn
      runtime//adt
      runtime//lux
      runtime//i64
      runtime//text
      runtime//io))

(def: #export artifact ..prefix)

(def: #export generate
  (Operation Any)
  (///.with-buffer
    (do ////.monad
      [_ (///.save! true ["" ..prefix]
                    ..runtime)]
      (///.save-buffer! ..artifact))))