|
12 | 12 | (utils "utils.scm")) |
13 | 13 | (export |
14 | 14 | (view graph) |
15 | | - (graph state-machine) |
| 15 | + (graph state-machine . title) |
16 | 16 | (graph-to-file state-machine file))) |
17 | 17 |
|
18 | 18 | (define *image-viewer* "eog") |
19 | 19 | (define *dot* "dot") |
20 | 20 |
|
21 | | -(define (graph state-machine) |
22 | | - (cond ((nfa? state-machine) |
23 | | - (graph-nfa state-machine)) |
24 | | - ((dfa? state-machine) |
25 | | - (graph-dfa state-machine)) |
26 | | - (else |
27 | | - (error "graph" "Can only produce a graph for nfa or dfa" state-machine)))) |
| 21 | +(define (graph state-machine . title) |
| 22 | + (let ((gr (cond ((nfa? state-machine) |
| 23 | + (graph-nfa state-machine)) |
| 24 | + ((dfa? state-machine) |
| 25 | + (graph-dfa state-machine)) |
| 26 | + (else |
| 27 | + (error "graph" "Can only produce a graph for nfa or dfa" state-machine))))) |
| 28 | + (if (null? title) |
| 29 | + gr |
| 30 | + (append-dot gr "graph[label=\"~a\"]" (car title))))) |
| 31 | + |
| 32 | +(define (append-dot gr fmt s) |
| 33 | + (close-dot (string-append (open-dot gr) (format fmt s)))) |
| 34 | + |
| 35 | +(define (close-dot gr) |
| 36 | + (string-append gr "}")) |
| 37 | + |
| 38 | +(define (open-dot gr) |
| 39 | + (let ((sc (string-contains gr "}"))) |
| 40 | + (if sc |
| 41 | + (string-shrink! gr sc) |
| 42 | + gr))) |
28 | 43 |
|
29 | 44 | (define (graph-to-file state-machine filename) |
30 | 45 | (call-with-output-file |
|
46 | 61 | node[shape=circle style=solid] |
47 | 62 | ~a |
48 | 63 | }") |
| 64 | + |
49 | 65 | (define dot-final-states |
50 | 66 | "node[shape=doublecircle style=solid] |
51 | 67 | ~a;") |
52 | 68 |
|
53 | 69 | (define (string-quote x) |
54 | 70 | (format "\"~a\"" x)) |
55 | 71 |
|
56 | | -(define (graph-dfa dfa) |
| 72 | +(define (graph-dfa dfa . args) |
57 | 73 | (let* ((final-states (if (null? (dfa-final-states dfa)) |
58 | 74 | "" |
59 | 75 | (format dot-final-states |
|
75 | 91 | (string-join transitions "")))) |
76 | 92 |
|
77 | 93 |
|
78 | | -(define (graph-nfa nfa) |
| 94 | +(define (graph-nfa nfa . args) |
79 | 95 | (let* ((final-states (if (null? (nfa-final-states nfa)) |
80 | 96 | "" |
81 | 97 | (format dot-final-states |
|
0 commit comments