Skip to content

Commit 94926af

Browse files
committed
Fixes #3
Removed setq name as it's more a set! scheme-style function, set-car! set-cdr! used instead of setcar setcdr, and set, setqq also removed. Wiki examples updated. Let me know if something else broken. Cheers, Jonas
1 parent 57fc9c4 commit 94926af

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

lisp.c

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// s=0; for i=1,100000 do s=s+i end; print(s);
2020
// function tail(n, s) if n == 0 then return s else return tail(n-1, s+1); end end print(tail(100000, 0))
2121

22-
// DEF(tail, (lambda (n s) (if (eq n 0) s (tail (- n 1) (+ s 1)))));
22+
// DEFINE(tail, (lambda (n s) (if (eq n 0) s (tail (- n 1) (+ s 1)))));
2323
// princ(evalGC(reads("(tail 100000 0)"), env));
2424
// -----------------------------------------------------------------
2525
// lisp.c (tail 1,000 0)
@@ -845,7 +845,7 @@ static void response(int req, char* method, char* path) {
845845
// (web 8080 (lambda (w s m p) (princ w) (princ " ") (princ s) (princ " ") (princ m) (princ " ") (princ p) (terpri) "FISH-42"))
846846
// ' | ./run
847847

848-
PRIM _setq(lisp* envp, lisp name, lisp v);
848+
PRIM _setb(lisp* envp, lisp name, lisp v);
849849

850850
int web_socket = 0;
851851

@@ -1152,7 +1152,6 @@ inline lisp getBind(lisp* envp, lisp name, int create) {
11521152
}
11531153

11541154
// like setqq but returns binding, used by setXX
1155-
// TODO: setqq will create a binding, for scheme it "should"
11561155
// 1. define, de - create binding in current environment
11571156
// 2. set! only modify existing binding otherwise give error
11581157
// 3. setq ??? (allow to define?)
@@ -1173,7 +1172,7 @@ inline PRIM _setqq(lisp* envp, lisp name, lisp v) {
11731172
// next line only needed because C99 can't get pointer to inlined function?
11741173
PRIM _setqq_(lisp* envp, lisp name, lisp v) { return _setqq(envp, name, v); }
11751174

1176-
inline PRIM _setq(lisp* envp, lisp name, lisp v) {
1175+
inline PRIM _setb(lisp* envp, lisp name, lisp v) {
11771176
lisp bind = _setqqbind(envp, name, nil, 0);
11781177
// TODO: evalGC? probably safe as steqqbind changed an existing env
11791178
// eval using our own named binding to enable recursion
@@ -1185,7 +1184,7 @@ inline PRIM _setq(lisp* envp, lisp name, lisp v) {
11851184

11861185
inline PRIM _set(lisp* envp, lisp name, lisp v) {
11871186
// TODO: evalGC? probably safe as steqqbind changed an existing env
1188-
return _setq(envp, eval(name, envp), v);
1187+
return _setb(envp, eval(name, envp), v);
11891188
}
11901189
// next line only needed because C99 can't get pointer to inlined function?
11911190
PRIM _set_(lisp* envp, lisp name, lisp v) { return _set(envp, name, v); }
@@ -1552,7 +1551,7 @@ static inline lisp eval_hlp(lisp e, lisp* envp) {
15521551
// "macro expansion" lol (replace with implementation)
15531552
// TODO: not safe if found through variable (like all!)
15541553
// TODO: keep on symbol ptr to primitive function/global, also not good?
1555-
// DEF(F,...) will then break many local passed variables
1554+
// DEFINE(F,...) will then break many local passed variables
15561555
// maybe must search all list till find null, then can look on symbol :-(
15571556
// but that's everytime? actually, not it's a lexical scope!
15581557
// TODO: only replace if not found in ENV and is on an SYMBOL!
@@ -2522,8 +2521,8 @@ lisp lisp_init() {
25222521
DEFPRIM(cons, 2, cons);
25232522
DEFPRIM(car, 1, car_);
25242523
DEFPRIM(cdr, 1, cdr_);
2525-
DEFPRIM(setcar, 2, setcar);
2526-
DEFPRIM(setcdr, 2, setcdr);
2524+
DEFPRIM(set-car!, 2, setcar);
2525+
DEFPRIM(set-cdr!, 2, setcdr);
25272526

25282527
DEFPRIM(list, 7, _quote);
25292528
DEFPRIM(length, 1, length);
@@ -2544,9 +2543,11 @@ lisp lisp_init() {
25442543

25452544
DEFPRIM(read, 1, read_);
25462545

2547-
DEFPRIM(set, -2, _set_);
2548-
DEFPRIM(setq, -2, _setq);
2549-
DEFPRIM(setqq, -2, _setqq_);
2546+
// TODO: consider introducting these that will create local bindings if no global exists, hmm bad?
2547+
//DEFPRIM(set, -2, _set_);
2548+
//DEFPRIM(setq, -2, _setq);
2549+
//DEFPRIM(setqq, -2, _setqq_);
2550+
DEFPRIM(set!, -2, _setb);
25502551

25512552
DEFPRIM(define, -7, define);
25522553
DEFPRIM(de, -7, de);
@@ -2861,7 +2862,6 @@ PRIM fibb(lisp n) { return mkint(fib(getint(n))); }
28612862

28622863
// lisp implemented library functions hardcoded
28632864
void init_library(lisp* envp) {
2864-
//SETQ(fibo, (lambda (n) (if (< n 2) 1 (+ (fibo (- n 1)) (fibo (- n 2))))));
28652865
//DEFINE(fibo, (lambda (n) (if (< n 2) 1 (+ (fibo (- n 1)) (fibo (- n 2))))));
28662866
DE((fibo (n) (if (< n 2) 1 (+ (fibo (- n 1)) (fibo (- n 2))))));
28672867
// POSSIBLE encodings to save memory:
@@ -3015,12 +3015,13 @@ static PRIM test(lisp* e) {
30153015
TEST((number? (read "42")), t);
30163016

30173017
// set, setq, setqq
3018-
TEST((setq a (+ 3 4)), 7);
3019-
TEST((setqq b a), a);
3020-
TEST(b, a);
3021-
TEST((set b 3), 3);
3022-
TEST(a, 3);
3018+
TEST((define a (+ 3 4)), 7);
3019+
//TEST((setqq b a), a);
3020+
TEST((set! b (quote a)));
30233021
TEST(b, a);
3022+
//TEST((set b 3), 3);
3023+
//TEST(a, 3);
3024+
//TEST(b, a);
30243025

30253026
// if
30263027
lisp IF = mkprim("if", -3, if_);
@@ -3043,30 +3044,30 @@ static PRIM test(lisp* e) {
30433044
TEST(((lambda (a) ((lambda (n) (+ n a)) 33)) 66), 99); // lexical scoping
30443045

30453046
// recursion
3046-
DEF(fac, (lambda (n) (if (= n 0) 1 (* n (fac (- n 1))))));
3047+
DEFINE(fac, (lambda (n) (if (= n 0) 1 (* n (fac (- n 1))))));
30473048
TEST((fac 6), 720);
30483049
TEST((fac 21), 952369152);
30493050

30503051
// tail recursion optimization test (don't blow up stack!)
3051-
DEF(bb, (lambda (b) (+ b 3)));
3052-
DEF(aa, (lambda (a) (bb a)));
3052+
DEFINE(bb, (lambda (b) (+ b 3)));
3053+
DEFINE(aa, (lambda (a) (bb a)));
30533054
TEST((aa 7), 10);
30543055

3055-
DEF(tail, (lambda (n s) (if (eq n 0) s (tail (- n 1) (+ s 1)))));
3056+
DEFINE(tail, (lambda (n s) (if (eq n 0) s (tail (- n 1) (+ s 1)))));
30563057
TEST(tail, xyz);
30573058
testss(envp, LOOPTAIL, LOOPS);
30583059

30593060
// progn, progn tail recursion
30603061
TEST((progn 1 2 3), 3);
3061-
TEST((setq a nil), nil);
3062-
TEST((progn (setq a (cons 1 a)) (setq a (cons 2 a)) (setq a (cons 3 a))),
3062+
TEST((set! a nil), nil);
3063+
TEST((progn (set! a (cons 1 a)) (set! a (cons 2 a)) (set! a (cons 3 a))),
30633064
(3 2 1));
30643065

30653066
// implicit progn in lambda
3066-
DEF(f, (lambda (n) (setq n (+ n 1)) (setq n (+ n 1)) (setq n (+ n 1))));
3067+
DEFINE(f, (lambda (n) (set! n (+ n 1)) (set! n (+ n 1)) (set! n (+ n 1))));
30673068
TEST((f 0), 3);
30683069

3069-
// PRINT((setq tailprogn (lambda (n) (progn 3 2 1 (if (= n 0) (quote ok) (tailprogn (- n 1)))))));
3070+
// PRINT((define tailprogn (lambda (n) (progn 3 2 1 (if (= n 0) (quote ok) (tailprogn (- n 1)))))));
30703071
// TEST(tailprogn, 3);
30713072
// TEST((tailprogn 10000), ok);
30723073

@@ -3095,7 +3096,7 @@ static PRIM test(lisp* e) {
30953096
TEST((mapcar car (list (cons 1 2) (cons 3 4) (cons 5 6))), (1 3 5));
30963097
TEST((mapcar cdr (list (cons 1 2) (cons 3 4) (cons 5 6))), (2 4 6));
30973098

3098-
TEST((setq a 2));
3099+
TEST((set! a 2));
30993100
TEST((list 1 2 (let ((a (+ 1 a)) (b a)) (list a (+ b b))) 5 (+(+ a (+ a a))), (1 2 (3 4) 5 6)));
31003101
TEST(a, 2);
31013102

lisp.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,17 @@ lisp list(lisp first, ...);
8080
#define END ((lisp) -1)
8181

8282
// User, macros, assume a "globaL" env variable implicitly, and updates it
83-
#define SET(sname, val) _setq(envp, sname, val)
84-
#define SETQc(sname, val) _setq(envp, symbol(#sname), val)
85-
#define SETQ(sname, val) _setq(envp, symbol(#sname), reads(#val))
86-
#define SETQQ(sname, val) _setq(envp, symbol(#sname), quote(reads(#val)))
87-
#define DEF(fname, sbody) _setq(envp, symbol(#fname), reads(#sbody))
83+
#define SET(sname, val) _setb(envp, sname, val)
84+
#define SETQc(sname, val) _setb(envp, symbol(#sname), val)
85+
#define SETQ(sname, val) _setb(envp, symbol(#sname), reads(#val))
86+
#define SETQQ(sname, val) _setb(envp, symbol(#sname), quote(reads(#val)))
8887
#define DEFINE(fname, sbody) define(envp, symbol(#fname), reads(#sbody))
8988
#define DE(all) de(envp, reads(#all))
9089
#define EVAL(what) eval(reads(#what), envp)
9190
#define PRINT(what) ({ princ(EVAL(what)); terpri(); })
9291
#define SHOW(what) ({ printf(#what " => "); princ(EVAL(what)); terpri(); })
9392
#define TEST(what, expect) testss(envp, #what, #expect)
94-
#define DEFPRIM(fname, argn, fun) _setq(envp, symbol(#fname), mkprim(#fname, argn, fun))
93+
#define DEFPRIM(fname, argn, fun) _setb(envp, symbol(#fname), mkprim(#fname, argn, fun))
9594

9695
// symbol (internalish) functions
9796
void init_symbols();

symbols.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ lisp hashsym(lisp sym, char* optionalString, int len, int create_binding) {
257257
}
258258
return MKCONS(s);
259259
} else if (!create_binding) {
260-
printf("%% Symbol unbound: "); princ(sym); terpri();
260+
printf("%% Symbol unbound: "); princ(sym);
261+
printf("\nUse (define var val) to define binding in relevant scope first!\n");
261262
error("%% Symbol unbound"); // this will show stack and go back toplevel
262263
return nil;
263264
} else {

0 commit comments

Comments
 (0)