source: branches/working-0711-perf/ccl/cocoa-ide/hemlock/unused/archive/elisp/codewalker.lisp

Last change on this file was 6, checked in by Gary Byers, 21 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.0 KB
Line 
1;;; The code walker should ideally be in ELISP-INTERNALS, however
2;;; getting it there won't be trivial, so ignoring that for now.
3(in-package "ELISP")
4
5(cl:defun walk-code (form &optional lexicals)
6 (cond ((null form) nil)
7 ((numberp form) form)
8 ((stringp form) form)
9 ((atom form) (if (member form lexicals)
10 form
11 `(elisp-value ',form)))
12 (t (cl:let ((head (car form))
13 (rest (cdr form)))
14 (cond ((eq head 'lexical-let)
15 (cl:let ((bindings (append lexicals
16 (mapcar #'(lambda (x)
17 (cl:if (symbolp x)
18 x
19 (car x)))
20 (car rest))))
21 (tail (cdr rest)))
22 (cons head
23 (cons (mapcar #'(lambda (form)
24 (walk-code form lexicals))
25 (car rest))
26 (mapcar #'(lambda (form)
27 (walk-code form bindings))
28 tail)))))
29 ((eq head 'let)
30 (cons head (cons (mapcar #'(lambda (form)
31 (walk-code form lexicals))
32 (car rest))
33 (mapcar #'(lambda (form)
34 (walk-code form lexicals))
35 (cdr rest)))))
36 ((member head '(defun defmacro))
37 (cl:let ((name (car rest))
38 (new-vars
39 (cl:loop for sym in (cadr rest)
40 if (not
41 (member sym '(&optional &rest
42 &aux &key)))
43 collect sym))
44 (forms (cddr rest))
45 (vars (cadr rest)))
46 `(,head ,name ,vars
47 ,@(mapcar
48 #'(lambda (form)
49 (walk-code form
50 (append lexicals new-vars)))
51 forms))))
52 ((eq head 'cond)
53 (cons head
54 (cl:loop for cond-form in rest
55 collect
56 (cl:loop for form in cond-form
57 collect (walk-code form lexicals)))))
58 ((eq head 'quote)
59 (cons head rest))
60 ((member head '(setq setf))
61 (cons head
62 (loop for symbol in rest
63 for toggle = t then (not toggle)
64 if toggle
65 collect symbol
66 else
67 collect (walk-code symbol lexicals))))
68 (t (cons head (mapcar #'(lambda (form)
69 (walk-code form lexicals))
70 rest))))))))
71
Note: See TracBrowser for help on using the repository browser.