Skip to content

Instantly share code, notes, and snippets.

@GlassGhost
Last active November 1, 2021 14:26
Show Gist options
  • Save GlassGhost/3d90c9818546fa985d3d6e876bac8ea1 to your computer and use it in GitHub Desktop.
Save GlassGhost/3d90c9818546fa985d3d6e876bac8ea1 to your computer and use it in GitHub Desktop.
gsi v4.9.3 owner@localhost:~/sync/Scripts/Lambda$ gsi ./automaton.scm *** ERROR IN "automaton.scm"@21.30 -- Ill-formed selector list
;#!/usr/bin/env scheme-r5rs
;https://github.com/carld/automata-via-macros
(define-syntax automaton ;https://cs.brown.edu/~sk/Publications/Talks/SwineBeforePerl/
(syntax-rules (:)
[(_ init-state
(state : response ...) ...)
(let-syntax
([process-state
(syntax-rules (accept ->)
[(_ accept)
(lambda (stream)
(cond
[(null? stream) #t]
[else #f]))]
[(_ (label -> target) (... ...))
(lambda (stream)
(cond
[(null? stream) #f]
[else (case (car stream)
[(label) (target (cdr stream))]
(... ...)
[else #f])]))])])
(letrec ([state (process-state response ...)] ...)
init-state))]))
(define m2
(automaton init
[init : (c -> more)]
[more : (a -> more)
(d -> more)
(r -> end)]
[end : accept]))
(define-syntax test
(syntax-rules()
((_ expression expectation)
(cond
((equal? expression expectation)
(display "TEST OK" )(newline))
(else (error "Unexpected result " expression))))))
(test (m2 `(c a d r)) #t)
(test (m2 `(c d a a d r)) #t)
(test (m2 `(r)) #f)
(test (m2 `(i)) #f)
(test (m2 `(a)) #f)
(test (m2 `(c)) #f)
(test (m2 `(c a r)) #t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment