-
Notifications
You must be signed in to change notification settings - Fork 4
/
toysh.c
147 lines (125 loc) · 3.3 KB
/
toysh.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
/* $Id: toysh.c,v 1.14 2012/03/01 12:33:31 mit-sato Exp $ */
#include <stdio.h>
#include <string.h>
#include "toy.h"
#define BUFFSIZE (4096)
#define COMMAND "toysh"
int main(int argc, char **argv, char **envp) {
char buff[BUFFSIZE];
Toy_Type *any, *result;
Toy_Type *script;
Toy_Type *err;
Bulk *b;
Cell *c;
Toy_Interp *interp;
jmp_buf jmp_env;
error_reset:
interp = new_interp("main", STACKSIZE, NULL, argc, argv, envp);
b = new_bulk();
if (0 == setjmp(jmp_env)) {
cstack_set_jmpbuff(0, &jmp_env);
} else {
fprintf(stderr, "Catch the main co-routine stack overflow, to be restarted.\n");
goto error_reset;
}
if (argv[1] && (strcmp(argv[1], "-") != 0)) {
if (0 == bulk_load_file(b, argv[1])) {
fprintf(stderr, "file not open: %s\n", argv[1]);
exit(1);
}
any = toy_parse_start(b);
if (NULL == any) {
fprintf(stderr, "no memory\n");
exit(1);
}
switch (GET_TAG(any)) {
case EXCEPTION:
err = any;
fprintf(stderr, "parse error: %s\n",
to_string(list_get_item(err->u.exception.msg_list)));
exit(0);
case SCRIPT:
script = any;
result = toy_eval_script(interp, script);
if (GET_TAG(result) != EXCEPTION) {
fprintf(stdout, "result[%s]=> ", toy_get_type_str(result));
fprintf(stdout, "%s\n", to_print(result));
} else {
fprintf(stdout, "EXCEPTION: %s\n", to_string(result));
}
exit(0);
default:
fprintf(stderr, "parse error: type=%s\n", toy_get_type_str(any));
exit(1);
}
}
while (! feof(stdin)) {
fputs("> ", stderr);
if (NULL == fgets(buff, BUFFSIZE, stdin)) break;
buff[BUFFSIZE-1] = 0;
if (buff[0] == '!') {
buff[strlen(buff)-1] = 0;
if (0 == bulk_load_file(b, &buff[1])) {
fprintf(stderr, "file not open: %s\n", &buff[1]);
continue;
}
} else {
bulk_set_string(b, buff);
}
any = toy_parse_start(b);
if (NULL == any) {
fprintf(stderr, "no memory\n");
} else {
switch (GET_TAG(any)) {
case EXCEPTION:
err = any;
fprintf(stderr, "parse error: %s\n",
to_string(list_get_item(err->u.exception.msg_list)));
if (cell_eq_str(err->u.exception.code, TE_PARSEBADCHAR) == 0) break;
if (buff[0] == '!') break;
c = new_cell(buff);
while (1) {
fputs("=> ", stderr);
if (NULL == fgets(buff, BUFFSIZE, stdin)) goto exit_loop;
buff[BUFFSIZE-1] = 0;
cell_add_str(c, buff);
bulk_set_string(b, cell_get_addr(c));
any = toy_parse_start(b);
if (GET_TAG(any) == EXCEPTION) {
err = any;
if (cell_eq_str(err->u.exception.code, TE_PARSEBADCHAR) == 0) {
fprintf(stderr, "parse error: %s\n",
to_string(list_get_item(err->u.exception.msg_list)));
goto next_loop;
}
} else {
break;
}
}
case SCRIPT:
script = any;
result = toy_eval_script(interp, script);
if (GET_TAG(result) != EXCEPTION) {
char *p;
fprintf(stdout, "result[%s]=> ", toy_get_type_str(result));
p = to_print(result);
if (strlen(p) > 512) {
fprintf(stdout, "%-.512s ...\n", p);
} else {
fprintf(stdout, "%s\n", p);
}
} else {
fprintf(stdout, "EXCEPTION: %s\n", to_string(result));
}
break;
default:
fprintf(stderr, "parse error: type=%s\n", toy_get_type_str(any));
}
}
next_loop:
/* dummy */
0;
}
exit_loop:
return 0;
}