-
Notifications
You must be signed in to change notification settings - Fork 1
/
cxx.clj
429 lines (369 loc) · 13.6 KB
/
cxx.clj
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
(defmacro malli-fns []
(def maps-to-vector
(fn [m]
(cond
(map? m) (maps-to-vector (cons :map m))
(coll? m) (mapv maps-to-vector m)
:else m)))
(def remove-kw-ns
(fn [m]
(cond
(vector? m) (mapv remove-kw-ns m)
(qualified-keyword? m) (keyword "lisc" (name m))
:else m)))
(def vector-to-maps
(fn [m]
(cond
(and (vector? m) (= :rtm (first m)))
m
(and (vector? m) (= :map (first m)))
(into (hash-map) (vector-to-maps (rest m)))
(coll? m)
(mapv vector-to-maps m)
:else
m)))
(def malli-to-map (comp vector-to-maps remove-kw-ns maps-to-vector))
nil)
(malli-fns)
(defmacro type-fns []
(def malli-types (volatile! (hash-map)))
(def m-set-types-raw
(fn [t]
(vreset! malli-types (malli-to-map t))))
(def m-add-type-raw
(fn [path t]
(let [sub-type (first t)
lasttwo (take-last 2 t)
ret-arg (if (= (first lasttwo) :->)
[(last t) (rest (drop-last 2 t))]
[:nil (rest t)])
malli-t (concat (vector :cat)
(second ret-arg)
(when-not (= :nil (first ret-arg))
(vector [:= (first ret-arg)])))]
(vswap! malli-types
assoc-in
(concat path (list sub-type))
malli-t))))
(def add-type-rtm-cxx
(fn [path]
(vswap! malli-types update-in (butlast path)
(fn [x] (assoc x :default (get x (last path)))))))
nil)
(type-fns)
(defmacro Ts-default [path]
(add-type-rtm-cxx path)
nil)
(defmacro m-load-types [str-types str-defaults]
(m-set-types-raw (read-string (slurp str-types)))
(run! add-type-rtm-cxx (read-string (slurp str-defaults)))
nil)
(defmacro m-add-type [path t]
(m-add-type-raw path t)
nil)
(defmacro class-fns []
(def make-syms
(fn [s n]
(mapv #(symbol (str s "_" %)) (range n))))
(def obj-encode
(fn [s p]
(str "__result = rt::dense_list(obj<string>(\"" s "\"), obj<pointer>(" p "))")))
(def obj-decode
(fn [dense-list]
(str "sequence::to<std_vector>(" dense-list ")[1]")))
(def cvt-from-c
(fn [t v]
(cond
(= t :string) (str "obj<string>(" v ")")
(= t :pointer) (str "obj<pointer>(" v ")")
(= t :double) (str "obj<number>(" v ")")
(and (vector? t) (= :vector (first t)))
(str "obj<array_seq<"
(name (last t))
", number>>("
v
", size_t("
(get (second t) :max)
"))")
:else v)))
(def argslist
(fn [strs]
(str "(" (apply str (interpose ", " strs)) ")")))
(def given-value
(fn [typ args]
(when (and (vector? typ) (= :enum (first typ)))
(if (= :args (last typ))
(nth args (second typ))
(second typ)))))
(def cvts-to-c
(fn [t v]
(cond
(= t :string) (str "string::to<std::string>(" v ").c_str()")
(= t :int) (str "number::to<std::int32_t>(" v ")")
(= t :double) (str "number::to<double>(" v ")")
(= t :sequence) (str "sequence::to<std_vector>(" v ")")
:else v)))
(def c-lambdabody
(fn [funname signature]
(str "return "
(cvts-to-c (first signature)
(str "run"
(argslist
(cons
funname
(map cvt-from-c
(rest signature)
(make-syms "b" (dec (count signature))))))))
";")))
(def m-c-lambda
(fn [varname malli-signature]
(let [signature (cons (second (last malli-signature))
(butlast (rest malli-signature)))
funargs (make-syms "b" (dec (count signature)))
argstypes (map (fn [e] (if (vector? e) (str (name (last e)) "*")
(name e)))
(rest signature))
combined (map (fn [t v] (str t " " v)) argstypes funargs)]
(str "[" varname "] " (argslist combined) " -> " (name (first signature))
" {" (c-lambdabody varname signature) "}"))))
(def get-malli-types
(fn [path] (get-in (deref malli-types) path)))
(def get-ct-malli-types
(fn [path]
(let [scheme (get-malli-types path)]
(if (map? scheme) (get scheme :cxx) scheme))))
(def cvt-to-c
(fn [args]
(fn [t v]
(let [funspec (get-ct-malli-types [:registry t])]
(cond
(given-value t args)
(str (given-value t args))
(and (vector? funspec) (= [:= :double] (last funspec)))
(m-c-lambda v funspec)
:else
(cvts-to-c t v))))))
(def wrap-result
(fn [t s]
(str "__result = " (cvt-from-c t s))))
(def new-raw
(fn [class args]
(let [m-c-sub (or (first args) :default)
m-contypes (next (get-ct-malli-types [(keyword class) m-c-sub]))
m-funargs (->> m-contypes count (make-syms "a"))
m-codestr (str "new "
(name class)
(argslist (map (cvt-to-c args) m-contypes m-funargs)))
real-funargs (mapv second
(remove (fn [x] (given-value (first x) args))
(map vector m-contypes m-funargs)))
m-funcode (list 'fn real-funargs (obj-encode (name class) m-codestr))
m-erg (vector (if (seq real-funargs) :fn
(if (= m-c-sub :default) :new-no-args :fn))
m-funcode)]
m-erg)))
(def call-raw
(fn [class method args]
(let [m-m-sub (or (first args) :default)
m-types (get-ct-malli-types
[(keyword class) (keyword method) m-m-sub])
m-funtypes (next m-types)
m-lasttwo (take-last 2 m-funtypes)
m-ret-arg (if (and (vector? (last m-funtypes))
(= := (first (last m-funtypes))))
[(second (last m-funtypes)) (butlast m-funtypes)]
[:nil m-funtypes])
m-arg-types (second m-ret-arg)
m-arg-symbols (->> m-arg-types count inc (make-syms "a"))
m-codestr (str "pointer::to_pointer<"
(name class)
">("
(obj-decode (first m-arg-symbols))
")->"
(name method)
(argslist (map (cvt-to-c args) m-arg-types (rest m-arg-symbols))))
m-erg (list 'fn m-arg-symbols
(if (= (first m-ret-arg) :nil)
m-codestr
(wrap-result
(first m-ret-arg)
m-codestr)))]
(vector :fn m-erg))))
(def stri
(fn [x]
(if (coll? x) (cons 'list (map stri x))
(str x))))
(def vector-to-list
(fn [fun]
(fn [x]
(cond
(nil? x)
"nil"
(map? x)
(into (hash-map)
(map (fn [a] (vector (str (second a)) (nth a 2)))
(map (vector-to-list fun) x)))
(coll? x)
(cons 'list (map (vector-to-list fun) x))
:else (fun x)))))
(def construct-call
(fn [sym-fun sym-args macargs c-function m-data]
(list sym-fun
(stri macargs)
((vector-to-list identity) m-data)
((vector-to-list str) m-data)
sym-args)))
(def interop-fn
(fn [macargs c-function m-data]
(list 'fn [(symbol "&") 'args]
(list
(list 'fn ['x]
(list 'if 'x 'x
(list 'apply (second c-function)
(construct-call 'transform 'args macargs c-function m-data))))
(construct-call 'checkit 'args macargs c-function m-data)))))
(def interop-fn-direct
(fn [macargs c-function m-data]
(cond
(= :new-no-args (first c-function))
(list
(list 'fn ['x]
(list 'if 'x 'x (list (second c-function))))
(construct-call 'checkit (list 'list)
macargs c-function m-data))
:else
(interop-fn macargs c-function m-data))))
(def interop-vec
(fn [macargs]
(let [method (first macargs)
class (second macargs)
types (first (nnext macargs))
m-types-kw (if (vector? types) :default (or types :default))
r (next (nnext macargs))]
(do
(cond
(and (vector? types) (= (symbol "new") method))
(m-add-type-raw (list (keyword class)) (map keyword (cons m-types-kw types)))
(vector? types)
(m-add-type-raw (map keyword [class method]) (map keyword (cons m-types-kw types))))
(let [m-data (if (= (symbol "new") method)
(get-malli-types [(keyword class) m-types-kw])
(get-malli-types [(keyword class)
(keyword method)
m-types-kw]))
c-function (cond
(= (symbol "bless") method)
(vector :fn 'identity)
(= (symbol "new") method)
(new-raw class (cons m-types-kw r))
:else
(call-raw class method (cons m-types-kw r)))]
(vector macargs c-function m-data))))))
(def interop-flat
(fn [macargs]
(apply interop-fn (interop-vec macargs))))
(def interop
(fn [macargs]
(apply interop-fn-direct (interop-vec macargs))))
(def _doto
(fn [args]
(let [frt (first args)
frt1 (if (= (symbol "new") (first frt)) (list frt) frt)
hack-frt (cons (first frt1) (map (fn [x] (list 'identity x))
(rest frt1)))
;; hack for https://github.com/nakkaya/ferret/issues/52
a (cons hack-frt (rest args))
b (map (fn [x] (if-not (coll? x) (list x) x))
a)
c (map (fn [x] (if-not (coll? (first x))
(cons (list (first x)) (rest x))
x))
b)
class (second (ffirst c))
d (cons (interop-flat (ffirst c)) (rest (first c)))
e (map (fn [x] (cons (interop-flat (concat (list (ffirst x) class)
(rest (first x))))
(rest x)))
(rest c))
f (cons d e)
erg (cons 'doto f)]
erg)))
nil)
(class-fns)
(defn transform [macargs raw-types str-types args]
(let [rs (when (and (not (= "nil" str-types))
(get str-types ":rtm"))
(get raw-types ":rtm"))]
(if rs
(cons (first args)
(map (fn [t] (get (second args) (first t)))
(rest rs)))
args)))
(defn not-double? [v]
(and (not (zero? v)) (zero? (dec (inc v)))))
(defn not-int? [v]
(not= (floor v) v))
(defn check-value [type v]
(list
(cond
(= type ":double") (if (not-double? v) "-" "+")
(= type ":int") (if (not-int? v) "-" "+")
(= type ":lisc/pos-int") (if (or (not-int? v) (not (pos? v))) "-" "+")
(= type ":lisc/pos") (if (not (pos? v)) "-" "+")
(= type ":lisc/one-letter") (if-not (and (string? v) (= 1 (count v))) "-" "+")
(= type ":string") (if-not (string? v) "-" "+")
:else "?")
type v))
(defn checkit [macargs raw-types str-types args]
(let [st (when (not (= "nil" str-types)) (get str-types ":rtm"))
rs (when st (get raw-types ":rtm"))
method (first macargs)
dict (second args)
erg (when rs
(cond
(and (not= "new" method) (not= (second macargs) (first (first args))))
(list "-" "wrong class")
(not dict)
(list "-" "no second arg")
:else
(map (fn [r s]
(let [type (second s)
kw (first r)]
(check-value type (get dict kw))))
(rest rs) (rest st))))]
(when (pos? (count (filter (fn [x] (not= (first x) "+")) erg)))
{:mismatch erg})))
(defmacro new [class & args]
(let [fncode (new-raw class args)]
(if (= :new-no-args (first fncode))
(list (second fncode))
(second fncode))))
(defmacro call [class method & args]
(second (call-raw class method args)))
(defmacro defnative [head body]
(list 'native-declare (str head "{return " (eval body) ";}")))
(defmacro _ [& args] (interop args))
(defmacro T [& args] (interop args))
(defmacro > [& args] (_doto args))
(defmacro To [& args] (_doto args))
(defmacro Ts [path cxx & [rtm]]
(if rtm
(vswap! malli-types assoc-in path
(hash-map :rtm (remove-kw-ns (vec (cons :map rtm)))
:cxx (remove-kw-ns (vec (cons :cat cxx)))))
(vswap! malli-types assoc-in path
(remove-kw-ns (vec (cons :cat cxx)))))
nil)
(defmacro def-ys-plus []
(list 'def '+_ '+))
(defmacro def-ys-star []
(list 'def '*_ '*))
(m-load-types "malli_types.edn" "root_defaults.edn")
(comment
(m-load-types "malli_types.edn")
(new-raw 'TF1 [:XR2])
(new-raw 'TCanvas [])
(new-raw 'TF1 [:native 'cpp_nslit])
(def u1 (get-malli-types [:TF1 :SetParameters :default]))
;;
)