/
backend.lisp
339 lines (243 loc) · 10.2 KB
/
backend.lisp
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
(load (merge-pathnames "quicklisp/setup.lisp"
(user-homedir-pathname)))
(ql:quickload "cl-json")
(ql:quickload "fare-matcher")
(defpackage niecza (:use common-lisp))
(in-package :niecza)
; Macros
(defun xref-to-symbol (xref) (if (equal (first xref) "MAIN") (xref-to-symbol-real xref) (list 'quote (xref-to-symbol-real xref))))
(defun xref-to-symbol-real (xref) (intern (concatenate 'string "XREF-" (first xref) "-" (write-to-string (second xref)))))
(load "cl-backend/niecza-stash.lisp")
(defun main-xref (i) (xref-to-symbol (list "MAIN" i "...")))
(defmacro get-stash (name) (niecza-stash:to-stash-name name))
(defmacro nam-op (name params &body body) `(defmacro ,(concat-symbol 'nam- name) ,params ,@body))
(defun concat-symbol (a b) (intern (concatenate 'string (string a) (string b))))
; Translation to symbols
(defun method-name (name) (intern name))
(defun var-name (name) (intern name))
; Hacks
;(nam-op ehspan (class name unused start end goto) )
;(nam-op span (n1 n2 sync body) body)
(nam-op xspan (n1 n2 flag body &rest rest) body)
(defun strip-ann (thing)
(if (consp thing)
(if (eq (first thing) 'nam-ann)
(strip-ann (fourth thing))
(mapcar #'strip-ann thing)
)
thing))
(defun to-symbol-first (thing)
(if (stringp thing)
(intern (string-upcase (concatenate 'string "nam-" thing)))
(to-symbol thing)))
(defun to-symbol (thing)
(if (consp thing)
(cons (to-symbol-first (first thing)) (mapcar #'to-symbol (rest thing)))
thing))
; P6 Classes
(defun make-scalar (value) (let ((scalar (make-instance 'p6-Scalar)))
(setf (slot-value scalar 'value) value)
scalar))
(defclass p6-Mu () ())
(defgeneric |new| (invocant &rest rest))
(defmethod |new| (invocant &rest rest) (make-instance (class-of invocant)))
(defclass p6-Scalar () (value))
(defgeneric |FETCH| (value))
(defmethod |FETCH| ((container p6-Scalar)) (slot-value container 'value))
(defmethod |STORE| ((container p6-Scalar) value) (setf (slot-value container 'value)value))
(defmethod |FETCH| (thing) thing)
(defun compile-param (param)
(fare-matcher:match param
((list
name ; For binding error messages
flags ; See doc/nam.pod
slot ; Name of lexical to accept value
names ; All legal named-parameter names
default ; Xref Sub to call if HAS_DEFAULT; must be child of this
) (if (equal flags 96)
(if slot
(var-name slot)
(gensym))
nil))))
(defun mymap (func list) (remove-if #'null (mapcar func list)))
(defvar preinit)
(setf preinit '())
(defmacro define-nam-sub
(i ; The Xref Id
name ; Sub's name for backtraces
outer_xref ; OUTER:: sub, may be in a setting unit
flags ; See doc/nam.pod
children ; Supports tree traversals
param_role_hack ; See doc/nam.pod
augment_hack ; See doc/nam.pod
hint_hack ; See doc/nam.pod
is_phaser ; See doc/nam.pod
body_of ; Only valid in immediate block of class {} et al
in_class ; Innermost enclosing body_of
cur_pkg ; OUR:: as a list of names
class ; &?BLOCK.WHAT; "Sub" or "Regex"
ltm ; Only for regexes; stores declarative prefix
exports ; List of global names
signature ; May be null in exotic cases
lexicals ; Come in multiple forms
nam ; See description of opcodes earlier
)
(if hint_hack
(let ((var (hint-var (xref-to-symbol (first hint_hack)) (second hint_hack))))
(eval `(defvar ,var))
(setf preinit (append `((setf ,var (,(main-xref i)))) preinit))
))
`(defun ,(main-xref i)
,(mymap #'compile-param signature)
(let ,(lexicals-to-let (main-xref i) lexicals) ,@nam)))
(defun xref-to-subsymbol (xref) (main-xref (cadr xref)))
; HACK
(defmacro define-nam-module (
i
name ; The object's debug name
exports ; List of global names to which object is bound
) `(setf ,(main-xref i) 'placeholder))
(defmacro define-nam-class (
i
name ; The object's debug name
exports ; List of global names to which object is bound
attributes ; Attributes local to the class
methods ; Methods local to the class
superclasses ; Direct superclasses of the class
linear_mro ; All superclasses in C3 order
)
(let ((class (main-xref i)))
`(progn
(defclass ,class (p6-Mu) ())
,@(mapcar (lambda (m) (compile-method class m)) methods)
(setf ,(main-xref i) (make-instance ',(main-xref i)))
)))
(defun method-name (name) (intern name))
(defun compile-method (class method)
(fare-matcher:match method
((and (list
name ; Method name without ! decorator
kind ; Allowable kinds are "normal", "private", and "sub"
var ; Variable for implementing sub in param role
body ; Reference to implementing sub
) (when (equal kind "normal")))
`(defmethod ,(method-name name) ((invocant ,class) &rest rest) (apply ',(xref-to-subsymbol body) invocant rest)))))
(defun hint-var (xref name)
(intern (concatenate 'string "HINT-" (string xref) "-" name)))
; converts one lexical to a variable declaration for a let
(defun lexical-to-let (xref lexical)
(fare-matcher:match lexical
((and (list var sub dunno-1 id dunno-2) (when (equal sub "sub"))) (list (var-name var) `(symbol-function ',(main-xref id))))
((and (list var simple flags) (when (equal simple "simple")))
(if (equal flags 4)
nil
(list (var-name var) (make-scalar ""))))
((and (list* var stash path) (when (equal stash "stash")))
(list (var-name var) `(get-stash ,path)))
((and (list var hint) (when (equal hint "hint")))
(list (var-name var) (hint-var xref var)))
((and (list* var common path) (when (equal common "common")))
(list (var-name var) (niecza-stash:to-stash-name path)))))
; converts a list of lexicals
(defun lexicals-to-let (xref lexicals)
(remove-if #'null (mapcar (lambda (x) (lexical-to-let xref x)) lexicals)))
(nam-op ann (filename line op) op)
(nam-op prog (&body ops) `(progn ,@ops))
(defun nam-sink (argument) nil)
(defun nam-str (string) string)
(defun nam-double (number) number)
(nam-op assign (to what) `(STORE ,to ,what))
(defun nam-const (thing) thing)
(defun nam-box (type thing) thing)
(defun nam-fetch (thing) (FETCH thing))
(nam-op letvar (&rest args) `(nam-scopedlex ,@args))
(nam-op scopedlex (var &rest rvalue)
(if (consp rvalue)
`(setf ,(var-name var) ,@rvalue)
(var-name var)))
(labels
((seperate (mixed)
(if (stringp (first mixed))
(let ((result (seperate (rest (rest mixed)))))
(list
(cons (list (var-name (first mixed)) (second mixed)) (first result))
(second result))
)
(list nil mixed))))
(nam-op letn (&body vars-and-body)
(let ((seperated (seperate vars-and-body)))
`(let* ,(first seperated) ,@(second seperated)))))
; ???
(defun nam-subcall (dunno-what-that-is thing &rest args) (apply thing args))
; TODO: check if this needs to be optimised
(labels
((known (arg) `(quote ,(intern arg)))
(to-method (arg) (if (stringp arg)
(known arg)
(if (eq (first arg) 'nam-str)
(known (second arg))
`(intern ,arg)
))))
(nam-op methodcall (method-name dunno invocant &rest args)
`(apply ,(to-method method-name) (list (FETCH ,(first args)) ,@(rest args)))))
(defun nam-obj_getbool (obj) (if (numberp obj) (not (equal obj 0)) obj))
(defun nam-bif_str (obj) (FETCH obj))
(defun nam-obj_getstr (obj) obj)
(nam-op ternary (cond if then) `(if ,cond ,if ,then))
(defun nam-null (type) nil)
; HACK
(nam-op bool (val) val)
; HACK
(nam-op corelex (var) `(nam-scopedlex ,var))
(nam-op newboundvar (dunno1 dunno2 thing) thing)
(defun compile-sub-or-packagoid (i def)
(let* ((type (first def))
(args (if (equal type "sub") (append (butlast (rest def)) (list (to-symbol-first (last def)))) (rest def))))
`(
,(intern (string-upcase (concatenate 'string "define-nam-" type)))
,i
,@args
)))
(defun compile-unit (nam)
(fare-matcher:match nam
((list
mainline_ref ; Xref to mainline subroutine
name ; Unit's unique name
log ; Mostly unused vestige of last stash system
setting ; Name of setting unit or null
bottom_ref ; Xref to sub containing {YOU_ARE_HERE}, or null
filename ; Filename of source code or null
modtime ; Seconds since 1970-01-01
xref ; Resolves refs from other units
tdeps ; Holds dependency data for recompilation
stash_root ; Trie holding classes and global variables
)
(list (niecza-stash:wrap-in-let stash_root
(loop for thing in xref for i upfrom 0 when thing collect (compile-sub-or-packagoid i thing)))))))
(defun print-thing (thing) (format t "~A" (FETCH thing)))
(defun p6-say (&rest things) (mapcar #'print-thing things) (format t "~%"))
(defun p6-concat (&rest things) (apply 'concatenate 'string (mapcar #'FETCH things)))
; BIF
(defun nam-bif_plus (a b) (+ (FETCH a) (FETCH b)))
(defun nam-bif_minus (a b) (- (FETCH a) (FETCH b)))
(defun nam-bif_mul (a b) (* (FETCH a) (FETCH b)))
(defun nam-bif_divide (a b) (/ (FETCH a) (FETCH b)))
(defun nam-bif_numgt (a b) (> (FETCH a) (FETCH b)))
(defun nam-bif_numlt (a b) (< (FETCH a) (FETCH b)))
(defun nam-bif_numeq (a b) (= (FETCH a) (FETCH b)))
(nam-op whileloop (until once cond body) `(loop while ,cond do ,body))
(defun nam-label (label))
(defun wrap-for-eval (compiled-unit)
`(let ((|&infix:<~>| #'p6-concat)
(|&say| #'p6-say)
(|Nil| "") ; HACK
(|Any| "") ; HACK
)
,@compiled-unit (eval `(progn ,@preinit)) (,(main-xref 0))))
(let ((compiled-unit (compile-unit (json:decode-json (open (first common-lisp-user::*args*))))))
;(format t "~w~%~%~%" (json:decode-json (open (first *args*))))
;(format t "~w~%~%~%" compiled-unit)
(format t "--------~%~%~w~%~%~%" (strip-ann compiled-unit))
(let ((wrapped (wrap-for-eval compiled-unit)))
(eval wrapped)
))