ook.lisp 8.69 KB
Newer Older
Marco Antoniotti's avatar
Marco Antoniotti committed
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
;;;; -*- Mode: Lisp -*-

;;;; ook.lisp
;;;; ========
;;;;
;;;; English commentary provided for non Orangutan and non-wizard
;;;; readers.
;;;;
;;;; See the file COPYING for license and copyright information.

(in-package "OOK")

(defgeneric ook. (ooks)
  (:documentation "The OOK compiler entry point.

This generic function just wraps the actual compiler, which expects a
list of - you guessed it - 'ooks'.

Arguments and Values:

OOKS : see the methods' descriptions.
result : a lambda-expression taking no arguments."))


(declaim (ftype (function (t &optional stream) t) ook!))

(declaim (ftype (function (t) t) ook?))


30
#-sbcl
Marco Antoniotti's avatar
Marco Antoniotti committed
31 32 33
(defconstant +ooks+ '(ook. ook? ook!)
  "Need we say more?")

34 35 36 37 38 39

#+sbcl ; Fascist SBCL!!!!!
(defparameter +ooks+ '(ook. ook? ook!)
  "Need we say more?")


Marco Antoniotti's avatar
Marco Antoniotti committed
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 317 318 319 320 321 322
(defconstant monkey?!? 'monkey?!?
  "You are in trouble if you call him 'monkey'!")


(defun make-library ()
  "Creates the memory where the progr... sorry, the 'ooks', reside."
  (make-array (expt 2 16)
              :element-type 'fixnum
              :initial-element 0))


(defparameter *library*
  (make-library)
  "The place where library is located (can be 'unseen').")


(defparameter *index* 0
  "The library counter (counts more than banana peels).")


;;; ook.
;;; ----
;;; OOK. methods.

(defmethod ook. ((ooks string))
  "Dispatches on a pathname containing the OOKS."
  (ook. (pathname ooks)))


(defmethod ook. ((ooks pathname))
  "The pathname containing the OOKS is opened and its content read."
  (with-open-file (ookf ooks :direction :input)
    (ook. ookf)))


(defmethod ook. ((ooks stream))
  "Compiles the OOKS from the (banana) STREAM."
  (unless (and (input-stream-p ooks)
               (open-stream-p ooks))
    (return-from ook. 'monkey?!?))

  (loop for ook = (read ooks nil)
        while ook
        collect ook into ooks-heard
        finally (return (ook. ooks-heard))))


(defmethod ook. ((ooks list))
  "OOKS is a list of OOKs to be compiled pair by pair.

The result is a LAMBDA of no arguments that contains the compiled
program (essentially a giant TAGBODY)."

  (when (oddp (list-length ooks))
    (return-from ook. 'monkey?!?))

  (unless (every (lambda (ook)
                   (member ook +ooks+ :test #'string-equal))
                 ooks)
    (return-from ook. 'monkey?!?))
 
  (loop with ook-pile = ()
        for (ook1 ook2) on ooks by #'cddr
        nconc
        (flet ((ook-is (ook)
                 (and (string-equal ook1 (first ook))
                      (string-equal ook2 (second ook))))

               (ook-tag ()
                 (gentemp "OOK-" (find-package "OOK")))
                 
               )
          (cond ((ook-is '(ook. ook?))
                 (list '(incf *index*)))

                ((ook-is '(ook? ook.))
                 (list '(decf *index*)))

                ((ook-is '(ook. ook.))
                 (list '(incf (aref *library* *index*))))

                ((ook-is '(ook! ook!))
                 (list '(decf (aref *library* *index*))))

                ((ook-is '(ook. ook!))
                 (list '(setf (aref *library* *index*)
                              (char-code (read-char)))))
                  
                ((ook-is '(ook! ook.))
                 (list '(unless (= 13 (aref *library* *index*)) ; CR/LF messiness on UN*X.
                          (write-char (code-char (aref *library* *index*))))))

                ((ook-is '(ook! ook?))
                 (let ((ook-tag (ook-tag))
                       (next-ook-tag (ook-tag))
                       )
                   (push next-ook-tag ook-pile)
                   (push ook-tag ook-pile)
                   (list ook-tag
                         `(when (zerop (aref *library* *index*))
                            (go ,next-ook-tag)))))

                ((ook-is '(ook? ook!))
                 (list `(go ,(pop ook-pile))
                       (pop ook-pile)))
                ))
        into ook-sounds
        finally (return `(lambda ()
                           (block ooks
                             (let ((*library* (make-library)))
                               (setf *index* 0)
                               (tagbody ,.ook-sounds (return-from ooks 'ook)))
                             )))
        ))


(defmethod ook. ((ooks t))
  "You are in trouble! This method says 'monkey?!?."
  'monkey?!?)


;;; the-ooks
;;; --------
;;; Extracts the ooks.

(defun the-ooks (ooks)
  ;; Ooks is the LAMBDA produced by OOK.
  (if (eq ooks monkey?!?)
      ()
      (butlast (rest ; tagbody
                (fourth ; let
                 (third ; block
                  (third ; lambda
                   ooks)))))
      ))


;;; ook!
;;; ----
;;; The OOK decompiler.

(defun ook! (ooks &optional (library-stream *standard-output*))
  "The Ook decompiler.

Takes the LAMBDA generated by the compiler and wirtes out the programs
in a format readable by an orangutan (or by a well fed and drunk
wizard, unless he is asleep, which will be the case most of the time
when well fed and drunk).

See Also:

KOO
"
  (koo ooks library-stream))


(defun koo (ooks &optional (library-stream *standard-output*))
  "The Ook decompiler.

Takes the LAMBDA generated by the compiler and wirtes out the programs
in a format readable by an orangutan (or by a well fed and drunk
wizard, unless he is asleep, which will be the case most of the time
when well fed and drunk).

I am repeating this because KOO is called by OOK! (the decompiler) and
wizards have been known to require more than a repetition of whatever
it is said (especially during Faculty Meetings.)

See Also:

OOK!
"

  (let ((uttered-ooks
         (with-output-to-string (uttered-ooks)
           (flet ((utter-ooks (ook ook?)
                    (case ook
                      (incf   (if (symbolp ook?) ; *index*
                                  (write-string "Ook. Ook?" uttered-ooks)
                                  (write-string "Ook. Ook." uttered-ooks)))

                      (decf   (if (symbolp ook?) ; *index*
                                  (write-string "Ook? Ook." uttered-ooks)
                                  (write-string "Ook! Ook!" uttered-ooks)))

                      (setf   (write-string "Ook. Ook." uttered-ooks))

                      (unless (write-string "Ook! Ook." uttered-ooks))

                      (when   (write-string "Ook! Ook?" uttered-ooks))

                      (go     (write-string "Ook? Ook!" uttered-ooks))

                      (t nil)))
                  )

             (let ((ooks (the-ooks ooks)))
               (unless (symbolp (first ooks))
                 (utter-ooks (caar ooks) (second (first ooks))))

               (loop for inner-ook in (rest ooks)
                     unless (symbolp inner-ook) ; a go tag.
                     do
                     (write-char #\Space uttered-ooks)
                     (utter-ooks (first inner-ook) (second inner-ook))))
             )))
        )

    (if library-stream
        (loop with ooks-length = (length uttered-ooks)
              for start from 0 by 70
              while (< start ooks-length)
              do
              (write-string uttered-ooks library-stream
                            :start start
                            :end (if (< (+ start 70) ooks-length)
                                     (+ start 70)))
              (terpri library-stream)
              )
        uttered-ooks)
    ))


;;; ook?
;;; ----
;;; The OOK compiler and banana peeler (or goalie).

(defun ook? (ooks)
  "The main request for interpretation of OOKS.

Or to peel a banana.  Works as well.  It is actually a wrapper for
gently asking the Librarian for some information.

See Also:

GENTLY-ASK-THE-LIBRARIAN
"
  (gently-ask-the-librarian ooks))


(defun gently-ask-the-librarian (ooks &optional (run t))
  "ACTUALLY, the main request for interpretation of OOKS.

Naturally, to be asked gently.

It compiles the OOKS and executes them if the optional RUN parameter is T;
it returns the compiled function that contains the OOKS.  The function
can then be reused as needed.

Arguments and Values:

OOKS : some 'ooks'.
RUN  : a generalized boolean, default is T.
result : a compiled function taking no arguments.


Examples:

cl-prompt> (ook? #P\"bottles.ook\")

Notes:

The compiler (which eventually calls the <strong>CL</strong> <code>compile</code>
function) may take a long time.  Whether this is due to the size of
the TAGBODY generated or whether the Librarian has run out of bananas
and went looking for them in the Unseen University kitchen, it is
unknown, but, surely, somewhat magical.

See Also:

OOK? OOK.
"
  (format t "Oook?~%")
  (let ((ooks-to-understand (ook. ooks)))
    (format t "Oook.~%Oook..~%")
    (let ((ooks (compile nil ooks-to-understand)))
      (format t "Oook!~2%")
      (when run
        (funcall ooks))
      ooks)))
  

;;;; end of file -- ook.lisp --