-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomaton.scm
56 lines (51 loc) · 1.33 KB
/
automaton.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
; http://www.cs.brown.edu/~sk/Publications/Papers/Published/sk-automata-macros/
; to match words
(define true #t)
(define false #f)
(define empty? null?)
(define first car)
(define rest cdr)
; main macro
(define-syntax automaton
(syntax-rules (:)
((_ init-state
(state : response ...)
...)
(let-syntax
((process-state
(syntax-rules (accept ->)
((_ accept)
(lambda (stream)
(cond
((empty? stream) true)
(else false))))
((_ (label -> target) (... ...))
(lambda (stream)
(cond
((empty? stream) false)
(else
(case (first stream)
((label) (target (rest stream)))
(... ...)
(else false)))))))))
(letrec ((state
(process-state response ...))
...)
init-state)))))
(define m
(automaton init
(init : (c -> more))
(more : (a -> more)
(d -> more)
(r -> end))
(end : accept)))
(define run
(lambda (xs)
(display xs)
(newline)
(display (m xs))
(newline)))
(for-each run
'((c a d a)
(c a d a r)
(c a d a r r)))