2 ;;;; I suggest you move along to another file,
3 ;;;; this one is terribly coded and in constant change.
4 ;;;; But if you care to look around - good luck.
6 ;;;; Heavily commented so I remember what happens here.
9 ; An ordered stack of operations is build up as closures,
10 ; each hold an accumulated value. When an operator of lesser
11 ; "rank" (see rank constants below) is encountered, the stack unwinds
12 ; recursively, passing the encountered rank and the accumulated
13 ; value for the operation.
15 ; Obviously there are different scopes where these operations apply,
16 ; namely "paren" and "function" scopes. Both scopes have their own
17 ; "read-eval" loop which terminate by the usual stack unwind and
18 ; a thrown symbol corresponding to the scope:
19 ; 'break-paren scope and 'break-fn-scope
21 ; The order of events and the way the termination is handled
22 ; is DIFFERENT between the two. Function scope is notably more
23 ; complex because I don't require ONE function parameter to be
24 ; enclosed in parentheses, multiple parameters require them.
28 ;;; Define variables as constant integers
29 (defmacro enumerate-constants
(&rest syms
&aux
(v 0))
35 (setf (values k v
) (values-list k
)))
36 (prog1 `(defconstant ,k
,v
)
40 ;V Set precedence ranks.V
52 +vip-rank
+);< Virtually infinite precedence.
55 (defun multn-if-last-read ()
56 (declare (special *last-read
*))
58 ;; Implicit multiplication, two
59 ;; values were seperated by a space.
61 (parsed-opern +multn-rank
+ #'multn
))))
63 ;;; See if /parsed-thing/ can be interpreted
64 ;;; in any way. If so, call success-fn, then
65 ;;; finish interpreting.
66 (defun interpret-parsed
67 (parsed-thing &optional
(success-fn #'values
))
68 (declare (special *last-read
*))
69 (multn-if-last-read);< Check for implied multiplication.
70 (if (symbolp parsed-thing
)
71 (when (boundp parsed-thing
)
72 (let ((val (symbol-value parsed-thing
)))
75 ;; A symbol representing a function
76 ;; was read, change scope.
78 (symbol-function val
))
79 (setq *last-read
* val
))))
81 ;; The reader thinks it's something other
82 ;; than a symbol, parser doesn't need to worry.
84 (setq *last-read
* parsed-thing
))))
86 ;;; An invalid symbol was read,
87 ;;; could be something like "34x"
88 (defun parse-compact (&optional sym
&aux arrlen
)
89 (declare (special *parse-next
* *last-read
*))
91 ;; Must create *parse-next* from a symbol
92 (let ((sym-str (symbol-name sym
)))
93 (setq arrlen
(length sym-str
)
96 :element-type
'character
97 :fill-pointer
(1- arrlen
)
98 :displaced-to sym-str
)))
99 (setq arrlen
(length *parse-next
*)))
101 :for index
:from
(fill-pointer *parse-next
*) :downto
1
104 (setf (fill-pointer *parse-next
*) index
)
106 (let ((*package
* (find-package :lineal.client-vars
)))
107 (read-from-string *parse-next
*))
109 ;; We found valid input.
112 (setf *parse-next
* nil
)
114 (fill-pointer *parse-next
*) arrlen
116 (make-array (- arrlen index
)
117 :element-type
'character
119 :displaced-to
*parse-next
*
120 :displaced-index-offset index
)))))
121 (when successp
(return)))
123 ;; Nothing matched; quit trying.
124 (setf (fill-pointer *parse-next
*) arrlen
)
125 (signal 'unbound-variable
:name
*parse-next
*)))
128 ;;; Everything in the file calls this
129 ;;; read function on the infix stream.
130 (defun read-next-infix ()
131 (declare (special *unwind-rank-fn
* *last-read
*
132 *parse-strm
* *parse-next
*))
133 (if *parse-next
* (parse-compact)
136 (let ((*package
* (find-package
137 :lineal.client-vars
)))
138 ;; Sometimes an exception is thrown and
139 ;; control breaks to the current "read loop"
140 (setq this-read
(read *parse-strm
*)))
142 ;; Did not just read an operator.
146 (lambda () (setf successp t
)))
148 ;; We have no idea wtf was just read.
149 (parse-compact this-read
)))
152 (declare (ignore condit
))
153 ;; Don't check for unclosed parentheses,
155 (funcall *unwind-rank-fn
*
156 +base-rank
+ *last-read
*)))))
158 ;;; This is a generic construct for a closure
159 ;;; which will be stored as *unwind-rank-fn*
160 (defmacro opern-climber-lambda
161 ((this-rank op-rank val
) . body
)
162 `(lambda (,op-rank
,val
)
164 ;; A prefixed operator (see function below)
165 ;; was parsed last, unary-pre-opern.
167 ,this-rank
(the function
,op-rank
)))
169 (declare (type (integer 0 ,+vip-rank
+) ,op-rank
))
172 ;;; When something like 2 + -3 is encountered,
173 ;;; the negative is what this function deals with.
174 (defun unary-pre-opern (prev-rank this-op-fn
)
175 (declare (special *unwind-rank-fn
*))
176 (let ((prev-unwind-rank-fn *unwind-rank-fn
*)
180 (opern-climber-lambda
181 (prev-rank op-rank val
)
182 (if (and (< prev-rank op-rank
)
183 (< +multn-rank
+ op-rank
))
184 (progn (setq *unwind-rank-fn
*
187 (funcall prev-unwind-rank-fn op-rank
188 (funcall this-op-fn val
))))
189 *unwind-rank-fn
* this-unwind-rank-fn
)
190 (throw 'fn-scope
(values))))
192 ;;; A binary operator is parsed.
193 (defun parsed-opern (this-rank this-op-fn
)
194 (declare (special *unwind-rank-fn
* *last-read
*))
196 (funcall *unwind-rank-fn
* this-op-fn nil
))
197 (let* ((args (cons (funcall *unwind-rank-fn
*
198 this-rank
*last-read
*)
201 (prev-unwind-rank-fn *unwind-rank-fn
*)
206 (opern-climber-lambda
207 (this-rank op-rank val
)
209 ((= this-rank op-rank
)
211 *unwind-rank-fn
* this-unwind-rank-fn
213 tail
(setf (cdr tail
) (cons val nil
)))
214 (throw 'fn-scope
(values)))
215 ((< this-rank op-rank
)
216 (setq *unwind-rank-fn
* this-unwind-rank-fn
)
219 (rplacd tail
(cons val nil
))
220 (funcall prev-unwind-rank-fn op-rank
221 (apply this-op-fn args
)))))
222 *unwind-rank-fn
* this-unwind-rank-fn
)
225 ;;; Parse an exclaimation point character.
226 (defun factorial-reader (c strm
)
227 (declare (ignore c strm
)
228 (special *unwind-rank-fn
* *last-read
*))
230 ;; Make sure any negatives are applied.
231 (funcall *unwind-rank-fn
*
232 #'lineal.overload
::over-factorial nil
))
234 (lineal.overload
::factorial
235 (funcall *unwind-rank-fn
*
236 +factorial-rank
+ *last-read
*)))
240 (defun open-paren-after-whitespace-peekp ()
241 (declare (special *parse-strm
* *parse-next
*))
243 (do () ((not (char= #\Space
(peek-char nil
*parse-strm
*)))
244 (char= #\
( (peek-char nil
*parse-strm
*)))
245 (read-char *parse-strm
*))))
247 (defun closed-paren-peekp ()
248 (declare (special *parse-strm
* *parse-next
*))
250 (char= #\
) (peek-char nil
*parse-strm
*))))
252 (defun parse-function-scope (this-fn)
253 (declare (special *unwind-rank-fn
* *last-read
*))
254 (when (open-paren-after-whitespace-peekp)
255 ;; User chose to enclose the
256 ;; argument(s) in parentheses.
259 (if (consp *last-read
*)
260 (apply this-fn
*last-read
*)
261 (funcall this-fn
*last-read
*)))
262 (return-from parse-function-scope
(values)))
263 (when (closed-paren-peekp)
264 ;; Using notation like (f*g)(x)
266 (setq *last-read
* this-fn
)
267 (return-from parse-function-scope
(values)))
268 (let ((prev-unwind-rank-fn *unwind-rank-fn
*)
273 (opern-climber-lambda
274 (+fn-rank
+ op-rank val
)
276 ((< op-rank
+fn-rank
+)
277 ;; Breaking from parens or program.
278 (funcall prev-unwind-rank-fn
279 op-rank
(if (consp val
)
281 (funcall this-fn val
))))
282 ((= op-rank
+fn-rank
+)
283 ;; Return from the function scope.
284 (setq *unwind-rank-fn
* prev-unwind-rank-fn
285 *last-read
* (if (consp val
)
287 (funcall this-fn val
)))
288 (throw 'break-fn-scope
(values)))
289 (t ;V Stay in the function scope.V
290 (setq *unwind-rank-fn
* this-unwind-rank-fn
)
292 *unwind-rank-fn
* this-unwind-rank-fn
)
298 (funcall *unwind-rank-fn
*
299 +fn-rank
+ *last-read
*)))
301 ;;; "read-eval" loop used by process-infix-from-stream
302 ;;; and open-paren-reader
303 (defun parse-infix ()
304 (declare (special *unwind-rank-fn
* *last-read
*))
305 (let ((prev-unwind-rank-fn *unwind-rank-fn
*)
310 (opern-climber-lambda
311 (+paren-rank
+ op-rank val
)
313 ((< op-rank
+paren-rank
+)
314 (funcall prev-unwind-rank-fn
316 ((= op-rank
+paren-rank
+)
317 ;; Closing paren encountered.
318 (setq *unwind-rank-fn
* prev-unwind-rank-fn
320 (throw 'break-paren-scope
(values)))
322 (setq *unwind-rank-fn
* this-unwind-rank-fn
)
324 *unwind-rank-fn
* this-unwind-rank-fn
)
330 (read-next-infix))))))
332 (defun open-paren-reader (strm ch
)
333 (declare (ignore strm ch
))
335 (catch 'break-paren-scope
339 ;;; Unwind the operation stack.
340 ;;; When the paren closure is reached,
341 ;;; *last-read* is set to the value calculated within
342 ;;; the parenthesis and 'break-paren-scope is thrown.
343 (defun close-paren-reader (strm ch
)
344 (declare (ignore strm ch
)
345 (special *unwind-rank-fn
* *last-read
*))
346 (funcall *unwind-rank-fn
*
347 +paren-rank
+ *last-read
*))
349 ;;; If *last-read* is nil, an operator
350 ;;; was read last, we can't logically
351 ;;; break from a function in that case.
352 (defun space-reader (strm ch
)
353 (declare (ignore strm ch
)
354 (special *last-read
*))
356 (throw 'break-fn-scope
(values))))
358 (defun comma-reader (strm ch
)
359 (declare (ignore strm ch
)
360 (special *unwind-rank-fn
* *last-read
*))
361 (let* ((lis (cons (funcall *unwind-rank-fn
*
362 +comma-rank
+ *last-read
*)
365 (prev-unwind-rank-fn *unwind-rank-fn
*)
370 (opern-climber-lambda
371 (+comma-rank
+ op-rank val
)
373 ((= +comma-rank
+ op-rank
)
379 (cdr (rplacd tail
(cons val nil
))))
380 (throw 'break-fn-scope
(values)))
381 ((< +comma-rank
+ op-rank
)
382 ;todo: figure out what this does
383 (setq *unwind-rank-fn
*
386 (t (rplacd tail
(cons val nil
))
387 (funcall prev-unwind-rank-fn op-rank
389 *unwind-rank-fn
* this-unwind-rank-fn
)
392 (defun set-opern-reader (ch this-rank this-op-fn
)
396 (declare (ignore strm ch
))
397 (parsed-opern this-rank this-op-fn
))))
399 (defparameter *infix-readtable
* (copy-readtable))
401 (let ((*readtable
* *infix-readtable
*))
402 (setf (readtable-case *readtable
*) :preserve
)
403 (set-macro-character #\
( #'open-paren-reader
)
404 (set-macro-character #\
) #'close-paren-reader
)
405 (set-macro-character #\Space
#'space-reader
)
406 (set-macro-character #\
, #'comma-reader
)
407 (set-opern-reader #\
+ +addn-rank
+ #'addn
)
408 (set-opern-reader #\-
+subtrn-rank
+ #'subtrn
)
409 (set-opern-reader #\
* +multn-rank
+ #'multn
)
410 (set-opern-reader #\
/ +divisn-rank
+ #'divisn
)
411 (set-macro-character #\
! #'factorial-reader
)
412 (set-opern-reader #\^
+exptn-rank
+ #'exptn
))
414 (defun process-infix-from-stream (strm)
415 (let ((*readtable
* *infix-readtable
*)
416 (*parse-strm
* strm
);< Stream to parse.
417 *parse-next
*;< Temporary buffer if we overparsed.
419 (lambda (op-rank val
)
420 (declare (ignore op-rank
))
421 (throw 'end-result val
)))
424 *parse-strm
* *parse-next
*
425 *unwind-rank-fn
* *last-read
*))
432 (declare (ignore condit
))
434 nil
"Likely too many parens! ~
435 (as if there were such a thing)~%"))
438 (format nil
"I don't understand: ~A~%"
439 (cell-error-name condit
)))
443 nil
"Evaluation flopped, perhaps bad input?~%~
444 Debug info: ~A~%" condit
)))))
446 (defun process-input-from-stream
447 (strm &optional
(infixp t
))
448 (let ((*read-default-float-format
* 'double-float
)
450 (if infixp
(process-infix-from-stream strm
)
451 (process-prefix-from-stream strm
))))
453 (defun process-input-from-string
454 (text &optional
(infixp t
))
455 (with-input-from-string (strm text
)
456 (process-input-from-stream strm infixp
)))