forked from woodrush/lambdalisp
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazyk-ulamb-blc-wrapper.cl
134 lines (114 loc) · 3.18 KB
/
lazyk-ulamb-blc-wrapper.cl
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
(if (not (boundp 'lambdacraft-loaded))
(load "./lambdacraft.cl"))
(def-lazy powerlist
(cons 128 (cons 64 (cons 32 (cons 16 (cons 8 (cons 4 (cons 2 (cons 1 nil)))))))))
(defrec-lazy int2bitlist (n powerlist cont)
(if (isnil powerlist)
(cont nil)
(do
(<- (car-pow cdr-pow) (powerlist))
(if-then-return (<= car-pow n)
(do
(<- (nextlist) (int2bitlist (- n car-pow) cdr-pow))
(cont (cons nil nextlist))))
(<- (nextlist) (int2bitlist n cdr-pow))
(cont (cons t nextlist)))))
(defrec-lazy ulambstr-to-blcstr (s)
(cond
((isnil s)
nil)
(t
(do
(<- (c-ulamb s-cdr) (s))
(<- (c-blc) (int2bitlist c-ulamb powerlist))
(cons c-blc (ulambstr-to-blcstr s-cdr))))))
(defrec-lazy bitlist2int (n powerlist cont)
(if (isnil powerlist)
(cont 0)
(do
(<- (car-pow cdr-pow) (powerlist))
(<- (car-n cdr-n) (n))
(<- (n-ret) (bitlist2int cdr-n cdr-pow))
(if car-n
(cont n-ret)
(cont (+ car-pow n-ret))))))
(defun-lazy blcchar-to-ulambchar (c cont)
(cond
((isnil c)
(cont nil))
(t
(bitlist2int c powerlist cont))))
(defun-lazy blcchar-to-lazychar (c cont)
(cond
((isnil c)
(cont nil))
(t
(bitlist2int c powerlist cont))))
(defrec-lazy blcstr-to-ulambstr (s)
(cond
((isnil s)
nil)
(t
(do
(<- (c-blc s-cdr) (s))
(<- (c-ulamb) (blcchar-to-ulambchar c-blc))
(cons c-ulamb (blcstr-to-ulambstr s-cdr))))))
;; Lazy K
(defrec-lazy blcstr-to-lazykstr (s)
(cond
((isnil s)
(inflist 256))
(t
(do
(<- (c-blc s-cdr) (s))
(<- (c-lazy) (blcchar-to-lazychar c-blc))
(cons c-lazy (blcstr-to-lazykstr s-cdr))))))
(defrec-lazy lazykstr-to-blcstr (s)
(do
(<- (c-ulamb s-cdr) (s))
(if-then-return (= 256 c-ulamb)
nil)
(<- (c-blc) (int2bitlist c-ulamb powerlist))
(cons c-blc (lazykstr-to-blcstr s-cdr))))
(defrec-lazy take-n* (n l cur-n cont)
(cond
((= n cur-n)
(cont nil l))
(t
(typematch-nil-cons l (car-l cdr-l)
;; nil case
(do
(<- (ret nextlist) (take-n* n l (succ cur-n)))
(cont (cons t ret) nextlist))
;; cons case
(do
(<- (ret nextlist) (take-n* n cdr-l (succ cur-n)))
(cont (cons car-l ret) nextlist))))))
(defun-lazy take-n (n l)
(take-n* n l 0))
(defrec-lazy bit-to-byte (s)
(typematch-nil-cons s (car-s cdr-s)
;; nil case
nil
;; cons case
(do
(<- (c s) (take-n 8 s))
(cons c (bit-to-byte s)))))
(defrec-lazy append-direct (l1 l2)
(typematch-nil-cons l1 (car-l1 cdr-l1)
;; nil case
l2
;; cons case
(cons car-l1 (append-direct cdr-l1 l2))))
(defrec-lazy byte-to-bit (s)
(typematch-nil-cons s (car-s cdr-s)
;; nil case
nil
;; cons case
(append-direct car-s (byte-to-bit cdr-s))))
(defun-lazy blc-to-lazyk-wrapper (program stdin)
(blcstr-to-lazykstr (program (lazykstr-to-blcstr stdin))))
(defun-lazy blc-to-ulamb-wrapper (program stdin)
(blcstr-to-ulambstr (program (ulambstr-to-blcstr stdin))))
(defun-lazy blc-to-bitblc-wrapper (program stdin)
(byte-to-bit (program (bit-to-byte stdin))))