-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathesl.el
317 lines (259 loc) · 8.45 KB
/
esl.el
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
(require 'utils)
(require 'defn)
(setf *esl-stack* nil)
(setf *esl-return-stack*)
(setf *esl-code-stack*)
(setf *esl-retain-stack*)
(defn esl-push [item]
;(print *esl-stack*)
(push item *esl-stack*))
(defn esl-pop []
;(print *esl-stack*)
(if ($ 0 = (length *esl-stack*))
(error "ESL: stack underflow %s" *esl-stack*))
(pop *esl-stack*))
(defn esl-dup []
(esl-push (car *esl-stack*)))
(defn esl-swap []
(let ((a (esl-pop))
(b (esl-pop)))
(esl-push a)
(esl-push b)))
(defun esl-mangle (name)
(intern (format "__esl-word__%s" name)))
(defun esl-unmangle (name)
(intern (substring (format "%s" name) 0 12)))
(defmacro* def-esl-word (name &body body)
`(defun ,(esl-mangle name) ()
,@body))
(defmacro* def-esl-binop (op)
`(def-esl-word ,op (esl-swap) (esl-push (,op (esl-pop) (esl-pop)))))
(defvar *esl-parsing-words* (tbl!))
(defmacro* def-esl-parsing-word (name &body body)
(tbl! *esl-parsing-words* (esl-mangle name) t)
`(def-esl-word ,name ,@body))
(defun parsing-word? (mangled-name)
(member mangled-name (keyshash *esl-parsing-words*)))
(loop for op in '( = - * / mod cons < > <= >= + - eq string=) do
(eval `(def-esl-binop ,op)))
(def-esl-word and (esl-swap) (esl-push (let ((a (esl-pop)) (b (esl-pop))) (and a b))))
(def-esl-word or (esl-swap) (esl-push (let ((a (esl-pop)) (b (esl-pop))) (or a b))))
(def-esl-word print (print (esl-pop)))
(def-esl-word drop-all (setf *esl-stack* nil))
(def-esl-word swap (esl-swap))
(def-esl-word rot (let ((top (esl-pop))
(mid (esl-pop))
(bot (esl-pop)))
(esl-push mid)
(esl-push top)
(esl-push bot)
))
(def-esl-word error (error (concat "ESL: " (esl-pop))))
(setf *esl-called-from-hard-compiled* nil)
(def-esl-word call
(let* ((qtn (esl-pop))
(qtn-hard (functionp qtn))
(caller-hard *esl-called-from-hard-compiled*))
(cond
(qtn-hard
(funcall qtn))
(caller-hard
(esl-eval-compiled qtn))
((not caller-hard)
(if *esl-code-stack* (push *esl-code-stack* *esl-return-stack*))
(setq *esl-code-stack* qtn)))))
(def-esl-word over (esl-push (cadr *esl-stack*)))
(def-esl-word curry
(let ((qtn (esl-pop))
(it (esl-pop)))
(esl-push (if (functionp qtn)
(list (eval `(function (lambda () (esl-push ',it))))
qtn)
(cons (eval `(function (lambda () (esl-push ',it))))
qtn)))))
(defun esl-qtn? (it)
(or (listp it) (functionp it)))
(def-esl-word compose
(let ((q2 (esl-pop))
(q1 (esl-pop)))
(if (not (and (esl-qtn? q1)
(esl-qtn? q2)))
(error "ESL:Trying to compose non-quotations."))
(esl-push (cond
((and (functionp q1)
(functionp q2))
(eval `(function (lambda () (funcall ,q1)
(funcall ,q2)))))
((and (not (functionp q1))
(not (functionp q2)))
(append q1 q2))
((and (functionp q1)
(not (functionp q2)))
(cons q1 q2))
((and (not (functionp q1))
(functionp q2))
(append q1 (list q2)))))))
(def-esl-word = (esl-push (= (esl-pop) (esl-pop))))
(def-esl-word not (esl-push (not (esl-pop))))
(def-esl-word if (let ((fb (esl-pop))
(tb (esl-pop))
(predval (esl-pop)))
(if predval
(esl-push tb)
(esl-push fb))
(__esl-word__call)
))
(def-esl-word t (esl-push t))
(def-esl-word f (esl-push nil))
(def-esl-word quot>word (let ((name (esl-pop))
(qtn (esl-pop)))
(eval `(def-esl-word ,name
(esl-push ',qtn)
(let ((*esl-called-from-hard-compiled* nil))
(__esl-word__call))))))
(def-esl-word dup (esl-dup))
(def-esl-word drop (esl-pop))
(def-esl-word retain> (push (esl-pop) *esl-retain-stack*))
(def-esl-word <retain (esl-push (pop *esl-retain-stack*)))
(def-esl-word push-retain (push (esl-pop) *esl-retain-stack*))
(def-esl-word pop-retain (esl-push (pop *esl-retain-stack*)))
(def-esl-word retain-dup (push (car *esl-retain-stack*) *esl-retain-stack*))
(def-esl-word retain-swap (let ((a (pop *esl-retain-stack*))
(b (pop *esl-retain-stack*)))
(push a *esl-retain-stack*)
(push b *esl-retain-stack*)))
(def-esl-word call-emacs-push
(let* ((emacs-fn (esl-pop))
(nargs (esl-pop))
(args (reverse (loop for i from 1 to nargs collect (esl-pop)))))
(let ((result (apply emacs-fn args)))
(esl-push result))))
(def-esl-parsing-word :
(let ((split (split-list-drop *esl-stack*
(lambda (v) (eq v 'end-word:)))))
(if split
(dlet_ [[qtn rest] split]
(setf *esl-stack* rest)
(esl-push 'quot>word)
(esl-push (list 'quote (car qtn)))
(esl-push (cdr qtn)))
(error "Could not find terminating end-word: for %s" (car *esl-stack*)))))
(def-esl-word mark-as-parsing
(let ((name (esl-pop)))
(if (symbolp name)
(tbl! *esl-parsing-words* (esl-mangle name) t)
(error "You tried to mark %s as parsing word, but its not a symbol" name))))
(def-esl-parsing-word PARSING:
(let ((split (split-list-drop *esl-stack*
(lambda (v) (eq v 'end-word:)))))
(if split
(dlet_ [[qtn rest] split]
(setf *esl-stack* rest)
(esl-push 'mark-as-parsing)
(esl-push (list 'quote (car qtn)))
(esl-push 'quot>word)
(esl-push (list 'quote (car qtn)))
(esl-push (cdr qtn)))
(error "Could not find terminating end-word: for %s" (car *esl-stack*)))))
(def-esl-parsing-word hard-compile-quotation
(let ((qtn (esl-pop)))
(esl-push (esl-soft->hard-compile qtn))))
(defn_ quotedp [it]
(and (listp it)
(eq 'quote (car it))))
(defn_ esl-hard-compile [esl-code]
(eval `(function (lambda nil ,@(let ((*esl-stack* esl-code))
(loop with output = nil
while *esl-stack*
do
(let ((part (esl-pop)))
(cond
((listp part)
(push (if (quotedp part)
`(esl-push ,part)
`(esl-push (quote ,(esl-compile part)))) output))
((symbolp part)
(cond ((parsing-word? (esl-mangle part))
(funcall (esl-mangle part)))
(t
(push (list (esl-mangle part)) output))))
((or
(numberp part)
(stringp part))
(push `(esl-push ,part) output))))
finally (return (reverse output))))))))
(defn_ esl-compile [esl-code]
(let ((*esl-stack* esl-code)
(*esl-code-stack* nil)
(*esl-retain-stack* nil)
(*esl-return-stack* nil))
(loop with output = nil
while *esl-stack*
do
(let ((part (esl-pop)))
(cond
((listp part)
(push (if (quotedp part)
(eval `(function (lambda () (esl-push ,part))))
(eval `(function (lambda () (esl-push (quote ,(esl-compile part))))))) output))
((symbolp part)
(cond ((parsing-word? (esl-mangle part))
(esl-eval-compiled (list (esl-mangle part))))
(t
(push (esl-mangle part) output))))
((or
(numberp part)
(stringp part))
(push part output))))
finally (return (reverse output)))))
(defn_ body-of [[l arg & body]]
body)
(defn_ esl-soft->hard-compile [qtn]
(eval `(function (lambda () ,@(loop for el in qtn
collect
(cond
((symbolp el)
(list el))
((functionp el)
`(progn ,@(body-of el))
)))))))
(defun esl-next-part ()
(cond (*esl-code-stack*
(pop *esl-code-stack*))
(*esl-return-stack*
(setf *esl-code-stack* (pop *esl-return-stack*))
(pop *esl-code-stack*))
(t
(error "Tried to get next part, but both the code stack and the return stack are empty - this should not happen."))))
(defun esl-eval-compiled (ccode)
(if (functionp ccode)
(let ((*esl-called-from-hard-compiled* t))
(functionp ccode))
(let ((*esl-code-stack* ccode)
(*esl-called-from-hard-compiled* nil))
(loop while
(or *esl-return-stack*
*esl-code-stack*)
do
(let ((part (esl-next-part)))
(cond ((or (stringp part) (numberp part))
(esl-push part))
((functionp part)
(funcall part))))))))
(defmacro* esl-do (&body body)
`(esl-eval-compiled (esl-compile ',body)))
(defmacro* esl-dop (&body body)
`(progn (esl-do ,@body)
(esl-print-state)
nil))
(defun esl-print-state ()
(let ((stack (loop with o = "" for s in (reverse *esl-stack*) do
(setf o (concat o (format "\n %s" s)))
finally (return o))))
(print (format "------------------\nesl-stack:\n- %s\n------------------" stack))))
(defun esl-eval-region (s e)
(interactive "r")
(let* ((fixed (concat "(" (buffer-substring s e) ")")))
(esl-eval-compiled (esl-compile (read fixed)))
(esl-print-state)))
(require 'esl-mode)