-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathencode.scm
executable file
·54 lines (45 loc) · 1.25 KB
/
encode.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
#!/usr/bin/env guile
!#
(define (lambda? s)
(and (symbol? s) (char=? #\\ (car (string->list (symbol->string s))))))
(define (lambda->symbol l)
(string->symbol (substring (symbol->string l) 1)))
(define (display-index i)
(cond
((zero? i) (display "0"))
(else (display "1") (display-index (- i 1)))))
; compile takes an expression and an environment and output the BLC to stdout
(define (compile e env)
;(newline)
;(display "e : ")
;(display e)
;(newline)
;(display "env: ")
;(display env)
;(newline)
(cond
((null? e) #t)
((lambda? (car e))
(compile-lambda e env))
((> (length e) 1)
(compile-app e env))
((symbol? (car e))
(compile-var (car e) env))
(else
(compile (car e) env))))
(define (compile-lambda e env)
(display "00")
(compile (cdr e) (cons (cons (lambda->symbol (car e)) (length env)) env)))
(define (compile-app e env)
(display "01")
(compile (reverse (cdr (reverse e))) env)
(compile (list (car (reverse e))) env))
(define (compile-var v env)
(display-index (- (length env) (cdr (assq v env)))))
(define (read-all)
(let ((expr (read)))
(cond
((eof-object? expr) '())
(else (cons expr (read-all))))))
; read stdin, then compile
(compile (read-all) '())