~eduardoroboto/land-of-lisp

eb0eb13f45f336d4a1f40411ab72b053fc359079 — eduardoroboto 4 months ago f803e5c main
Everything done, up to chapter 6
* Wizard Games
* Examples
2 files changed, 69 insertions(+), 0 deletions(-)

A example.lisp
M wizard_game.lisp
A example.lisp => example.lisp +24 -0
@@ 0,0 1,24 @@


;; (defun say-hello()
;;   (print "Please type your name:")
;;   (let ((name (read)))
;;     (print "Nice to meet you, ")
;;     (print name)))


(defun add-five ()
  (print "please enter a number:")
  (let ((num (read)))
    (print "When I add five I get")
    (print (+ num 5))))


(defun say-hello ()
  (princ "Please type your name:")
  (let ((name (read-line)))
    (princ "Nice to meet you, ")
    (princ name)))


(defparameter *foo* '(+ 1 2))

M wizard_game.lisp => wizard_game.lisp +45 -0
@@ 77,3 77,48 @@

(defun inventory ()
  (cons 'items (objects-at 'body *objects* *object-locations*)))


(defun game-repl ()
  (let ((cmd (game-read)))
    (unless (eq (car cmd) 'quit)
      (game-print (game-eval cmd))
      (game-repl))))


(defun game-read ()
  (let ((cmd (read-from-string
                (concatenate 'string "(" (read-line) ")" ))))
    (flet ((quote-it (x)
             (list 'quote x)))
      (cons (car cmd) (mapcar #'quote-it (cdr cmd))))))

(defparameter *allowed-commands* '(look walk pickup inventory))


(defun game-eval (sexp)
  (if (member (car sexp) *allowed-commands*)
      (eval sexp)
      '(i do not know that command.)))


(defun tweak-text (lst caps lit)
  (when lst
  (let ((item (car lst))
        (rest (cdr lst)))
    (cond ((eq item #\space) (cons item (tweak-text rest caps lit)))
     ((member item '(#\! #\? #\.)) (cons item (tweak-text rest t lit)))
     ((eq item #\") (tweak-text rest caps (not lit)))
     (lit (cons item (tweak-text rest nil lit)))
     ((or caps lit) (cons (char-upcase item) (tweak-text rest nil lit)))
     (t (cons (char-downcase item) (tweak-text rest nil nil)))))))


(defun game-print (lst)
  (princ (coerce (tweak-text (coerce (string-trim "() "
                                                  (prin1-to-string lst))
                                     'list)
                             t
                             nil)
                 'string))
  (fresh-line))