Revision 7, 1.0 kB
(checked in by naoya_t, 17 years ago)
|
Zu interpreter for Gauche: initial import
|
Line | |
---|
1 | ;;; |
---|
2 | ;;; Zu - ported from yhara's Ruby implementation, by naoya_t |
---|
3 | ;;; |
---|
4 | (define-module nt.zu |
---|
5 | (export |
---|
6 | zu-interpreter |
---|
7 | )) |
---|
8 | |
---|
9 | (select-module nt.zu) |
---|
10 | |
---|
11 | (use nt.textgraph) ;require "nt/textgraph" |
---|
12 | (use srfi-1) ;filter |
---|
13 | |
---|
14 | (define (zu-interpreter src) |
---|
15 | (let* ([tg (parse-textgraph src)] |
---|
16 | [cells [tg'cells]] |
---|
17 | [links [tg'links]]) |
---|
18 | |
---|
19 | (define (ask msg) |
---|
20 | (display msg) |
---|
21 | (display " [y/n]\n> ") |
---|
22 | (let loop () |
---|
23 | (case (read-char) |
---|
24 | ((#\y #\Y) #t) |
---|
25 | ((#\n #\N) #f) |
---|
26 | (else (loop))))) |
---|
27 | |
---|
28 | (define (say msg) |
---|
29 | (print msg) |
---|
30 | #t) |
---|
31 | |
---|
32 | (define (eval inst) |
---|
33 | (case (car inst) |
---|
34 | ((ask) (ask (cadr inst))) |
---|
35 | ((say) (say (cadr inst))) |
---|
36 | (else #f))) |
---|
37 | |
---|
38 | (let loop ([i 0]) |
---|
39 | (let* ([cell (ref cells i)] |
---|
40 | [inst-str (string-append "(" [cell'content] ")")] |
---|
41 | [inst (read-from-string inst-str)] |
---|
42 | [ret (eval inst)] |
---|
43 | [nexts (map cdr (filter (lambda (link) (= i (car link))) links))] |
---|
44 | ) |
---|
45 | (cond ((= 0 (length nexts)) 'done) |
---|
46 | ((= 1 (length nexts)) (loop (car nexts))) |
---|
47 | (else |
---|
48 | (if ret |
---|
49 | (loop (cadr nexts)) |
---|
50 | (loop (car nexts))))) |
---|
51 | )))) |
---|
52 | |
---|
53 | (provide "nt/zu") |
---|
54 | ;;EOF |
---|