-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecifier.lisp
488 lines (469 loc) · 21.1 KB
/
specifier.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
(in-package :lcc)
(defclass specifier ()
((name :initarg :name :accessor name)
(construct :initarg :construct :accessor construct)
(const :initarg :const :accessor const)
(typeof :initarg :typeof :accessor typeof)
(modifier :initarg :modifier :accessor modifier)
(const-ptr :initarg :const-ptr :accessor const-ptr)
(array-def :initarg :array-def :accessor array-def)
(default :initarg :default :accessor default)
(attrs :initarg :attrs :accessor attrs)
(anonymous :initarg :anonymous :initform nil :accessor anonymous)
(body :initform nil :accessor body)
(params :initform nil :accessor params)
(inners :initform nil :accessor inners)))
(defun make-specifier (name construct const typeof modifier const-ptr array-def default attrs &optional (anonymous nil))
(let ((instance (make-instance 'specifier
:name name
:construct construct
:const const
:typeof typeof
:modifier modifier
:const-ptr const-ptr
:array-def array-def
:default default
:attrs attrs
:anonymous anonymous)))
(cond ((eql construct '|@FUNCTION|)
(setf (params instance) (make-hash-table :test 'eql)))
((eql construct '|@ENUM|)
(setf (inners instance) (make-hash-table :test 'eql)))
((eql construct '|@STRUCT|)
(setf (inners instance) (make-hash-table :test 'eql)))
((eql construct '|@UNION|)
(setf (inners instance) (make-hash-table :test 'eql)))
((eql construct '|@CLASS|)
(setf (inners instance) (make-hash-table :test 'eql)))
((eql construct '|@GUARD|)
(setf (inners instance) (make-hash-table :test 'eql)))
((eql construct '|@TARGET|)
(setf (inners instance) (make-hash-table :test 'eql)))
(t t))
instance))
(defun specify (key table value)
(setf (gethash key table) value))
(defun copy-specifiers (table)
(let ((new-table (make-hash-table
:test (hash-table-test table)
:size (hash-table-size table)
:rehash-size (hash-table-rehash-size table)
:rehash-threshold (hash-table-rehash-threshold table))))
(maphash #'(lambda(key value)
(setf (gethash key new-table) value))
table)
new-table))
(defun print-specifier (spec &optional (lvl 0))
(format t
"~A~A ~A ~:[~A ~;~*~]~:[~A ~;~*~]~:[~A ~;~*~]~:[~A ~;~*~]~:[~{~A~} ~;~*~]~:[= ~A ~;~*~]~:[{~{~A~^ ~}}~;~*~]~:[~;A~]~%"
(indent lvl) (construct spec) (name spec) (null (const spec)) (const spec) (null (typeof spec)) (typeof spec)
(null (modifier spec)) (modifier spec) (null (const-ptr spec)) (const-ptr spec)
(null (array-def spec)) (array-def spec) (null (default spec)) (default spec)
(null (attrs spec)) (attrs spec) (anonymous spec))
(let ((params (params spec))
(inners (inners spec)))
(when params (print-specifiers params (+ 1 lvl)))
(when inners (print-specifiers inners (+ 1 lvl)))))
(defun print-specifiers (table &optional (lvl 0))
(maphash #'(lambda (k v) (print-specifier v lvl)) table))
(defun specify-name< (name)
(if (is-name name) (symbol-name name)
(error (format nil "wrong name ~S" name))))
(defun specify-type-name< (name)
(cond ((key-eq name '|uchar|) "unsigned char")
((key-eq name '|ushort|) "unsigned short")
((key-eq name '|uint|) "unsigned int")
((key-eq name '|ulong|) "unsigned long")
((key-eq name '|llong|) "long long")
((key-eq name '|ullong|) "unsigned long long")
((key-eq name '|i8|) "int8_t")
((key-eq name '|u8|) "uint8_t")
((key-eq name '|i16|) "int16_t")
((key-eq name '|u16|) "uint16_t")
((key-eq name '|i32|) "int32_t")
((key-eq name '|u32|) "uint32_t")
((key-eq name '|i64|) "int64_t")
((key-eq name '|u64|) "uint64_t")
((key-eq name '|i128|) "__int128")
((key-eq name '|u128|) "unsigned __int128")
((key-eq name '|real|) "long double")
(t (specify-name< name))))
(defun specify-type< (desc ir)
(let ((len (if (listp desc) (length desc) 1))
(const nil)
(type nil)
(modifier nil)
(const-ptr nil)
(variable nil)
(array nil)
(status 0))
(cond ((symbolp desc) (setq type desc))
((= len 1) (setq type (nth 0 desc)))
((= len 2) (if (key-eq (nth 0 desc) '|const|)
(progn
(setq const (nth 0 desc))
(setq type (nth 1 desc)))
(if (find (nth 1 desc) *modifiers* :test #'key-eq)
(progn
(setq type (nth 0 desc))
(setq modifier (nth 1 desc)))
(if (is-array (nth 1 desc))
(progn
(setq type (nth 0 desc))
(setq array (nth 1 desc)))
(progn
(setq type (nth 0 desc))
(setq variable (nth 1 desc)))))))
((= len 3) (if (key-eq (nth 0 desc) '|const|)
(if (find (nth 2 desc) *modifiers* :test #'key-eq)
(progn
(setq const (nth 0 desc))
(setq type (nth 1 desc))
(setq modifier (nth 2 desc)))
(if (is-array (nth 2 desc))
(progn
(setq const (nth 0 desc))
(setq type (nth 1 desc))
(setq array (nth 2 desc)))
(progn
(setq const (nth 0 desc))
(setq type (nth 1 desc))
(setq variable (nth 2 desc)))))
(if (find (nth 1 desc) *modifiers* :test #'key-eq)
(if (key-eq (nth 2 desc) '|const|)
(progn
(setq type (nth 0 desc))
(setq modifier (nth 1 desc))
(setq const-ptr (nth 2 desc)))
(if (is-array (nth 2 desc))
(progn
(setq type (nth 0 desc))
(setq modifier (nth 1 desc))
(setq array (nth 2 desc)))
(progn
(setq type (nth 0 desc))
(setq modifier (nth 1 desc))
(setq variable (nth 2 desc)))))
(progn
(setq type (nth 0 desc))
(setq variable (nth 1 desc))
(setq array (nth 2 desc))))))
((= len 4) (if (key-eq (nth 0 desc) '|const|)
(if (find (nth 2 desc) *modifiers* :test #'key-eq)
(if (key-eq (nth 3 desc) '|const|)
(progn
(setq const (nth 0 desc))
(setq type (nth 1 desc))
(setq modifier (nth 2 desc))
(setq const-ptr (nth 3 desc)))
(if (is-array (nth 3 desc))
(progn
(setq const (nth 0 desc))
(setq type (nth 1 desc))
(setq modifier (nth 2 desc))
(setq array (nth 3 desc)))
(progn
(setq const (nth 0 desc))
(setq type (nth 1 desc))
(setq modifier (nth 2 desc))
(setq variable (nth 3 desc)))))
(progn
(setq const (nth 0 desc))
(setq type (nth 1 desc))
(setq variable (nth 2 desc))
(setq array (nth 3 desc))))
(if (key-eq (nth 2 desc) '|const|)
(if (is-array (nth 3 desc))
(progn
(setq type (nth 0 desc))
(setq modifier (nth 1 desc))
(setq const-ptr (nth 2 desc))
(setq array (nth 3 desc)))
(progn
(setq type (nth 0 desc))
(setq modifier (nth 1 desc))
(setq const-ptr (nth 2 desc))
(setq variable (nth 3 desc))))
(progn
(setq type (nth 0 desc))
(setq modifier (nth 1 desc))
(setq variable (nth 2 desc))
(setq array (nth 3 desc))))))
((= len 5) (if (is-array (nth 4 desc))
(progn
(setq const (nth 0 desc))
(setq type (nth 1 desc))
(setq modifier (nth 2 desc))
(setq const-ptr (nth 3 desc))
(setq array (nth 4 desc)))
(progn
(setq const (nth 0 desc))
(setq type (nth 1 desc))
(setq modifier (nth 2 desc))
(setq const-ptr (nth 3 desc))
(setq variable (nth 4 desc)))))
((= len 6) (progn
(setq const (nth 0 desc))
(setq type (nth 1 desc))
(setq modifier (nth 2 desc))
(setq const-ptr (nth 3 desc))
(setq variable (nth 4 desc))
(setq array (nth 5 desc))))
(t (setq status -1)))
(unless (or (null const) (key-eq const '|const|)) (setq status -2))
(unless (or (null modifier) (key-eq modifier '&) (key-eq modifier '*) (key-eq modifier '**)) (setq status -3))
(unless (or (null const-ptr) (key-eq const-ptr '|const|)) (setq status -4))
(unless (or (null const-ptr) (key-eq modifier '*) (key-eq modifier '**)) (setq status -5))
(unless (or (null array) (is-array array)) (setq status -6))
(when (< status 0) (error (format nil "wrong type descriptor ~D ~A" status desc)))
(values const type modifier const-ptr variable array)))
(defun specify-type-value< (desc ir)
(let ((l (cdr (last desc)))
(wl (without-last desc)))
(if (and (listp l) (> (length desc) 2) (key-eq (nth (- (length desc) 2) desc) 'FUNCTION))
(progn (setq l (nth (- (length desc) 1) desc))
(multiple-value-bind (const type modifier const-ptr variable array)
(specify-type< (without-last wl) ir)
(values const type modifier const-ptr variable array l)))
(if (and (listp l) (> (length desc) 2) (key-eq (nth (- (length desc) 2) desc) 'QUOTE))
(progn (setq l (nthcdr (- (length desc) 2) desc))
(multiple-value-bind (const type modifier const-ptr variable array)
(specify-type< (without-last wl) ir)
(values const type modifier const-ptr variable array l)))
(if (listp l)
(specify-type< desc ir)
(multiple-value-bind (const type modifier const-ptr variable array)
(specify-type< wl ir)
(values const type modifier const-ptr variable array l)))))))
(defun specify-preprocessor (def attrs lvl ir)
(when (> (length attrs) 0) (error (format nil "wrong attributes ~A" attrs)))
(when (> (length def) 2) (error (format nil "wrong preprocessor definition ~A" def)))
(let* ((name (gensym "lcc#PREPROC"))
(preproc-specifier (make-specifier name '|@PREPROC| nil nil nil nil nil nil nil)))
(specify name ir preproc-specifier)
(setf (body preproc-specifier) def)))
(defun specify-include (def attrs lvl ir)
(when (> (length attrs) 0) (error (format nil "wrong attributes ~A" attrs)))
(let ((heads (cdr def)))
(dolist (head heads)
(if (or (symbolp head) (stringp head))
(specify head ir (make-specifier head '|@INCLUDE| nil nil nil nil nil nil nil))
(error "wrong inclusion")))))
(defun specify-typedef (def attrs lvl ir)
(when (> (length attrs) 0) (error (format nil "wrong attributes ~A" attrs)))
(when (< (length def) 3) (error (format nil "syntax error ~A" def)))
(multiple-value-bind (const type modifier const-ptr variable array) (specify-type< (nthcdr 1 def) ir)
(when (null variable) (error (format nil "syntax error ~A" def)))
(specify variable ir (make-specifier variable '|@TYPEDEF| const type
modifier const-ptr array nil nil))))
(defun specify-variable (def attrs lvl ir)
(let* ((is-auto nil)
(is-register nil)
(is-static nil)
(is-extern nil)
(type (cdr def)))
(dolist (attr attrs)
(let ((name (car attr)))
(cond ((key-eq name '|auto|) (setq is-auto t))
((key-eq name '|register|) (setq is-register t))
((key-eq name '|static|) (setq is-static t))
((key-eq name '|extern|) (setq is-extern t))
(t (error (format nil "unknown variable attribute ~A" attr))))))
(multiple-value-bind (const type modifier const-ptr variable array default)
(specify-type-value< type ir)
(let ((attributes '()))
(when is-extern (push '|extern| attributes))
(when is-static (push '|static| attributes))
(when is-register (push '|register| attributes))
(when is-auto (push '|auto| attributes))
(specify variable ir (make-specifier variable '|@VARIABLE| const type
modifier const-ptr array default attributes))))))
(defun specify-function (def attrs lvl ir)
(let* ((is-static nil)
(is-declare nil)
(is-inline nil)
(is-extern nil)
(name (specify-name< (nth 1 def)))
(params (nth 2 def))
(r-> (nth 3 def))
(has-returns (and (consp r->) (key-eq (car r->) '|returns|)))
(returns (if has-returns r->
(if (key-eq name '|main|) '(|returns| |int|) '(|returns| |void|))))
(body (if has-returns (nthcdr 4 def) (nthcdr 3 def))))
(dolist (attr attrs)
(let ((name (car attr)))
(cond ((key-eq name '|static|) (setq is-static t))
((key-eq name '|declare|) (setq is-declare t))
((key-eq name '|inline|) (setq is-inline t))
((key-eq name '|extern|) (setq is-extern t))
(t (error (format nil "unknown function attribute ~A" attr))))))
(when (< (length def) 3) (error (format nil "wrong function definition ~A" def)))
(when (and is-declare body) (error (format nil "function declaration with body '~A' ~A" name (first body))))
(let ((function-specifier nil))
(let ((attributes '()))
(when is-extern (push '|extern| attributes))
(when is-inline (push '|inline| attributes))
(when is-static (push '|static| attributes))
(when is-declare (push '|declare| attributes))
(multiple-value-bind (const type modifier const-ptr variable array)
(specify-type< (cdr returns) ir)
(setq function-specifier (make-specifier name '|@FUNCTION| const type modifier const-ptr array nil attributes))
(specify name ir function-specifier)))
(setf (body function-specifier) body)
(dolist (param params)
(let ((is-anonymous nil))
(multiple-value-bind (const type modifier const-ptr variable array default)
(specify-type-value< param ir)
(when (null variable)
(setq is-anonymous t)
(setq variable (gensym "lcc#PARAM")))
(specify variable (params function-specifier)
(make-specifier variable '@|PARAMETER| const type modifier const-ptr array default nil is-anonymous))))))))
(defun specify-enum (def attrs lvl ir)
(when (> (length attrs) 0) (error (format nil "wrong attributes ~A" attrs)))
(let* ((is-anonymous (or (= (length def) 1) (not (symbolp (nth 1 def)))))
(name (if is-anonymous (gensym "lcc#ENUM") (specify-name< (nth 1 def))))
(constants (if is-anonymous (nthcdr 1 def) (nthcdr 2 def)))
(enum-specifier (make-specifier name '|@ENUM| nil nil nil nil nil nil nil)))
(specify name ir enum-specifier)
(setf (anonymous enum-specifier) is-anonymous)
(loop for const in constants
with l = (length constants)
for i from 0 to l
do (progn
(unless (and (consp const) (symbolp (car const))) (error (format nil "syntax error ~A" const)))
(let ((key (car const))
(value (cdr const)))
(unless (or (null value) (numberp value) (symbolp value)) (error (format nil "syntax error ~A" const)))
(specify key (inners enum-specifier) (make-specifier key '|@VARIABLE| nil nil nil nil nil value nil)))))))
(defun specify-struct (def attrs lvl ir &key ((:nested is-nested) nil))
(when (> (length attrs) 0) (error (format nil "wrong attributes ~A" attrs)))
(let* ((is-anonymous (or (= (length def) 1) (not (symbolp (nth 1 def)))))
(name (if is-anonymous (gensym "lcc#STRUCT") (specify-name< (nth 1 def))))
(clauses (if is-anonymous (nthcdr 1 def) (nthcdr 2 def)))
(struct-specifier (make-specifier name '|@STRUCT| nil nil nil nil nil nil nil)))
(when (and is-anonymous (not is-nested)) (error (format nil "only nested structs could be anonymous")))
(specify name ir struct-specifier)
(setf (anonymous struct-specifier) is-anonymous)
(let ((attributes '())
(declares '()))
(dolist (clause clauses)
(if (consp clause)
(let ((construct (car clause)))
(cond ((find (char (symbol-name construct) 0) "@#")
(specify-preprocessor clause attributes 0 (inners struct-specifier))
(setq attributes '()))
((key-eq construct '|static|) (push clause attributes))
((key-eq construct '|declare|) (push clause attributes))
((key-eq construct '|inline|) (push clause attributes))
((key-eq construct '|auto|) (push clause attributes))
((key-eq construct '|register|) (push clause attributes))
((key-eq construct '|extern|) (push clause attributes))
((key-eq construct '|member|)
(specify-variable clause attributes (+ lvl 1) (inners struct-specifier))
(setq attributes '()))
((key-eq construct '|method|)
(specify-function clause attributes (+ lvl 1) (inners struct-specifier))
(setq attributes '()))
((key-eq construct '|enum|)
(specify-enum clause attributes (+ lvl 1) (inners struct-specifier))
(setq attributes '()))
((key-eq construct '|struct|)
(specify-struct clause attributes (+ lvl 1) (inners struct-specifier) :nested t)
(setq attributes '()))
((key-eq construct '|union|)
(specify-union clause attributes (+ lvl 1) (inners struct-specifier) :nested t)
(setq attributes '()))
((key-eq construct '|declares|)
(when (= (length clause) 1)
(error (format nil "declares needs a name of variable for anonymous struct")))
(when (> (length clause) 2)
(error (format nil "declares clause only accepts a name of variable ~A" (nth 1 clause))))
(push (nth 1 clause) declares))
(t (error (format nil "unknown clause ~A in struct ~A" construct name)))))
(error (format nil "syntax error ~A" clause))))
(when (and (not is-anonymous) (> (length declares) 0))
(error (format nil "declares must be inside anonymous struct ~A" name)))
(dolist (decl declares)
(specify decl (inners struct-specifier) (make-specifier decl '|@DECLARES| nil name nil nil nil nil nil))))))
(defun specify-union (def attrs lvl ir &key ((:nested is-nested) nil))
(when (> (length attrs) 0) (error (format nil "wrong attributes ~A" attrs)))
(let* ((is-anonymous (or (= (length def) 1) (not (symbolp (nth 1 def)))))
(name (if is-anonymous (gensym "lcc#UNION") (specify-name< (nth 1 def))))
(clauses (if is-anonymous (nthcdr 1 def) (nthcdr 2 def)))
(union-specifier (make-specifier name '|@UNION| nil nil nil nil nil nil nil)))
(when (and is-anonymous (not is-nested)) (error (format nil "only nested unions could be anonymous")))
(specify name ir union-specifier)
(setf (anonymous union-specifier) is-anonymous)
(let ((attributes '())
(declares '()))
(dolist (clause clauses)
(if (consp clause)
(let ((construct (car clause)))
(cond ((find (char (symbol-name construct) 0) "@#")
(specify-preprocessor clause attributes 0 (inners union-specifier))
(setq attributes '()))
((key-eq construct '|member|)
(specify-variable clause attributes (+ lvl 1) (inners union-specifier))
(setq attributes '()))
((key-eq construct '|struct|)
(specify-struct clause attributes (+ lvl 1) (inners union-specifier) :nested t)
(setq attributes '()))
((key-eq construct '|union|)
(specify-union clause attributes (+ lvl 1) (inners union-specifier) :nested t)
(setq attributes '()))
((key-eq construct '|declares|)
(when (= (length clause) 1)
(error (format nil "declares needs a name of variable for anonymous union")))
(when (> (length clause) 2)
(error (format nil "declares clause only accepts a name of variable ~A" (nth 1 clause))))
(push (nth 1 clause) declares))
(t (error (format nil "unknown clause ~A in union ~A" construct name)))))
(error (format nil "syntax error ~A" clause))))
(when (and (not is-anonymous) (> (length declares) 0))
(error (format nil "declares must be inside anonymous union ~A" name)))
(dolist (decl declares)
(specify decl (inners union-specifier) (make-specifier decl '|@DECLARES| nil name nil nil nil nil nil))))))
(defun specify-guard (def attrs lvl ir)
(when (> (length attrs) 0) (error (format nil "wrong attributes ~A" attrs)))
(let* ((name (specify-name< (nth 1 def)))
(clauses (nthcdr 2 def))
(guard-specifier (make-specifier name '|@GUARD| nil nil nil nil nil nil nil)))
(specify name ir guard-specifier)
(let ((attributes '()))
(dolist (clause clauses)
(if (consp clause)
(let ((construct (car clause)))
(cond ((find (char (symbol-name construct) 0) "@#")
(specify-preprocessor clause attributes 0 (inners guard-specifier))
(setq attributes '()))
((key-eq construct '|static|) (push clause attributes))
((key-eq construct '|declare|) (push clause attributes))
((key-eq construct '|inline|) (push clause attributes))
((key-eq construct '|auto|) (push clause attributes))
((key-eq construct '|register|) (push clause attributes))
((key-eq construct '|extern|) (push clause attributes))
((key-eq construct '|include|) (setq attributes '()))
((key-eq construct '|guard|)
(specify-guard clause attributes lvl (inners guard-specifier))
(setq attributes '()))
((key-eq construct '|variable|)
(specify-variable clause attributes lvl (inners guard-specifier))
(setq attributes '()))
((key-eq construct '|function|)
(specify-function clause attributes lvl (inners guard-specifier))
(setq attributes '()))
((key-eq construct '|enum|)
(specify-enum clause attributes lvl (inners guard-specifier))
(setq attributes '()))
((key-eq construct '|struct|)
(specify-struct clause attributes lvl (inners guard-specifier))
(setq attributes '()))
((key-eq construct '|union|)
(specify-union clause attributes lvl (inners guard-specifier))
(setq attributes '()))
((key-eq construct '|typedef|)
(specify-typedef clause attributes lvl (inners guard-specifier))
(setq attributes '()))
(t (error (format nil "unknown clause ~A in guard ~A" construct name)))))
(error (format nil "syntax error ~A" clause)))))))