Skip to content

Instantly share code, notes, and snippets.

@tcolgate
Created March 2, 2013 10:59
Show Gist options
  • Select an option

  • Save tcolgate/5070523 to your computer and use it in GitHub Desktop.

Select an option

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
(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