Created
March 2, 2013 10:59
-
-
Save tcolgate/5070523 to your computer and use it in GitHub Desktop.
CL State machine taken from http://www.findinglisp.com/blog/2004/06/basic-automaton-macro.html and a follow up post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defmacro define-automaton (name states &key (stop 'stop) (debug nil)) | |
| (let ((event-func (gensym "func"))) | |
| `(defun ,name (,event-func) | |
| (tagbody | |
| ,@(loop for (state-name . transitions) in states | |
| appending | |
| (list state-name | |
| `(case (funcall ,event-func) | |
| ,@(loop for (match next . actions) in transitions | |
| collecting `(,match | |
| ,@actions | |
| ,@(when debug | |
| `((format t "Matched ~A. Transitioning to state ~A.~%" ',match ',next))) | |
| (go ,next)))) | |
| `(go ,state-name))) | |
| ,stop)))) | |
| (define-automaton look-for-lisp | |
| ((start ('l found-l) | |
| ('x stop)) | |
| (found-l ('i found-i) | |
| ('l found-l) | |
| (otherwise start)) | |
| (found-i ('s found-s) | |
| ('l found-l) | |
| (otherwise start)) | |
| (found-s ('p start | |
| (format t "Found LISP~%") | |
| (return-from look-for-lisp t)) | |
| ('l found-l) | |
| (otherwise start)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment