Skip to content

Instantly share code, notes, and snippets.

@PuercoPop
Created March 15, 2014 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PuercoPop/9563095 to your computer and use it in GitHub Desktop.
Save PuercoPop/9563095 to your computer and use it in GitHub Desktop.
Friday Night Coding with @ivoscc and @marsam
(defpackage regexp
(:use :cl :fsm))
(in-package :regexp)
(match-sequence "Hello")
;; fsm
(deffsm regexp ()
())
(defun match-sequence (str)
(let* ((states (loop
for i from 0 upto (1+ (length str))
collect (gensym)))
(my-fsm (make-instance 'regexp :state (car states)))
(last-index (1+ (length str))))
(setf (elt states last-index) (gensym "FAIL"))
(values my-fsm
(append
(loop
for char across str
for index from 0 upto (1+ last-index)
collect
(defstate regexp (elt states index) (fsm event)
(if (char= char event)
(elt states (1+ index))
(elt states last-index))))
(defstate regexp (elt states last-index) (fsm event)
(prog1
(elt states last-index)
(format t "FAIL WHALE~%"))))
(elt states (1- last-index))
(elt states last-index)
states)))
;; test case
(multiple-value-bind
(fsm methods success fail states) (match-sequence "hello")
(declare (ignore methods))
(let ((input "hello"))
(map 'list
(lambda (c)
(if (eql :error (fsm:state fsm))
(format t "Skipping: ~S~%" c)
(funcall fsm c)))
input)
(values fsm (fsm:state fsm) success fail "===" states)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment