Skip to content

Commit 2a00af2

Browse files
committed
Create friendly version
1 parent 17bd5be commit 2a00af2

File tree

3 files changed

+76
-25
lines changed

3 files changed

+76
-25
lines changed

lisp.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <string.h>
2626
#include <locale.h>
2727
#include <limits.h>
28+
#include <setjmp.h>
2829
#endif
2930

3031
/*───────────────────────────────────────────────────────────────────────────│─╗
@@ -46,6 +47,7 @@
4647
int cx; /* stores negative memory use */
4748
int dx; /* stores lookahead character */
4849
int RAM[0100000]; /* your own ibm7090 */
50+
jmp_buf undefined;
4951

5052
Intern() {
5153
int i, j, x;
@@ -157,10 +159,12 @@ Print(e) {
157159
╚────────────────────────────────────────────────────────────────────────────│*/
158160

159161
Car(x) {
162+
if (x >= 0) longjmp(undefined, x);
160163
return M[x];
161164
}
162165

163166
Cdr(x) {
167+
if (x >= 0) longjmp(undefined, x);
164168
return M[x + 1];
165169
}
166170

@@ -186,7 +190,7 @@ Pairlis(x, y, a) {
186190
}
187191

188192
Assoc(x, y) {
189-
if (!y) return 0;
193+
if (y >= 0) longjmp(undefined, x);
190194
if (x == Car(Car(y))) return Cdr(Car(y));
191195
return Assoc(x, Cdr(y));
192196
}
@@ -207,14 +211,14 @@ Apply(f, x, a) {
207211
if (f == kAtom) return Car(x) < 0 ? 0 : kT;
208212
if (f == kCar) return Car(Car(x));
209213
if (f == kCdr) return Cdr(Car(x));
214+
longjmp(undefined, f);
210215
}
211216

212217
Eval(e, a) {
213218
int A, B, C;
214-
if (e >= 0)
215-
return Assoc(e, a);
216-
if (Car(e) == kQuote)
217-
return Car(Cdr(e));
219+
if (!e) return 0;
220+
if (e > 0) return Assoc(e, a);
221+
if (Car(e) == kQuote) return Car(Cdr(e));
218222
A = cx;
219223
if (Car(e) == kCond) {
220224
e = Evcon(Cdr(e), a);
@@ -235,12 +239,20 @@ Eval(e, a) {
235239
╚────────────────────────────────────────────────────────────────────────────│*/
236240

237241
main() {
238-
int i;
242+
int x, a = 0;
239243
setlocale(LC_ALL, "");
240244
bestlineSetXlatCallback(bestlineUppercase);
241-
for(i = 0; i < sizeof(S); ++i) M[i] = S[i];
245+
for(x = 0; x < sizeof(S); ++x) M[x] = S[x];
242246
for (;;) {
243-
cx = 0;
244-
Print(Eval(Read(), 0));
247+
if (!(x = setjmp(undefined))) {
248+
x = Eval(Read(), a);
249+
if (x < 0) {
250+
a = Cons(x, a);
251+
}
252+
} else {
253+
if (x == 1) x = 0;
254+
PrintChar('?');
255+
}
256+
Print(x);
245257
}
246258
}

lisp.lisp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,15 @@ NIL
120120
((EQ (CAR E) (QUOTE COND)) (EVCON (CDR E) A))
121121
((QUOTE T) (APPLY (CAR E) (EVLIS (CDR E) A) A))))
122122
((QUOTE T) (APPLY (CAR E) (EVLIS (CDR E) A) A))))))
123+
124+
(CONS (QUOTE NOT)
125+
(QUOTE (LAMBDA (X)
126+
(COND (X (QUOTE F))
127+
((QUOTE T) (QUOTE T))))))
128+
129+
((LAMBDA (X E C)
130+
(CONS (QUOTE LAMBDA) (CONS NIL (CONS (CAR (CDR C)) NIL))))
131+
(QUOTE T)
132+
(QUOTE (LAMBDA (F) (F)))
133+
(QUOTE (COND (X (QUOTE F))
134+
((QUOTE T) (QUOTE T)))))

sectorlisp.S

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
// Compatible with the original hardware
2424

