Source

lisp-random / symbol.lisp

(defparameter *env* nil)

(defun evaluate (expr)
  (cond
    ((numberp expr) expr)
    ((symbolp expr) expr)
    ((consp expr) (evaluate-form expr))
    (t "BAD INPUT")))

(defun evaluate-form (expr)
  (destructuring-bind (op . args) expr
    (if (consp op)
        nil
        (case op
          ((val) (lookup (first args)))
;;          ((vals) (transform-vals (first args)))
          ((set) (bind (first args) (second args)))
          (t (cons op (mapcar #'evaluate args)))
          ;; ...
          ))))

(defun lookup (var)
  (let ((result (assoc var *env*)))
    (unless (null result)
      (cdr result))))

(defun bind (var val)
  (let ((result (assoc var *env*)))
    (if result
        (rplacd result val)
        (push (cons var val) *env*)))
  (values))

(defun transform-vals (expr var)
  (cond
    ((eql expr var) `(val ,var))
    ((null expr) expr)
    ((listp expr) (case (first expr)
                    ((val) expr)
                    ((quote) (if (eql (second expr) var)
                                 var
                                 expr))
                    (t (mapcar (lambda (e) (transform-vals e var)) expr))))
    (t expr)))

(defun repl ()
  (setf *env* nil)
  (loop
    (format t "~&> ") 
    (let ((user-input (read *standard-input* nil :quit)))
    (if (eql user-input :quit)
        (return-from repl)
        (print (evaluate user-input))))))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.