-
Notifications
You must be signed in to change notification settings - Fork 4
/
decode.scm
executable file
·42 lines (35 loc) · 963 Bytes
/
decode.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
#!/usr/bin/env guile
!#
(define (lambda->symbol l)
(string->symbol (substring (symbol->string l) 1)))
(define (decode env)
(let ((c (read-char)))
(cond
((eof-object? c) #f)
((char=? c #\0) (decode-lam-or-app env))
(else (decode-var 0 env)))))
(define (decode-var d env)
(let ((c (read-char)))
(cond
((eof-object? c) #f)
((char=? c #\0)
(if (>= d (length env))
(list d)
(list (list-ref env d))))
(else (decode-var (+ d 1) env)))))
(define (decode-lam-or-app env)
(let ((c (read-char)))
(cond
((eof-object? c) #f)
((char=? c #\0) (decode-lam env))
(else (list (decode env) (decode env))))))
(define (decode-lam env)
(let ((x (gensym "\\v")))
(cons x (decode (cons (lambda->symbol x) env)))))
(define (decode-all)
(let ((e (decode '())))
(cond
(e (display e) (newline) (decode-all))
(else #f))))
;(display (decode '()))
(decode-all)