-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmlis2.c
1371 lines (1135 loc) · 30.6 KB
/
mlis2.c
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* MonoLis (minimam Lisp interpreter)
written by kenichi sasagawa 2016/3
special thanks to Tsuyoshi Uema
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <setjmp.h>
#include "mlis2.h"
cell heap[HEAPSIZE];
int stack[STACKSIZE];
int argstk[STACKSIZE];
token stok = {GO,OTHER};
jmp_buf buf;
int cell_hash_table[HASHTBSIZE];
int main(void){
printf("MonoLis Ver0.02\n");
initcell();
initsubr();
int ret = setjmp(buf);
repl:
if(ret == 0)
while(1){
printf("> "); fflush(stdout); fflush(stdin);
print(eval(read ()));
printf("\n"); fflush(stdout);
}
else
if(ret == 1){
ret = 0;
goto repl;
}
else
return 0;
}
//-----------------------------
void initcell(void){
int addr,x;
for(addr=0; addr < HEAPSIZE; addr++){
heap[addr].flag = FRE;
heap[addr].cdr = addr+1;
}
hp = 0;
fc = HEAPSIZE;
for(x=0; x<HASHTBSIZE; x++)
cell_hash_table[x] = NIL;
//0番地はnil、環境レジスタを設定する。初期環境
ep = makesym("nil");
assocsym(makesym("nil"),NIL);
assocsym(makesym("t"),makesym("t"));
sp = 0;
ap = 0;
}
int freshcell(void){
int res;
res = hp;
hp = heap[hp].cdr;
SET_CDR(res,0);
fc--;
return(res);
}
//deep-bindによる。シンボルが見つからなかったら登録。
//見つかったらそこに値をいれておく。
void bindsym(int sym, int val){
int addr;
addr= assoc(sym,ep);
if(addr == 0)
assocsym(sym,val);
else
SET_CDR(addr,val);
}
//変数の束縛
//ローカル変数の場合は以前の束縛に積み上げていく。
//したがって同じ変数名があったとしても書き変えない。
void assocsym(int sym, int val){
ep = cons(cons(sym,val),ep);
}
//環境は次のように連想リストになっている。
// env = ((sym1 . val1) (sym2 . val2) ...(t . t)(nil . 0))
// assocでシンボルに対応する値を探す。
//見つからなかった場合には-1を返す。
int findsym(int sym){
int addr;
addr = assoc(sym,ep);
if(addr == 0)
return(-1);
else
return(cdr(addr));
}
//シンボルの唯一性を確保するためのもの
int getsym(char *name, int index){
int addr;
addr = cell_hash_table[index];
while(addr != NIL){
if(strcmp(name,GET_NAME(car(addr))) == 0)
return(car(addr));
else
addr = cdr(addr);
}
return(0);
}
int addsym(char *name, int index){
int addr,res;
addr = cell_hash_table[index];
addr = cons(res=makesym1(name),addr);
cell_hash_table[index] = addr;
return(res);
}
int makesym1(char *name){
int addr;
addr = freshcell();
SET_TAG(addr,SYM);
SET_NAME(addr,name);
return(addr);
}
int hash(char *name){
int res;
res = 0;
while(*name != NUL){
res = res + (int)*name;
name++;
}
return(res % HASHTBSIZE);
}
//-------デバッグ用------------------
void cellprint(int addr){
switch(GET_FLAG(addr)){
case FRE: printf("FRE "); break;
case USE: printf("USE "); break;
}
switch(GET_TAG(addr)){
case EMP: printf("EMP "); break;
case NUM: printf("NUM "); break;
case SYM: printf("SYM "); break;
case LIS: printf("LIS "); break;
case SUBR: printf("SUBR "); break;
case FSUBR: printf("FSUBR "); break;
case FUNC: printf("FUNC "); break;
case MACRO: printf("MACRO "); break;
}
printf("%07d ", GET_CAR(addr));
printf("%07d ", GET_CDR(addr));
printf("%07d ", GET_BIND(addr));
printf("%s \n", GET_NAME(addr));
}
//ヒープダンプ
void heapdump(int start, int end){
int i;
printf("addr F TAG CAR CDR BIND NAME\n");
for(i=start; i<= end; i++){
printf("%07d ", i);
cellprint(i);
}
}
//---------ガベージコレクション-----------
void gbc(void){
int addr;
printf("enter GBC free=%d\n", fc); fflush(stdout);
gbcmark();
gbcsweep();
fc = 0;
for(addr=0; addr < HEAPSIZE; addr++)
if(IS_EMPTY(addr))
fc++;
printf("exit GBC free=%d\n", fc); fflush(stdout);
}
void markoblist(void){
int addr;
addr = ep;
while(!(nullp(addr))){
MARK_CELL(addr);
addr = cdr(addr);
}
MARK_CELL(0);
}
void markcell(int addr){
if(USED_CELL(addr))
return;
MARK_CELL(addr);
if(car(addr) != 0)
markcell(car(addr));
if(cdr(addr) != 0)
markcell(cdr(addr));
if((GET_BIND(addr) != 0) && IS_FUNC(addr))
markcell(GET_BIND(addr));
if((GET_BIND(addr) != 0) && IS_MACRO(addr))
markcell(GET_BIND(addr));
}
void gbcmark(void){
int addr, i;
//oblistをマークする。
markoblist();
//oblistからつながっているcellをマークする。
addr = ep;
while(!(nullp(addr))){
markcell(car(addr));
addr = cdr(addr);
}
//argstackからbindされているcellをマークする。
for(i = 0; i < ap; i++)
markcell(argstk[i]);
//シンボルハッシュテーブルにつながっているcellをマークする。
for(i = 0; i < HASHTBSIZE; i++)
markcell(cell_hash_table[i]);
}
void gbcsweep(void){
int addr;
addr = 0;
while(addr < HEAPSIZE){
if(USED_CELL(addr))
NOMARK_CELL(addr);
else{
clrcell(addr);
SET_CDR(addr,hp);
hp = addr;
}
addr++;
}
}
void clrcell(int addr){
SET_TAG(addr,EMP);
free(heap[addr].name);
heap[addr].name = NULL;
SET_CAR(addr,0);
SET_CDR(addr,0);
SET_BIND(addr,0);
}
//自由セルが一定数を下回った場合にはgbcを起動する。
void checkgbc(void){
if(fc < FREESIZE)
gbc();
}
//--------------リスト操作---------------------
int car(int addr){
return(GET_CAR(addr));
}
int caar(int addr){
return(car(car(addr)));
}
int cdar(int addr){
return(cdr(car(addr)));
}
int cdr(int addr){
return(GET_CDR(addr));
}
int cadr(int addr){
return(car(cdr(addr)));
}
int caddr(int addr){
return(car(cdr(cdr(addr))));
}
int cadar(int addr){
return(car(cdr(car(addr))));
}
int cons(int car, int cdr){
int addr;
addr = freshcell();
SET_TAG(addr,LIS);
SET_CAR(addr,car);
SET_CDR(addr,cdr);
return(addr);
}
int assoc(int sym, int lis){
if(nullp(lis))
return(0);
else if(eqp(sym, caar(lis)))
return(car(lis));
else
return(assoc(sym,cdr(lis)));
}
int length(int addr){
int len = 0;
while(!(nullp(addr))){
len++;
addr = cdr(addr);
}
return(len);
}
int list(int arglist){
if(nullp(arglist))
return(NIL);
else
return(cons(car(arglist),list(cdr(arglist))));
}
int append(int x, int y){
if(nullp(x))
return(y);
else
return(cons(car(x),append(cdr(x),y)));
}
//----------------------------------------
int makenum(int num){
int addr;
addr = freshcell();
SET_TAG(addr,NUM);
SET_NUMBER(addr,num);
return(addr);
}
int makesym(char *name){
int index,res;
index = hash(name);
if((res=getsym(name, index)) != 0)
return(res);
else
return(addsym(name,index));
}
//スタック。ep環境ポインタの保存用
void push(int pt){
stack[sp++] = pt;
}
int pop(void){
return(stack[--sp]);
}
//arglistスタックのpush/pop
void argpush(int addr){
argstk[ap++] = addr;
}
void argpop(void){
--ap;
}
//-------read()--------
void gettoken(void){
char c;
int pos;
if(stok.flag == BACK){
stok.flag = GO;
return;
}
if(stok.ch == ')'){
stok.type = RPAREN;
stok.ch = NUL;
return;
}
if(stok.ch == '('){
stok.type = LPAREN;
stok.ch = NUL;
return;
}
c = getchar();
while((c == SPACE) || (c == EOL) || (c == TAB))
c=getchar();
switch(c){
case '(': stok.type = LPAREN; break;
case ')': stok.type = RPAREN; break;
case '\'': stok.type = QUOTE; break;
case '.': stok.type = DOT; break;
case '`': stok.type = BACKQUOTE; break;
case ',': stok.type = COMMA; break;
case '@': stok.type = ATMARK; break;
default: {
pos = 0; stok.buf[pos++] = c;
while(((c=getchar()) != EOL) && (pos < BUFSIZE) &&
(c != SPACE) && (c != '(') && (c != ')') &&
(c != '`') && (c != ',') && (c != '@'))
stok.buf[pos++] = c;
stok.buf[pos] = NUL;
stok.ch = c;
if(numbertoken(stok.buf)){
stok.type = NUMBER;
break;
}
if(symboltoken(stok.buf)){
stok.type = SYMBOL;
break;
}
stok.type = OTHER;
}
}
}
int numbertoken(char buf[]){
int i;
char c;
if(((buf[0] == '+') || (buf[0] == '-'))){
if(buf[1] == NUL)
return(0); // case {+,-} => symbol
i = 1;
while((c=buf[i]) != NUL)
if(isdigit(c))
i++; // case {+123..., -123...}
else
return(0);
}
else {
i = 0; // {1234...}
while((c=buf[i]) != NUL)
if(isdigit(c))
i++;
else
return(0);
}
return(1);
}
int symboltoken(char buf[]){
int i;
char c;
if(isdigit(buf[0]))
return(0);
i = 0;
while((c=buf[i]) != NUL)
if((isalpha(c)) || (isdigit(c)) || (issymch(c)))
i++;
else
return(0);
return(1);
}
int issymch(char c){
switch(c){
case '!':
case '?':
case '+':
case '-':
case '*':
case '/':
case '=':
case '<':
case '>': return(1);
default: return(0);
}
}
int read(void){
gettoken();
switch(stok.type){
case NUMBER: return(makenum(atoi(stok.buf)));
case SYMBOL: return(makesym(stok.buf));
case QUOTE: return(cons(makesym("quote"), cons(read(),NIL)));
case BACKQUOTE: return(cons(makesym("quasi-quote"), cons(read(),NIL)));
case COMMA: {gettoken();
if(stok.type == ATMARK)
return(cons(makesym("unquote-splicing"), cons(read(),NIL)));
else{
stok.flag = BACK;
return(cons(makesym("unquote"), cons(read(),NIL)));
}
}
case LPAREN: return(readlist());
default: break;
}
error(CANT_READ_ERR,"read",NIL);
return(0);
}
int readlist(void){
int car,cdr;
gettoken();
if(stok.type == RPAREN)
return(NIL);
else
if(stok.type == DOT){
cdr = read();
if(atomp(cdr))
gettoken();
return(cdr);
}
else{
stok.flag = BACK;
car = read();
cdr = readlist();
return(cons(car,cdr));
}
}
//-----print------------------
void print(int addr){
switch(GET_TAG(addr)){
case NUM: printf("%d", GET_NUMBER(addr)); break;
case SYM: printf("%s", GET_NAME(addr)); break;
case SUBR: printf("<subr>"); break;
case FSUBR: printf("<fsubr>"); break;
case FUNC: printf("<function>"); break;
case MACRO: printf("<macro>");break;
case LIS: { printf("(");
printlist(addr); break;}
default: printf("<undef>"); break;
}
}
void printlist(int addr){
if(IS_NIL(addr))
printf(")");
else
if((!(listp(cdr(addr)))) && (! (nullp(cdr(addr))))){
print(car(addr));
printf(" . ");
print(cdr(addr));
printf(")");
}
else {
print(GET_CAR(addr));
if(! (IS_NIL(GET_CDR(addr))))
printf(" ");
printlist(GET_CDR(addr));
}
}
//--------eval---------------
int eval(int addr){
int res;
if(atomp(addr)){
if(numberp(addr))
return(addr);
if(symbolp(addr)){
res = findsym(addr);
if(res == -1)
error(CANT_FIND_ERR, "eval", addr);
else
return(res);
}
}
else
if(listp(addr)){
if((symbolp(car(addr))) &&(HAS_NAME(car(addr),"quote")))
return(cadr(addr));
if((symbolp(car(addr))) &&(HAS_NAME(car(addr),"quasi-quote")))
return(eval(quasi_transfer2(cadr(addr),0)));
if(numberp(car(addr)))
error(ARG_SYM_ERR, "eval", addr);
if(subrp(car(addr)))
return(apply(car(addr),evlis(cdr(addr))));
if(fsubrp(car(addr)))
return(apply(car(addr),cdr(addr)));
if(functionp(car(addr)))
return(apply(car(addr),evlis(cdr(addr))));
if(macrop(car(addr)))
return(apply(car(addr),cdr(addr)));
}
error(CANT_FIND_ERR, "eval", addr);
return(0);
}
int apply(int func, int args){
int symaddr,varlist,body,res,macrofunc;
symaddr = findsym(func);
if(symaddr == -1)
error(CANT_FIND_ERR, "apply", func);
else {
switch(GET_TAG(symaddr)){
case SUBR: return((GET_SUBR(symaddr))(args));
case FSUBR: return((GET_SUBR(symaddr))(args));
case FUNC: { varlist = car(GET_BIND(symaddr));
body = cdr(GET_BIND(symaddr));
bindarg(varlist,args);
while(!(IS_NIL(body))){
res = eval(car(body));
body = cdr(body);
}
unbind();
return(res); }
case MACRO: { macrofunc = GET_BIND(symaddr);
varlist = car(GET_BIND(macrofunc));
body = cdr(GET_BIND(macrofunc));
bindarg(varlist,args);
while(!(IS_NIL(body))){
res = eval(car(body));
body = cdr(body);
}
unbind();
res = eval(res);
return(res);
}
default: error(ILLEGAL_OBJ_ERR, "apply", symaddr);
}
}
return(0);
}
void bindarg(int varlist, int arglist){
int arg1,arg2;
push(ep);
while(!(IS_NIL(varlist))){
arg1 = car(varlist);
arg2 = car(arglist);
assocsym(arg1,arg2);
varlist = cdr(varlist);
arglist = cdr(arglist);
}
}
void unbind(void){
ep = pop();
}
int evlis(int addr){
int car_addr,cdr_addr;
argpush(addr);
checkgbc();
if(IS_NIL(addr)){
argpop();
return(addr);
}
else{
car_addr = eval(car(addr));
argpush(car_addr);
cdr_addr = evlis(cdr(addr));
argpop();
argpop();
return(cons(car_addr,cdr_addr));
}
}
int atomp(int addr){
if((IS_NUMBER(addr)) || (IS_SYMBOL(addr)))
return(1);
else
return(0);
}
int numberp(int addr){
if(IS_NUMBER(addr))
return(1);
else
return(0);
}
int symbolp(int addr){
if(IS_SYMBOL(addr))
return(1);
else
return(0);
}
int listp(int addr){
if(IS_LIST(addr) || IS_NIL(addr))
return(1);
else
return(0);
}
int nullp(int addr){
if(IS_NIL(addr))
return(1);
else
return(0);
}
int eqp(int addr1, int addr2){
if(addr1 == addr2)
return(1);
else if((numberp(addr1)) && (numberp(addr2))
&& ((GET_NUMBER(addr1)) == (GET_NUMBER(addr2))))
return(1);
else if ((symbolp(addr1)) && (symbolp(addr2))
&& (SAME_NAME(addr1,addr2)))
return(1);
else
return(0);
}
int subrp(int addr){
int val;
val = findsym(addr);
if(val != -1)
return(IS_SUBR(val));
else
return(0);
}
int fsubrp(int addr){
int val;
val = findsym(addr);
if(val != -1)
return(IS_FSUBR(val));
else
return(0);
}
int functionp(int addr){
int val;
val = findsym(addr);
if(val != -1)
return(IS_FUNC(val));
else
return(0);
}
int macrop(int addr){
int val;
val = findsym(addr);
if(val != -1)
return(IS_MACRO(val));
else
return(0);
}
//-------エラー処理------
void error(int errnum, char *fun, int arg){
switch(errnum){
case CANT_FIND_ERR:{printf("%s can't find difinition of ", fun);
print(arg); break; }
case CANT_READ_ERR:{printf("%s can't read of ", fun);
break; }
case ILLEGAL_OBJ_ERR:{printf("%s got an illegal object ", fun);
print(arg); break; }
case ARG_SYM_ERR: {printf("%s require symbol but got ", fun);
print(arg); break; }
case ARG_NUM_ERR: {printf("%s require number but got ", fun);
print(arg); break; }
case ARG_LIS_ERR: {printf("%s require list but got ", fun);
print(arg); break; }
case ARG_LEN0_ERR: {printf("%s require 0 arg but got %d", fun, length(arg));
break; }
case ARG_LEN1_ERR: {printf("%s require 1 arg but got %d", fun, length(arg));
break; }
case ARG_LEN2_ERR: {printf("%s require 2 args but got %d", fun, length(arg));
break; }
case ARG_LEN3_ERR: {printf("%s require 3 args but got %d", fun, length(arg));
break; }
case MALFORM_ERR: {printf("%s got malformed args " ,fun);
print(arg); break; }
}
printf("\n");
longjmp(buf,1);
}
void checkarg(int test, char *fun, int arg){
switch(test){
case NUMLIST_TEST: if(isnumlis(arg)) return; else error(ARG_NUM_ERR, fun, arg);
case SYMBOL_TEST: if(symbolp(arg)) return; else error(ARG_SYM_ERR, fun, arg);
case NUMBER_TEST: if(numberp(arg)) return; else error(ARG_NUM_ERR, fun, arg);
case LIST_TEST: if(listp(arg)) return; else error(ARG_LIS_ERR, fun, arg);
case LEN0_TEST: if(length(arg) == 0) return; else error(ARG_LEN0_ERR, fun, arg);
case LEN1_TEST: if(length(arg) == 1) return; else error(ARG_LEN1_ERR, fun, arg);
case LEN2_TEST: if(length(arg) == 2) return; else error(ARG_LEN2_ERR, fun, arg);
case LEN3_TEST: if(length(arg) == 3) return; else error(ARG_LEN3_ERR, fun, arg);
}
}
int isnumlis(int arg){
while(!(IS_NIL(arg)))
if(numberp(car(arg)))
arg = cdr(arg);
else
return(0);
return(1);
}
//--------組込み関数
//subrを環境に登録する。
void defsubr(char *symname, int(*func)(int)){
bindfunc(symname, SUBR, func);
}
//fsubr(引数を評価しない組込関数)の登録。
void deffsubr(char *symname, int(*func)(int)){
bindfunc(symname, FSUBR, func);
}
void bindfunc(char *name, tag tag, int(*func)(int)){
int sym,val;
sym = makesym(name);
val = freshcell();
SET_TAG(val,tag);
SET_SUBR(val,func);
SET_CDR(val,0);
bindsym(sym,val);
}
void bindfunc1(char *name, int addr){
int sym,val;
sym = makesym(name);
val = freshcell();
SET_TAG(val,FUNC);
SET_BIND(val,addr);
SET_CDR(val,0);
bindsym(sym,val);
}
void bindmacro(char *name, int addr){
int sym,val1,val2;
sym = makesym(name);
val1 = freshcell();
SET_TAG(val1,FUNC);
SET_BIND(val1,addr);
SET_CDR(val1,0);
val2 = freshcell();
SET_TAG(val2,MACRO);
SET_BIND(val2,val1);
SET_CDR(val2,0);
bindsym(sym,val2);
}
void initsubr(void){
defsubr("+",f_plus);
defsubr("-",f_minus);
defsubr("*",f_mult);
defsubr("/",f_div);
defsubr("exit",f_exit);
defsubr("hdmp",f_heapdump);
defsubr("car",f_car);
defsubr("cdr",f_cdr);
defsubr("cons",f_cons);
defsubr("list",f_list);
defsubr("append",f_append);
defsubr("eq",f_eq);
defsubr("null",f_nullp);
defsubr("atom",f_atomp);
defsubr("oblist",f_oblist);
defsubr("gbc",f_gbc);
defsubr("read",f_read);
defsubr("eval",f_eval);
defsubr("apply",f_apply);
defsubr("print",f_print);
defsubr("=",f_numeqp);
defsubr(">",f_greater);
defsubr(">=",f_eqgreater);
defsubr("<",f_smaller);
defsubr("<=",f_eqsmaller);
defsubr("numberp",f_numberp);
defsubr("symbolp",f_symbolp);
defsubr("listp",f_listp);
deffsubr("setq",f_setq);
deffsubr("defun",f_defun);
deffsubr("defmacro",f_defmacro);
deffsubr("if",f_if);
deffsubr("begin",f_begin);
deffsubr("cond",f_cond);
}
//--subr----
int f_plus(int arglist){
int arg,res;
checkarg(NUMLIST_TEST, "+", arglist);
res = 0;
while(!(IS_NIL(arglist))){
arg = GET_NUMBER(car(arglist));
arglist = cdr(arglist);
res = res + arg;
}
return(makenum(res));
}
int f_minus(int arglist){
int arg,res;
checkarg(NUMLIST_TEST, "-", arglist);
res = GET_NUMBER(car(arglist));
arglist = cdr(arglist);
while(!(IS_NIL(arglist))){
arg = GET_NUMBER(car(arglist));
arglist = cdr(arglist);
res = res - arg;
}
return(makenum(res));
}
int f_mult(int arglist){
int arg,res;
checkarg(NUMLIST_TEST, "*", arglist);
res = GET_NUMBER(car(arglist));
arglist = cdr(arglist);
while(!(IS_NIL(arglist))){
arg = GET_NUMBER(car(arglist));
arglist = cdr(arglist);
res = res * arg;
}
return(makenum(res));
}
int f_div(int arglist){
int arg,res;
checkarg(NUMLIST_TEST, "/", arglist);
res = GET_NUMBER(car(arglist));
arglist = cdr(arglist);
while(!(IS_NIL(arglist))){
arg = GET_NUMBER(car(arglist));
arglist = cdr(arglist);
res = res / arg;
}
return(makenum(res));