2525
.code16
26+
.set save,-10
27+
.set look,start+2
2628
.globl _start
2729
_start: .asciz "NIL" # dec %si ; dec %cx ; dec %sp
2830
kT: .asciz "T" # add %dl,(%si) boot A:\ DL=0
@@ -36,37 +38,53 @@ kCdr: .asciz "CDR" # ordering matters
3638
kCons: .asciz "CONS" # ordering matters
3739
kEq: .asciz "EQ" # needs to be last
3840

39-
begin: push %cs # that means ss = ds = es = cs
41+
begin: mov $2,%bx
42+
mov $0x8000,%cx
43+
main: cli
44+
push %cs # that means ss = ds = es = cs
4045
pop %ds # noting ljmp set cs to 0x7c00
4146
push %cs # that's the bios load address
4247
pop %es # therefore NULL points to NUL
4348
push %cs # terminated NIL string above!
4449
pop %ss # errata exists but don't care
4550
xor %sp,%sp # use highest address as stack
46-
mov $2,%bx
47-
main: mov $0x8000,%cx # dl (g_look) is zero or cr
51+
sti
4852
call GetToken
4953
call GetObject
54+
mov %dx,save
5055
call Eval
51-
xchg %ax,%si
56+
test %ax,%ax
57+
jns Print
58+
push %ax
59+
xchg %ax,%di
60+
xchg %dx,%ax
61+
call Cons
62+
xchg %ax,%dx
63+
pop %ax
64+
Print: xchg %ax,%si
5265
call PrintObject
5366
mov $'\r',%al
5467
call PutChar
5568
jmp main
5669

57-
GetToken: # GetToken():al, dl is g_look
70+
GetToken: # GetToken():al
5871
mov %cx,%di
59-
1: mov %dl,%al
72+
1: mov look,%al
6073
cmp $' ',%al
6174
jbe 2f
6275
stosb
6376
xchg %ax,%si
6477
2: call GetChar # exchanges dx and ax
78+
cmp $'\b',%al
79+
jne 4f
80+
dec %di
81+
jmp 2b
82+
4: xchg %ax,look
6583
cmp $' ',%al
6684
jbe 1b
6785
cmp $')',%al
6886
jbe 3f
69-
cmp $')',%dl # dl = g_look
87+
cmpb $')',look
7088
ja 1b
7189
3: mov %bh,(%di) # bh is zero
7290
xchg %si,%ax
@@ -123,14 +141,21 @@ Intern: push %cx # Intern(cx,di): ax
123141
jmp 1b
124142
2: rep movsb # memcpy(di,si,cx)
125143
9: pop %cx
126-
ret
144+
3: ret
145+
146+
Undef: push %ax
147+
mov $'?',%al
148+
call PutChar
149+
pop %ax
150+
mov save,%dx
151+
jmp Print
127152

128153
GetChar:xor %ax,%ax # GetChar→al:dl
129154
int $0x16 # get keystroke
130155
PutChar:mov $0x0e,%ah # prints CP-437
131156
int $0x10 # vidya service
132157
cmp $'\r',%al # don't clobber
133-
jne 1f # look xchg ret
158+
jne 3b # look xchg ret
134159
mov $'\n',%al
135160
jmp PutChar
136161

@@ -183,6 +208,15 @@ Gc: cmp %dx,%di # Gc(x:di,A:dx,B:si):ax
183208
add %dx,%ax
184209
ret
185210

211+
Assoc: mov %dx,%si # Assoc(x:ax,y:dx):ax
212+
1: test %si,%si
213+
jns Undef
214+
mov (%si),%di
215+
mov (%bx,%si),%si
216+
scasw
217+
jne 1b
218+
jmp Car
219+
186220
GetList:call GetToken
187221
cmp $')',%al
188222
je .retF
@@ -234,13 +268,6 @@ Cdr: scasw # increments our data index by 2
234268
Car: mov (%di),%ax # contents of address register!!
235269
2: ret
236270

237-
Assoc: mov %dx,%si # Assoc(x:ax,y:dx):ax
238-
1: mov (%si),%di
239-
mov (%bx,%si),%si
240-
scasw
241-
jne 1b
242-
jmp Car
243-
244271
1: mov (%bx,%di),%di # di = Cdr(c)
245272
Evcon: push %di # save c
246273
mov (%di),%si # di = Car(c)

0 commit comments

Comments
 (0)