-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmeta.lisp
368 lines (308 loc) · 9.88 KB
/
meta.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
;;;; -*- lisp -*- Copyright 2004, 2013-2017 Lars Brinkhoff
;;; Meta compiler to C target.
;;
;; Usage: (compile-forth "nucleus.fth" <optionally more sources> "kernel.fth")
;;
;; The output is a C file containing a dictionary with a linked list
;; of "struct word" as defined by forth.h. CODE words are emitted as
;; C functions, and colon definitions generate indirect threaded code.
;;; Words (partially) supported by this meta compiler:
;;
;; ( \ [IF] [ELSE] [THEN] [DEFINED] [UNDEFINED] INCLUDE
;; : ; IMMEDIATE DOES> DEFER CODE END-CODE
;; VARIABLE VALUE CREATE ALLOT HERE , ' CELLS INVERT RSHIFT CHAR > = + -
;; [CHAR] ['] [ ] LITERAL POSTPONE COMPILE [COMPILE] 'DODOES TO IS ." S"
;; ABORT" AHEAD IF ELSE THEN DO LEAVE LOOP +LOOP BEGIN AGAIN WHILE REPEAT
;; UNTIL CELL NAME_LENGTH TO_NEXT TO_CODE TO_DOES TO_BODY
;; .CS
;;; Restrictions and special features:
;;
;; Many words are immediate and only work in compilation mode,
;; i.e. they always append code to the current definition.
;;
;; CODE may be followed by a \ comment which specifies the generated C
;; function declaration.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defpackage :meta
(:use :cl))
(in-package :meta)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Retreive some information passed from C.
(load "params.lisp")
(defvar *this-word*)
(defvar *previous-word*)
(defvar *vocabulary* nil)
(defvar *control-stack* nil)
(defvar *state* 'interpret-word)
(defvar *input*)
(defvar *output*)
(defvar *here* 0)
(defvar *finished-p* t)
(defvar *dictionary* (make-array 0 :adjustable t :fill-pointer 0))
(defvar *deferred* nil)
(defun trivial-quit ()
#+sbcl
(#.(or (find-symbol "EXIT" "SB-EXT") (find-symbol "QUIT" "SB-EXT")))
#+clisp
(ext:quit)
#+ecl
(si:quit)
#+ccl
(ccl:quit)
#+gcl
(lisp:bye)
#-(or sbcl clisp ecl ccl gcl)
(implementation-dependent-quit))
#-ecl
(declaim (ftype function interpret-file compile-word mangle-char mangle-word
output output-line output-name output-finish quoted read-word
whitespacep))
(defun output-extern (name)
(output "extern struct word ~A_word;" (mangle-word name)))
(defun cl-user::compile-forth (&rest input-files
&aux
(output-file (output-name input-files)))
(with-open-file (*output* output-file :direction :output
:if-exists :supersede)
(output-line "#include \"forth.h\"")
(output-extern "docol,")
(let ((*previous-word* "0"))
(dolist (file input-files)
(interpret-file file))
(output-finish)))
(trivial-quit))
(defun interpret-file (file)
(loop for path in '("" "src/")
for file-path = (concatenate 'string path file)
when (probe-file file-path) do
(with-open-file (*input* file-path)
(do ((word (read-word) (read-word)))
((null word))
(funcall *state* word)))))
(defun emit (string)
(unless (stringp string)
(setq string (format nil "~A" string)))
(vector-push-extend string *dictionary*)
(incf *here* *cell-size*)
(values))
(defun trunc-word (word)
(subseq word 0 (min (length word) (1- *name-size*))))
(defun tick (word)
(format nil "&~A_word" (mangle-word word)))
(defun word-body (word &optional (n 0))
(format nil "~A.param[~D]" (tick word) n))
(defun branch-target (dest)
(word-body *this-word* (floor dest *cell-size*)))
(defun emit-word (word)
(emit (tick word)))
(defun emit-literal (x)
(emit-word "(literal)")
(if (and (integerp x) (plusp x))
(emit (format nil "~DU" x))
(emit x)))
(defun emit-branch (word dest)
(emit-word word)
(if (eq dest :unresolved)
(push *here* *control-stack*)
(setq dest (branch-target dest)))
(emit dest))
(defun resolve-branch (&optional (orig (pop *control-stack*)))
(setf orig (floor orig *cell-size*))
(setf (aref *dictionary* orig) (branch-target *here*))
(values))
(defun output (format &rest args)
(output-line (apply #'format nil format args)))
(defun output-line (line)
(fresh-line *output*)
(write-line line *output*)
(values))
(defun output-name (files)
(let* ((file (car (last files)))
(pos (position #\/ file)))
(when pos
(setq file (subseq file (1+ pos))))
(merge-pathnames (make-pathname :type "c") file)))
(defun output-finish ()
(when *deferred*
(output (shiftf *deferred* nil)))
(unless *finished-p* ;(string= *previous-word* "0")
(output-line "} };"))
(setq *finished-p* t))
(defvar *peeked-word* nil)
(defun read-word (&optional (delimiter nil delimp))
(or (shiftf *peeked-word* nil)
(let ((word-end-p
(if delimp
(lambda (char) (eql char delimiter))
(progn
(peek-char t *input* nil)
#'whitespacep))))
(do ((word nil)
(char (read-char *input* nil) (read-char *input* nil)))
((or (null char)
(funcall word-end-p char))
word)
(setq word (concatenate 'string word (string char)))))))
(defun peek-word (&rest args)
(setq *peeked-word* (apply #'read-word args)))
(defun whitespacep (char)
(or (char= char #\Tab)
(char= char #\Space)
(char= char #\Newline)))
(defpackage :interpreted
(:use)
(:export ":" "?:" "FORWARD:" "DEFER" "VALUE" "CODE" "ALLOT" "," "'" ".(" "CR"
"CELLS" "CREATE" "HERE" "+" "-" "1+" "*" "CHAR" "VARIABLE" "CELL" "IS"
"]" "INVERT" "RSHIFT" "=" ">" "INCLUDE" "CONSTANT" "LSHIFT"))
(defpackage :immediate
(:use)
(:export ";" "IS" "TO" "DOES>" "(" "\\" "LITERAL" "POSTPONE" "'DODOES"
"COMPILE" "[COMPILE]" ".\"" "S\"" "ABORT\"" "AHEAD" "IF" "ELSE"
"THEN" "DO" "LEAVE" "LOOP" "+LOOP" "BEGIN" "AGAIN" "WHILE"
"REPEAT" "UNTIL" "[CHAR]" "[']" "CELL" "NAME_LENGTH" "TO_NEXT"
"TO_CODE" "TO_DOES" "TO_BODY" "[" "[DEFINED]" "[UNDEFINED]"
"[IF]" "[ELSE]" "[THEN]" ".CS"))
(defmacro defword (name lambda-list &body body)
`(progn
(setf (get ',name 'args) ',lambda-list)
(defun ,name ,(remove '&parse lambda-list) ,@body)))
(defun find-word (word package)
(find-symbol (string-upcase word) package))
(defun immediate-word (word)
(find-word word "IMMEDIATE"))
(defun interpreted-word (word)
(find-word word "INTERPRETED"))
(defun to-integer (x)
(etypecase x
(number x)
(string (parse-integer x))))
(defun execute (word)
(do* ((args nil)
(lambda-list (get word 'args) (rest lambda-list))
(arg (first lambda-list) (first lambda-list)))
((null lambda-list)
(let ((value (apply word args)))
(and value (push value *control-stack*))))
(if (eq arg '&parse)
(setq args (nconc args (list (read-word)))
lambda-list (rest lambda-list))
(let ((datum (pop *control-stack*))
(type (char (symbol-name arg) 0)))
(when (member type '(#\N #\U))
(setq datum (to-integer datum)))
(push datum args)))))
(defun compile-word (word)
(cond
((immediate-word word)
(execute (immediate-word word)))
((multiple-value-bind (i p) (parse-integer word :junk-allowed t)
(when (and i (= p (length word)))
(emit-literal i)
t)))
(t
(emit-word word))))
(defun interpret-word (word)
(cond
((interpreted-word word)
(execute (interpreted-word word)))
((immediate-word word)
(execute (immediate-word word)))
(t
(push word *control-stack*))))
(defun output-header (name code does &optional immediatep)
(push name *vocabulary*)
(let* ((mangled (mangle-word name))
(latestxt (tick name))
(name (trunc-word name))
(len (length name)))
(when immediatep
(setq len (- len)))
(output-finish)
(output "struct word ~A_word = { ~D, \"~A\", ~A, ~A, ~A, {"
mangled len (quoted name) *previous-word* does code)
(setq *previous-word* latestxt)
(setq *finished-p* nil)))
(defun mangle-word (name)
(cond
((and (char= (char name 0) #\()
(char= (char name (1- (length name))) #\)))
(concatenate 'string "do" (mangle-word (string-trim "()" name))))
((char= (char name (1- (length name))) #\0)
name)
((equal name "0branch") "zbranch")
((equal name ">r") "to_r")
((equal name "2>r") "two_to_r")
((equal name "r>") "r_from")
((equal name "2r>") "two_r_from")
((equal name "0=") "zero_equals")
((equal name "0<") "zero_less")
((equal name "0>") "zero_greater")
(t (apply #'concatenate 'string
(map 'list #'mangle-char name)))))
(defun mangle-char (char)
(case char
(#\! "store")
(#\" "quote")
(#\# "number")
(#\' "tick")
(#\( "paren")
(#\* "star")
(#\+ "plus")
(#\, "comma")
(#\- "minus")
(#\. "dot")
(#\/ "slash")
(#\0 "zero")
(#\1 "one")
(#\2 "two")
(#\3 "three")
(#\4 "four")
(#\: "colon")
(#\; "semicolon")
(#\< "lt")
(#\= "eq")
(#\> "gt")
(#\? "query")
(#\@ "fetch")
(#\[ "lbracket")
(#\] "rbracket")
(#\\ "backslash")
(t (string char))))
(defun quoted (name)
(concatenate 'string
(loop for char across name
when (or (eql char #\") (eql char #\\))
collect #\\
collect char)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun immediatep ()
(and (equal (peek-word) "immediate")
(read-word)))
;;;definterpreted end-code
(defun roll (n list)
(let ((tail (nthcdr n list)))
(append (list (first tail)) (ldiff list tail) (rest tail))))
(defun cs-roll (n)
(setq *control-stack* (roll n *control-stack*))
(values))
(defvar *leave*)
(defun emit-loop (word)
(emit-word word)
(emit-branch "0branch" (pop *control-stack*))
(dolist (dest *leave*)
(resolve-branch dest))
(setq *leave* nil)
(emit-word "unloop"))
(defun ends-with-p (string1 string2)
(let ((n (- (length string1) (length string2))))
(and (>= n 0) (string= string1 string2 :start1 n))))
(defun mask-word (x)
(logand x (1- (ash 1 (* 8 *cell-size*)))))
(defun word-found-p (word vocabulary)
(member word vocabulary :test #'string-equal))
(defun skip-until (&rest words)
(do ((word (read-word) (read-word)))
((word-found-p word words))
(when (string-equal word "[if]")
(skip-until "[then]"))))
(load "lisp/words.lisp")