Advertisement
eudoxia

Interactive Fiction Parser

May 7th, 2011
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 6.93 KB | None | 0 0
  1. (defparameter word-ignore '("a" "the" "to" "an" "at" "for" "i" "from" "is" "it" "of"
  2.                             "over" "please" "around" "that" "these" "them" "through"))
  3.                            
  4. (defparameter modifiers '("in" "on"
  5.                           "with"
  6.                           "under" "behind"))
  7.                          
  8. (defparameter error-table '(:wrong-or-unknown-verb "I don't understand that verb.")
  9. )
  10.                            
  11. (defun error-alert (error-code)
  12.     ;(if (null (member error-code error-table))
  13.             ;(writeline "~&[Non-lethal] Error: Corresponding error code not found."))
  14.             ;(writeline "~&~a" (getf error-table error-code)))
  15. )
  16.  
  17. (defun member-of-word-ignore (atom)
  18.     (if (not (null (member atom word-ignore :test #'equalp)))
  19.         'T))
  20.        
  21. (defun announce (list)
  22.     (writeline "~&~s" list))
  23.  
  24. (defun and-parser (list)
  25.     (setf list (list-split-multiple-delimiters list "and"))
  26.     ;(writeline "~&AND-PARSER:: List after being split by 'and's: ~s" list)
  27.     (loop for n from 0 to (1- (length list)) do
  28.         (setf (nth n list) (concatenate-strings-with-spaces (nth n list))))
  29.     ;(writeline "~&AND-PARSER:: List after having the words inside lists concatenated: ~s" list)
  30. (append list))    
  31.    
  32. (defun pre-parser (string)
  33.     (let ((splat-string (split-string string #\Space)))
  34.         ;(writeline "~&Splat-string (By spaces): ~s" splat-string)
  35.         (loop for n from 0 to (length splat-string) do
  36.             (if (equal '(#\.) (last (coerce (nth n splat-string) 'list)))
  37.                 (setf splat-string (append (list-subseq splat-string 0 (1- n))
  38.                                            (list (coerce
  39.                                                     (reverse (cdr (reverse (coerce (nth n splat-string) 'list))))
  40.                                                 'string) "then")
  41.                                            (list-subseq splat-string (1+ n))))))
  42.         ;(writeline "~&After coercion of 'word.' into 'word then': ~s" splat-string)
  43.         (loop for n from 0 to (length splat-string) do
  44.             (if (equal '(#\,) (last (coerce (nth n splat-string) 'list)))
  45.                 (setf splat-string (append (list-subseq splat-string 0 (1- n))
  46.                                            (list (coerce
  47.                                                     (reverse (cdr (reverse (coerce (nth n splat-string) 'list))))
  48.                                                 'string) "and")
  49.                                            (list-subseq splat-string (1+ n))))))
  50.         ;(writeline "~&After coercion of 'word,' into 'word and': ~s" splat-string)
  51.         (setf splat-string (remove-if #'member-of-word-ignore splat-string))
  52.         ;(writeline "~&String after removal of ignored words: ~s" splat-string)
  53.         (setf splat-string (list-split-multiple-delimiters splat-string "then"))
  54.         ;(writeline "~&String after separation into multiple statements by 'then': ~s" splat-string)
  55.         (loop for n from 0 to (1- (length splat-string)) do (classify (nth n splat-string)))))
  56.  
  57. (defun classify (string)
  58.     (let* ((modifier '())
  59.            (and-exists? nil))
  60.         ;(writeline "~&Command to parse: ~s" string)
  61.         (if (null (member (first string) modifiers)) ;check whether the verb is fucked up
  62.                 (error-alert :wrong-or-unknown-verb))
  63.         (if (not (null (member "and" string :test #'equalp)))
  64.             (setf and-exists? t))
  65.         ;(writeline "~&Is there an 'and'?: ~s" and-exists?)
  66.         (loop for n from 0 to (length modifiers) do
  67.             (if (not (null (member (nth n modifiers) string :test #'equalp)))
  68.                 ;check whether the string has a modifier, if so, append its string and position
  69.                 (progn (setf modifier (append (list (nth n modifiers)) (list (car (collect-item-positions string (nth n modifiers) :single? t)))))
  70.                        (return))))
  71.         (if (null modifier)
  72.                     (progn ;(writeline "~&Type: UNMODIFIED.")   ;doesn't have modifiers
  73.                            (parser string 'unmodified and-exists?)
  74.                     )
  75.                     (progn ;(writeline "~&Type: MODIFIED.")     ;has modifiers
  76.                            (if (eql 1 (cadr modifier))
  77.                                (progn ;(writeline "~&The modifier is first-word.")
  78.                                       (parser string 'modified-first-word and-exists?)
  79.                                )
  80.                                (progn ;(writeline "~&The modifier is last-word.")
  81.                                       (parser string 'modified-last-word and-exists? :modifier-position (cadr modifier))
  82. ))))))
  83.    
  84. (defun parser (list modified? and-exists? &optional &key modifier-position)
  85.     ;(writeline "~&Parsing string: ~s" list)
  86.     ;(writeline "~&Of type: ~s" modified?)
  87.     ;(writeline "~&And exists?: ~s" and-exists?)
  88.     (cond (and-exists?
  89.             (cond ((equal modified? 'unmodified)
  90.                         (setf list (append (list (car list))
  91.                                             (list (and-parser (list-subseq list 1)))))
  92.                         (announce list)
  93.                   )
  94.                   ((equal modified? 'modified-first-word)
  95.                         (setf list (append (list (concatenate 'string (first list) "-" (second list)))
  96.                                             (list (and-parser (list-subseq list 2)))))
  97.                         (announce list)
  98.                   )
  99.                   ((equal modified? 'modified-last-word)
  100.                         (setf list (append (list (concatenate 'string (first list) "-" (nth modifier-position list)))
  101.                                             (list (and-parser (list-subseq list 1 (1- modifier-position))))
  102.                                             (list (and-parser (list-subseq list (1+ modifier-position))))))
  103.                         (announce list)
  104.                   )))
  105.           ((not and-exists?)
  106.             (cond ((equal modified? 'unmodified)
  107.                         (setf list (append (list (car list))
  108.                                             (list (list (concatenate-strings-with-spaces (cdr list))))))
  109.                         (announce list)
  110.                   )
  111.                   ((equal modified? 'modified-first-word)
  112.                         (setf list (append (list (concatenate 'string (first list) "-" (second list)))
  113.                                             (list (list (concatenate-strings-with-spaces (cddr list))))))
  114.                         (announce list)
  115.                   )
  116.                   ((equal modified? 'modified-last-word)
  117.                         (setf list (append (list (concatenate 'string (first list) "-" (nth modifier-position list)))
  118.                                             (list (list (concatenate-strings-with-spaces (list-subseq list 1 (1- modifier-position)))))
  119.                                             (list (list (concatenate-strings-with-spaces (list-subseq list (1+ modifier-position)))))))
  120.                         (announce list)
  121. )))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement