-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlisplambda.lisp
260 lines (235 loc) · 7.44 KB
/
lisplambda.lisp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
(defparameter **lambdalisp-suppress-repl** t) ;; Enters script mode and suppresses REPL messages
;; Evaluates a byte-oriented Binary Lambda Calculus (BLC) Program.
;; There is `lisplambda-bit.lisp` which runs on bit-oriented mode, that runs faster than this program.
;;
;; Usage:
;; ( cat bin/lambdalisp.blc | bin/asc2bin; cat examples-advanced/lisplambda.lisp;
;; echo "0010ab" ) | bin/uni
;;
;; Specifications:
;; - The input is in the format of: [code][stdin]
;; - The interpreter first parses the [code] as a BLC program.
;; - All characters except for `0` and `1` are ignored.
;; - As soon as the BLC program finishes (note that the BLC language is a prefix code),
;; the interpreter starts parsing the [stdin].
;; - [stdin] is the standard input provided to the BLC program.
;; - The interpreter reads one line of standard input.
;; All characters are buffered into a BLC list until it hits the first newline (0x0a).
;; - Note that due to the specifications of LambdaLisp,
;; the program crashes when a trailing newline does not exist.
;;
;; Example programs:
;; - Echo program:
;; $ ( cat bin/lambdalisp.blc | bin/asc2bin; cat examples-advanced/lisplambda.lisp;
;; echo "0010ab" ) | bin/uni
;; > ab
;; - Corresponds to (lambda (stdin) stdin).
;; - Prepend parts of the stdin:
;; $ ( cat bin/lambdalisp.blc | bin/asc2bin; cat examples-advanced/lisplambda.lisp;
;; echo "00 00 01 01 10 01 110 0000110 110ab" ) | bin/uni
;; > aab
;; - Corresponds to (lambda (stdin) (cons (car stdin) stdin)).
;;================================================================================
;; Krivine machine
;;================================================================================
(defun read-print-01char ()
(let ((c (read-char)))
(cond
((or (= "0" c) (= "1" c))
(format t c)
c)
(t
(read-print-01char)))))
(defun parsevarname-stdin ()
(let ((c (read-print-01char)))
(cond
((= "0" c)
0)
(t
(+ 1 (parsevarname-stdin))))))
(defun parseblc-stdin ()
(let ((c (read-print-01char)))
(cond
((= c "0")
(setq c (read-print-01char))
(cond
((= c "0")
(cons 'L (parseblc-stdin)))
;; 1 case
(t
(list (parseblc-stdin) (parseblc-stdin)))))
;; 1 case
(t
(parsevarname-stdin)))))
(defun nth (n l)
(cond
((= 0 n)
(car l))
(t
(nth (- n 1) (cdr l)))))
(defun krivine (et ep ee isouter)
(let ((tmp nil))
(loop
(cond
;; Variable
((integerp et)
(setq tmp (nth et ee))
(setq et (car tmp))
(setq ee (cdr tmp)))
;; Abstraction
((eq 'L (car et))
;; If the stack is empty, finish the evaluation
(cond ((eq nil ep)
(cond
(isouter
;; Exit when EOF == nil is detected
(cond ((isnil et) (return-from krivine et)))
;; Evaluate the leading character of the current output buffer,
;; assuming that the current term is a list of integers
(setq tmp (list et ltrue))
(setq tmp (krivine tmp ep ee nil))
;; Print the leading character
(format t (lchar2char tmp))
;; Evaluate the rest of the output buffer,
;; assuming that the current term is a list of integers
(setq et (list et lnil)))
(t
;; Finish evaluating the leading character
(return-from krivine et))))
(t
(setq et (cdr et))
(setq ee (cons (car ep) ee))
(setq ep (cdr ep)))))
;; Empty term
((eq nil et)
(return-from krivine et))
;; Application
(t
(setq ep
(cons
(cons (car (cdr et)) ee)
ep))
(setq tmp (car et))
(setq et tmp))))))
;;================================================================================
;; I/O
;;================================================================================
(defparameter ltrue (cons 'L (cons 'L 1)))
(defparameter lnil (cons 'L (cons 'L 0)))
(defun lcons (a b)
(cons 'L `((0 ,a) ,b)))
(defun isnil (e)
(and
(eq 'L (car e))
(eq 'L (car (cdr e)))
(= 0 (cdr (cdr e)))))
(defun lcar (l)
(car (cdr (car (cdr l)))))
(defun lcdr (l)
(car (cdr (cdr l))))
(defun list2llist (l)
(cond
((eq nil l) lnil)
(t
(lcons
(if (= 0 (car l)) ltrue lnil)
(list2llist (cdr l))))))
(defun lbool2int (i)
(cond
((and
(eq 'L (car i))
(eq 'L (car (cdr i))))
(cond
((= 1 (cdr (cdr i)))
(return 0))
((= 0 (cdr (cdr i)))
(return 1)))))
2)
(defparameter *powerlist* (list 128 64 32 16 8 4 2 1))
(defun lint2int* (i powerlist)
(cond
((eq nil powerlist) 0)
((isnil i) 0)
(t
(let ((n (lbool2int (lcar i))))
(cond
((= 0 n)
(lint2int* (lcdr i) (cdr powerlist)))
((= 1 n)
(+ (car powerlist) (lint2int* (lcdr i) (cdr powerlist))))
(t
(error "The argument is not a number")))))))
(defun lint2int (i)
(lint2int* i *powerlist*))
(defun int2lint* (n powerlist)
(cond
((eq nil powerlist)
nil)
((<= (car powerlist) n)
(lcons lnil (int2lint* (- n (car powerlist)) (cdr powerlist))))
(t
(lcons ltrue (int2lint* n (cdr powerlist))))))
(defun int2lint (n)
(int2lint* n *powerlist*))
(defparameter *newline* "
")
(defparameter *chartable* (list
"[0x00]" "[0x01]" "[0x02]" "[0x03]" "[0x04]" "[0x05]" "[0x06]" "[0x07]"
"[0x08]" "[0x09]" *newline* "[0x0b]" "[0x0c]" "[0x0d]" "[0x0e]" "[0x0f]"
"[0x10]" "[0x11]" "[0x12]" "[0x13]" "[0x14]" "[0x15]" "[0x16]" "[0x17]"
"[0x18]" "[0x19]" "[0x1a]" "[0x1b]" "[0x1c]" "[0x1d]" "[0x1e]" "[0x1f]"
" " "!" "\"" "#" "$" "%" "&" "'" "(" ")" "*" "+" "," "-" "." "/"
"0" "1" "2" "3" "4" "5" "6" "7" "8" "9" ":" ";" "<" "=" ">" "?"
"@" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O"
"P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z" "[" "\\" "]" "^" "_"
"`" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o"
"p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "{" "|" "}" "-" "[0x7f]"))
(defun char2lchar (c)
(let ((i 0) (l *chartable*) (c2 (car *chartable*)))
(loop
(cond
((= c c2)
(return-from char2lchar (int2lint i)))
;; TODO: Handle special bytes
((< 127 i)
(return (int2lint 0))))
(setq i (+ 1 i))
(setq l (cdr l))
(setq c2 (car l)))))
(lambda (stdin)
(cons (car stdin) stdin))
(defun str2lstr (s)
(cond
((eq "" s)
lnil)
(t
(lcons (char2lchar (carstr s)) (str2lstr (cdrstr s))))))
(defun lchar2char (c)
(nth (lint2int c) *chartable*))
(defun lstr2str (s)
(cond
((isnil s)
"")
(t
(+ (lchar2char (lcar s)) (lstr2str (lcdr s))))))
(defun lreadline ()
(let ((c (read-char)))
(cond
((= c *newline*)
lnil)
(t
(lcons (char2lchar c) (lreadline))))))
(defun main ()
(let ((parsed nil) (stdin nil) (program nil) (result nil) (result-text nil))
(format t "~%")
(format t "Code: ")
(setq parsed (parseblc-stdin))
(format t "~%")
(format t "Parsed: ~a~%" parsed)
(setq stdin (lreadline))
(format t "Stdin: ~a~%" stdin)
(setq program (list parsed stdin))
(format t "Output:~%")
(krivine program nil nil t)
(exit)))
(main)