-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.c
2458 lines (2201 loc) · 51.4 KB
/
main.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
/*
* Easy-ISLisp (ISLisp) written by kenichi sasagawa 2016/4~
* main functions read,eval,print (REPL)
*
* <files>
* main.c REPL
* cell.c generate cells.
* link.c interface functions for dynamic-linked code.
* data.c predicate for data structure. operation of data structure.
* compute.c computation numeric
* bignum.c computation of bignum
* function.c builtin functions
* syntax.c syntax functions
* extension.c extended functions (non ISLisp standard)
* gbc.c garbage collenction
* edlis.c Edlis simple CUI editor
*
* <cell type summary>
* sym symbol
* int integer
* flt float
* str string
* stm stream
* arr array
* vec vector
* char character
* big bignum
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <signal.h>
#include <unistd.h>
#include <getopt.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "compat/term_stubs.h"
#include "eisl.h"
#include "mem.h"
#include "fmt.h"
#include "except.h"
#include "str.h"
#include "long.h"
#include "compat/eiffel_stubs.h"
#include "compat/curses_stubs.h"
/* pointer */
int ep[PARASIZE]; /* environment pointer */
int dp[PARASIZE]; /* dynamic pointer */
int hp[PARASIZE]; /* heap pointer for mark and sweep */
int sp[PARASIZE]; /* stack pointer */
int fc[PARASIZE]; /* free counter */
int ap[PARASIZE]; /* arglist pointer */
int lp[PARASIZE]; /* shelter pointer */
int cp; /* tag pointer for catch & throw */
/* class */
int cobject;
int cbasic_array;
int cbasic_array_star;
int cgeneral_array_star;
int cbasic_vector;
int cgeneral_vector;
int cstring;
int cbuilt_in_class;
int ccharacter;
int cfunction;
int cgeneric_function;
int cstandard_generic_function;
int clist;
int ccons;
int cnull;
int csymbol;
int cnumber;
int cfloat;
int cinteger;
int cserious_condition;
int cerror;
int carithmetic_error;
int cdivision_by_zero;
int cfloating_point_overflow;
int cfloating_point_underflow;
int ccontrol_error;
int cparse_error;
int cprogram_error;
int cdomain_error;
int cclass_error;
int cundefined_entity;
int cunbound_variable;
int cundefined_function;
int csimple_error;
int cstream_error;
int cend_of_stream;
int cstorage_exhausted;
int cstandard_class;
int cstandard_object;
int cstream;
int cinvalid;
int cfixnum;
int clongnum;
int cbignum;
/* stream */
int standard_input;
int standard_output;
int standard_error;
int input_stream;
int output_stream;
int error_stream;
char stream_str[STRSIZE];
int charcnt; /* for format-tab. store number of chars up to now. */
/* read scaner */
token stok;
int line;
int column;
char buffer[COL_SIZE + 1][NUM_HISTORY];
char buffer1[COL_SIZE + 1];
char buffer2[COL_SIZE + 1] = { 0 }; //for read_stdin()
char buffer3[STRSIZE] = { 0 }; //for Multi-Process and Distributed TCP/IP
/* heap ,stack and bignum */
cell heap[CELLSIZE];
int stack[STACKSIZE][PARASIZE];
int argstk[STACKSIZE][PARASIZE];
int cell_hash_table[HASHTBSIZE];
int shelter[STACKSIZE][PARASIZE];
int dynamic[DYNSIZE][2][PARASIZE];
int bigcell[BIGSIZE];
/* object oriented */
int generic_func; /* generic function in eval. */
int generic_vars; /* args list of generic function in eval. */
int next_method; /* head address of finded method. */
int generic_list = NIL; /* symbol list of generic function. */
int setf_list = NIL; /* generic function related setf */
/* system global variable */
int gArgC;
char **gArgV;
int genint = 1; /* integer of gensym */
/* flag */
bool gbc_flag = false; /* false=GC not display ,true= do display. */
bool simp_flag = true; /* true=simplify, false=Not for bignum */
bool ignore_flag = false; /* false=normal,true=ignore error */
bool open_flag = false; /* false=normal,true=now loading */
bool top_flag = true; /* true=top-level,false=not-top-level */
bool redef_flag = false; /* true=redefine-class,false=not-redefine */
bool start_flag = true; /* true=line-start,false=not-line-start */
bool back_flag = true; /* for backtrace,true=on,false=off */
bool ignore_topchk = false; /* for FAST-compiler true=ignore,false=normal */
#ifndef WITHOUT_CURSES
bool repl_flag = true; /* for REPL read_line true=on,false=off */
bool org_repl_flag = true; /* original val for restore */
#endif
bool option_flag = false; /* while handling command line option it is true, else false */
volatile sig_atomic_t exit_flag = 0; /* true= ctrl+C */
bool greeting_flag = true; /* for (quit) */
bool script_flag = false; /* for -s option */
bool handling_resource_err = false; /* stop infinite recursion */
bool looking_for_shebang = false; /* skip over #! */
bool multiple_call_next_method; /* method body has multiple (call-next-method) */
bool error_flag = false; /* invoked error? */
bool parallel_flag = false; /* while executing parallel */
bool parallel_exit_flag = false; /* To exit parallel threads */
bool process_flag = false; /* when invoke as child process, flag is true */
bool thread_flag = false; /* when invoke as multi thread, flag is true */
bool network_flag = false; /* when invoke as network child, flag is true */
bool connect_flag = false; /* when child listen, connect_flag is true */
bool receiver_exit_flag = false; /* TO exit child TCP/IP receiver */
bool child_busy_flag = false; /* while evalating in child, child_buzy_flag is true */
/* try function (try time s-exp binary) */
bool try_flag; /* true or false */
double try_timer; /* limit timer */
int try_res; /* argument list */
/* bignum pointer */
int big_pt0 = 0; /* pointer of temporaly bignum */
int big_pt1 = BIGNUM_PARMA; /* pointer of parmanent bignum */
/* longjmp control and etc */
Except_T Restart_Repl = { "Restart REPL" },
Exit_Interp = { "Exit interpreter" };
jmp_buf block_buf[CTRLSTK];
jmp_buf catch_buf[CTRLSTK];
jmp_buf cont_buf;
Except_T Ignored_Error = { "Ignored error" }; /* for ignore-errors */
Except_T Exit_Thread = { "Exit thread" }; /* for Multi-thread */
Except_T Exit_Process = { "Exit Process" }; /* for Multi-process */
Except_T Exit_Network = { "Exit Network" }; /* for Distributed parallel */
int signal_condition_x;
int signal_condition_y;
int block_tag_check[CTRLSTK];
int block_data[CTRLSTK][3];
int catch_data[CTRLSTK][4]; /* new data type for catch tag */
int unwind_buf[CTRLSTK];
int block_pt; /* index of block. following are similer */
int catch_pt = 0; /* catch counter */
int unwind_pt; /* lambda address for unwind-protect */
int block_arg; /* receive argument of block */
int catch_arg; /* receive argument of catch */
int cont_arg; /* receive argument of continue-condition */
int tagbody_tag = NIL; /* tag address fo tagbody */
int error_handler = NIL; /* for store first argument of with-handler */
int error_handler1 = NIL; /* for restore error_handler */
int trace_list = NIL; /* function list of trace */
int backtrace[BACKSIZE];
int unwind_nest; /* unwind-protect nest level */
int process_arg; /* when -p option child process number */
/* multi thread */
pthread_mutex_t mutex;
pthread_mutex_t mutex1;
int mt_queue[PARASIZE];
int mt_queue_pt;
int mt_queue_num;
int thread_num = 1;
int para_input[PARASIZE];
int para_output[PARASIZE];
pthread_t mt_para_thread[PARASIZE];
pthread_cond_t mt_cond_para[PARASIZE];
pthread_cond_t mt_cond_main;
pthread_cond_t mt_cond_queue;
pthread_attr_t mt_para_attr[PARASIZE];
size_t mt_para_size[PARASIZE];
/*multi proccess*/
int pipe_p2c[PROCSIZE][2];
int pipe_c2p[PROCSIZE][2];
pid_t pid[PROCSIZE];
int process_pt = 0;
int process_num;
struct sigaction child_action;
int child_signal[PROCSIZE];
int child_signal1[PROCSIZE];
/* -----distributed parallel & TCPIP------*/
int sockfd[PARASIZE];
socklen_t parent_len;
struct sockaddr_in parent_addr, child_addr[PARASIZE];
int child_num;
pthread_t receiver_thread;
int child_result[PARASIZE];
/* -----TCPIP for server----------------*/
socklen_t server_len;
struct sockaddr_in server_addr, client_addr;
/* -----debugger----- */
int examin_sym;
int stepper_flag = 0;
/* -----profiler----- */
int prof_sw = 0; /* 0= not profiler, 1=system-function 2=user-function */
int prof_sym[PROFSIZE];
int prof_pt = 1;
double prof_dt0[PROFSIZE];
int prof_dt1[PROFSIZE];
/* -----editor------- */
int ed_lparen_col;
int ed_rparen_col;
const char *ed_candidate[COMPLETION_CANDIDATES_MAX];
int ed_candidate_pt;
const short ed_syntax_color = COLOR_RED;
const short ed_builtin_color = COLOR_CYAN;
const short ed_extended_color = COLOR_MAGENTA;
const short ed_string_color = COLOR_YELLOW;
const short ed_comment_color = COLOR_BLUE;
int ed_incomment = -1; /* #|...|# comment */
/* Defaults, should be filled in later */
char ed_key_up = 'A';
char ed_key_down = 'B';
char ed_key_right = 'C';
char ed_key_left = 'D';
static void usage(void)
{
puts("List of options:\n"
"-c -- EISL starts after reading compiler.lsp.\n"
"-f -- EISL starts after reading formatter.lsp.\n"
"-h -- display help.\n"
"-l filename -- EISL starts after reading the file.\n"
"-n -- EISL starts as network child mode.\n"
"-r -- EISL does not use editable REPL.\n"
"-s filename -- EISL runs the file with script mode.\n"
"-v -- display version number.");
}
static inline void maybe_greet(void)
{
if (greeting_flag)
Fmt_print("Easy-ISLisp Ver%1.2f\n", VERSION);
}
static inline void disable_repl_flag(void)
{
#ifndef WITHOUT_CURSES
repl_flag = false;
org_repl_flag = false;
#endif
}
int main(int argc, char *argv[])
{
int errret;
Fmt_register('D', cvt_D);
if (setupterm((char *) 0, 1, &errret) == ERR ||
key_up == NULL || key_down == NULL ||
key_right == NULL || key_left == NULL) {
disable_repl_flag();
} else {
ed_key_down = key_down[2];
ed_key_left = key_left[2];
ed_key_right = key_right[2];
ed_key_up = key_up[2];
}
init_stok();
init_cell();
init_class();
init_stream();
init_subr();
init_exsubr();
init_syntax();
init_generic();
init_dp();
init_pointer();
/* ctrl+c */
signal(SIGINT, signal_handler_c);
signal(SIGSTOP, SIG_IGN);
/* signal for read_from_pipe_part() */
memset(&child_action, 0, sizeof(child_action));
child_action.sa_sigaction = &signal_handler_child;
child_action.sa_flags = SA_SIGINFO;
#ifdef __linux__
sigaction(SIGRTMIN, &child_action, NULL);
#else
sigaction(SIGUSR1, &child_action, NULL);
#endif
if (setenv("EASY_ISLISP", STRQUOTE(SHAREDIR), /* overwrite = */ 0) ==
-1) {
perror("setenv");
exit(EXIT_FAILURE);
}
input_stream = standard_input;
output_stream = standard_output;
error_stream = standard_error;
int ch;
char *script_arg;
/* handle command line options */
option_flag = true;
TRY {
if (access("startup.lsp", R_OK) == 0)
f_load(list1(make_str("startup.lsp")), 0);
while ((ch = getopt(argc, argv, "l:s:p:cfrhvn")) != -1) {
char *str;
switch (ch) {
case 'l':
if (f_probe_file(list1(make_str(optarg)), 0) == T) {
f_load(list1(make_str(optarg)), 0);
} else {
puts("File doesn't exist.");
exit(EXIT_FAILURE);
}
break;
case 'c':
str = library_file("compiler.lsp");
f_load(list1(make_str(str)), 0);
FREE(str);
break;
case 'f':
str = library_file("formatter.lsp");
f_load(list1(make_str(str)), 0);
FREE(str);
break;
case 's':
if (access(optarg, R_OK) == -1) {
puts("File doesn't exist.");
exit(EXIT_FAILURE);
}
disable_repl_flag();
script_flag = true;
looking_for_shebang = true;
script_arg = optarg;
break;
case 'r':
disable_repl_flag();
break;
case 'p':
process_flag = true;
process_num = strtol(optarg, NULL, 10);
break;
case 'n':
puts("EISL runs with network mode.");
network_flag = true;
init_parent();
init_receiver();
str = library_file("compiler.lsp");
f_load(list1(make_str(str)), 0);
FREE(str);
break;
case 'v':
Fmt_print("Easy-ISLisp Ver%1.2f\n", VERSION);
exit(EXIT_SUCCESS);
case 'h':
usage();
exit(EXIT_SUCCESS);
default:
usage();
exit(EXIT_FAILURE);
}
}
gArgC = argc - optind;
gArgV = argv + optind;
if (script_flag) {
f_load(list1(make_str(script_arg)), 0);
exit(EXIT_SUCCESS);
}
}
EXCEPT(Restart_Repl) exit(EXIT_FAILURE);
END_TRY;
option_flag = false;
/* REPL */
volatile bool quit = false;
do {
if (!process_flag)
maybe_greet();
TRY while (1) {
init_pointer();
if (!process_flag && !network_flag) {
fputs("> ", stdout);
print(eval(sread(), 0));
putchar('\n');
} else if (process_flag) {
TRY print(eval(sread(), 0));
putchar('\n');
fflush(stdout);
union sigval value;
value.sival_int = (int) process_num;
#ifdef __linux__
sigqueue(getppid(), SIGRTMIN, value);
#else
kill(getppid(), SIGUSR1);
#endif
EXCEPT(Exit_Process);
END_TRY;
} else if (network_flag) {
int exp, res;
exp = str_to_sexp(receive_from_parent());
printf("receive_from_parent ");
print(exp);
printf("\n");
fflush(stdout);
if (equalp(exp, make_int(999))) {
printf("exit_from_network_mode\n");
close_socket();
exit(0);
} else {
TRY child_busy_flag = true;
res = eval(exp, 0);
child_busy_flag = false;
printf("send_to_parent ");
print(res);
printf("\n");
fflush(stdout);
send_to_parent(sexp_to_str(res));
EXCEPT(Exit_Network);
END_TRY;
}
}
if (redef_flag)
redef_generic();
}
EXCEPT(Restart_Repl);
EXCEPT(Exit_Interp) {
quit = true;
exit_thread();
close_socket();
}
END_TRY;
}
while (!quit);
}
char *library_file(const char *basename)
{
char *prefix;
if ((prefix = getenv("EASY_ISLISP")) != NULL) {
return Str_catv(prefix, 1, 0, "/library/", 1, 0, basename, 1, 0,
NULL);
}
return Str_cat(STRQUOTE(SHAREDIR) "/library/", 1, 0, basename, 1, 0);
}
void init_dp(void)
{
int i;
for (i = 0; i < PARASIZE; i++)
dp[i] = 0;
}
void init_pointer(void)
{
int i, ls;
for (i = 0; i < PARASIZE; i++) {
ep[i] = 0;
sp[i] = 0;
ap[i] = 0;
lp[i] = 0;
}
cp = 0;
block_pt = 0;
catch_pt = 0;
unwind_pt = 0;
error_handler = NIL;
top_flag = true;
start_flag = true;
error_flag = false;
charcnt = 0;
generic_func = NIL;
generic_vars = NIL;
big_pt0 = 0;
unwind_nest = 0;
/* clear nest level of tracing function. */
ls = trace_list;
while (!nullp(ls)) {
SET_TR(GET_CAR(car(ls)), 0);
ls = cdr(ls);
}
}
void exit_thread(void)
{
/* exit parallel function thread */
exit_para();
}
void signal_handler_c(int signo __unused)
{
exit_flag = 1;
}
void signal_handler_child(int sig, siginfo_t * siginfo, void *context)
{
int n;
n = siginfo->si_value.sival_int;
child_signal[n] = 1;
}
/* -------read()-------- */
int string_readc(int stm __unused)
{
int c;
c = GET_NAME(input_stream)[GET_CDR(input_stream)];
SET_CDR(input_stream, GET_CDR(input_stream) + 1);
if (c == '\\') {
c = GET_NAME(input_stream)[GET_CDR(input_stream)];
SET_CDR(input_stream, GET_CDR(input_stream) + 1);
} else if (c == NUL) {
c = EOF;
SET_CDR(input_stream, GET_CDR(input_stream) - 1);
}
return (c);
}
int readc(void)
{
int c;
if (string_input_stream_p(input_stream)) {
/* string-input-stream */
return (string_readc(input_stream));
} else if (process_flag == true) {
/* EISL as child process */
return (read_stdin());
} else if (input_stream == standard_input && repl_flag) {
/* REPL-mode and standard-input */
c = read_line(1);
} else {
/* not REPL-mode and standard-input */
c = getc(GET_PORT(input_stream));
if (!script_flag && input_stream == standard_input && c == EOF) {
/* ctrl+D and not script-mode quit system */
greeting_flag = false;
putchar('\n');
RAISE(Exit_Interp);
} else if (script_flag)
/* on script-mode only retrun c */
return (c);
}
if (c == EOL) {
line++;
column = 0;
} else
column++;
return (c);
}
void unreadc(char c)
{
if (c == EOL)
line--;
else
column--;
if (input_stream == standard_input && repl_flag)
(void) read_line(-1);
else if (GET_OPT(input_stream) != EISL_INSTR)
ungetc(c, GET_PORT(input_stream));
else if (c != EOF)
SET_CDR(input_stream, GET_CDR(input_stream) - 1);
}
void init_stok()
{
stok.ch = '\0';
stok.flag = GO;
stok.type = OTHER;
stok.bufsize = BUFSIZE;
stok.buf = ALLOC(BUFSIZE * sizeof(char));
stok.buf[0] = '\0';
}
void set_stok_buf(int index, char c)
{
if (index >= stok.bufsize) {
int new_bufsize = index + 1;
RESIZE(stok.buf, sizeof(char) * new_bufsize);
stok.bufsize = new_bufsize;
}
stok.buf[index] = c;
return;
}
void replace_stok_buf(char *str)
{
if (strlen(str) > (size_t) stok.bufsize) {
int new_bufsize = strlen(str);
RESIZE(stok.buf, sizeof(char) * new_bufsize);
stok.bufsize = new_bufsize;
}
strcpy(stok.buf, str);
}
void get_token(void)
{
int c;
int pos;
int res;
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 = readc();
skip:
while (c == SPACE || c == EOL || c == TAB || c == RET)
c = readc();
/* skip comment line
* if find EOF at end of line, return FILEEND.
*/
if (c == ';') {
while (!(c == EOL)) {
c = readc();
if (c == EOF) {
stok.type = FILEEND;
return;
}
}
goto skip;
}
/* if end of file,return FILEEND. */
if (c == EOF) {
stok.type = FILEEND;
return;
}
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;
case '"':
{
c = readc();
pos = 0;
while (c != '"') {
set_stok_buf(pos++, c);
if (c == '\\') {
c = readc();
set_stok_buf(pos++, c);
}
if (c == EOF) {
error(SYSTEM_ERR, "not exist right hand double quote",
NIL, 0);
}
c = readc();
}
set_stok_buf(pos, NUL);
stok.type = STRING;
break;
}
case '#':
{
c = readc();
if (c == '\'') {
stok.type = FUNCTION;
break;
} else if (c == '(') {
stok.type = VECTOR;
break;
} else if (c == '\\') {
c = readc();
pos = 0;
set_stok_buf(pos++, c);
if (c == ' ')
goto chskip;
while (((c = readc()) != EOL) && (c != EOF)
&& (pos < stok.bufsize - 1) && (c != SPACE)
&& (c != '(')
&& (c != ')')) {
set_stok_buf(pos++, c);
}
chskip:
set_stok_buf(pos, NUL);
stok.type = CHARACTER;
if (c == EOF)
stok.ch = ' ';
else
stok.ch = c;
break;
} else if (isdigit(c)) {
pos = 0;
while (isdigit(c)) {
set_stok_buf(pos, c);
pos++;
c = readc();
}
stok.buf[pos] = NUL;
set_stok_buf(pos, c);
if (c == 'a' || c == 'A') {
stok.type = ARRAY;
break;
} else {
stok.type = OTHER;
return;
}
}
if (c == '|') {
reskip:
c = readc();
while (c != '|') {
if (c == EOF)
error(SYSTEM_ERR,
"not exist right hand #| comment |#", NIL,
0);
c = readc();
}
c = readc();
if (c == '#') {
c = readc();
goto skip;
} else
goto reskip;
} else
unreadc(c);
c = '#';
}
/*FALLTHROUGH*/ default:
{
pos = 0;
set_stok_buf(pos++, c);
while (((c = readc()) != EOL) && (c != EOF)
&& (pos < stok.bufsize - 1) && (c != SPACE)
&& (c != '(')
&& (c != ')') && (c != '`') && (c != ',') && (c != '@'))
set_stok_buf(pos++, c);
set_stok_buf(pos, NUL);
stok.ch = c;
if (flt_token(stok.buf)) {
stok.type = FLOAT_N;
break;
}
/* first step,check bignum_token() and second int_token() ignores number of digits. */
if (bignum_token(stok.buf)) {
stok.type = BIGNUM;
break;
}
if (int_token(stok.buf)) {
stok.type = INTEGER;
break;
}
if (int_token_nsgn(stok.buf)) {
stok.type = INTEGER;
break;
}
if (bin_token(stok.buf)) {
stok.type = BINARY;
break;
}
if (oct_token(stok.buf)) {
stok.type = OCTAL;
break;
}
if (dec_token(stok.buf)) {
stok.type = DECNUM;
break;
}
if (hex_token(stok.buf)) {
stok.type = HEXNUM;
break;
}
if ((res = expt_token(stok.buf))) {
if (res == 2)
stok.type = EXPTOVERF;
else if (res == 3)
stok.type = EXPTUNDERF;
else
stok.type = EXPTNUM;
break;
}
if (symbol_token(stok.buf)) {
stok.type = SYMBOL;
break;
}
stok.type = OTHER;
}
}
}
septoken separater(char buf[], char sep)
{
int i, j;
char c;
septoken res;
res.sepch = NUL;
res.after[0] = NUL;
res.before[0] = buf[0];
i = 1;
j = 1;
while ((c = buf[i]) != NUL)
if (c == sep) {
res.before[j] = NUL;
res.sepch = sep;
i++;
j = 0;
while ((c = buf[i]) != NUL) {
res.after[j] = c;
i++;
j++;
}
res.after[j] = NUL;
} else {
res.before[j] = c;
i++;
j++;
}
return (res);
}
void insert_str(char ch, char buf[])
{
int i;
i = last_str(buf) + 1;
while (i >= 0) {
buf[i + 1] = buf[i];
i--;
}
buf[0] = ch;
}
int last_str(char buf[])
{
int i;
i = 0;
while (buf[i] != NUL)
i++;
return (i - 1);
}
/* remove #\ from char, for example #\a -> a */
void drop_char(char buf[])
{
int i, j;
j = last_str(buf);
for (i = 2; i <= j; i++)
buf[i - 2] = buf[i];
buf[i - 2] = NUL;
}
/* integer of sign. ignore number of digits. */
int int_token(char buf[])
{
int i;
char c;
if (buf[0] == NUL) /* null string */
return (0);
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