forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.c
376 lines (345 loc) · 10.2 KB
/
repl.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
/*
repl.c
system startup, main(), and console interaction
*/
#include "repl.h"
char *jl_answer_color = "\033[1m\033[34m";
char *prompt_string;
static char jl_prompt_plain[] = "julia> ";
static char jl_color_normal[] = "\033[0m";
static int lisp_prompt = 0;
int jl_have_event_loop = 0;
static char *program = NULL;
char *image_file = "sys.ji";
jl_value_t *rl_ast = NULL;
int tab_width = 2;
int prompt_length = 0;
int have_color = 1;
static const char *usage = "julia [options] [program] [args...]\n";
static const char *opts =
" -q --quiet Quiet startup without banner\n"
" -H --home=<dir> Load files relative to <dir>\n"
" -T --tab=<size> Set REPL tab width to <size>\n\n"
" -e --eval=<expr> Evaluate <expr> and don't print\n"
" -E --print=<expr> Evaluate and print <expr>\n"
" -P --post-boot=<expr> Evaluate <expr> right after boot\n"
" -L --load=file Load <file> right after boot\n"
" -b --bare Bare: don't load default startup files\n"
" -J --sysimage=file Start up with the given system image file\n"
" --lisp Start with Lisp prompt not Julia\n\n"
" -h --help Print this message\n";
void parse_opts(int *argcp, char ***argvp) {
static char* shortopts = "+H:T:bhJ:";
static struct option longopts[] = {
{ "home", required_argument, 0, 'H' },
{ "tab", required_argument, 0, 'T' },
{ "bare", no_argument, 0, 'b' },
{ "lisp", no_argument, &lisp_prompt, 1 },
{ "help", no_argument, 0, 'h' },
{ "sysimage", required_argument, 0, 'J' },
{ 0, 0, 0, 0 }
};
int c;
opterr = 0;
while ((c = getopt_long(*argcp,*argvp,shortopts,longopts,0)) != -1) {
if (c == '?') {
optind--;
break;
}
switch (c) {
case 0:
break;
case 'H':
julia_home = strdup(optarg);
break;
case 'T':
// TODO: more robust error checking.
tab_width = atoi(optarg);
break;
case 'b':
image_file = NULL;
break;
case 'J':
image_file = optarg;
break;
case 'h':
printf("%s%s", usage, opts);
exit(0);
default:
ios_printf(ios_stderr, "julia: unhandled option -- %c\n", c);
ios_printf(ios_stderr, "This is a bug, please report it.\n");
exit(1);
}
}
if (!julia_home) {
julia_home = getenv("JULIA_HOME");
if (julia_home) {
julia_home = strdup(julia_home);
} else {
char *julia_path = (char*)malloc(PATH_MAX);
get_exename(julia_path, PATH_MAX);
julia_home = strdup(dirname(julia_path));
free(julia_path);
}
}
char *answer_color = getenv("JL_ANSWER_COLOR");
if (answer_color) {
if (!strcmp(answer_color,"black"))
jl_answer_color = "\033[1m\033[30m";
else if (!strcmp(answer_color,"red"))
jl_answer_color = "\033[1m\033[31m";
else if (!strcmp(answer_color,"green"))
jl_answer_color = "\033[1m\033[32m";
else if (!strcmp(answer_color,"yellow"))
jl_answer_color = "\033[1m\033[33m";
else if (!strcmp(answer_color,"blue"))
jl_answer_color = "\033[1m\033[34m";
else if (!strcmp(answer_color,"magenta"))
jl_answer_color = "\033[1m\033[35m";
else if (!strcmp(answer_color,"cyan"))
jl_answer_color = "\033[1m\033[36m";
else if (!strcmp(answer_color,"white"))
jl_answer_color = "\033[1m\033[37m";
}
*argvp += optind;
*argcp -= optind;
if (image_file==NULL && *argcp > 0) {
if (strcmp((*argvp)[0], "-")) {
program = (*argvp)[0];
}
++*argvp; --*argcp;
}
}
int ends_with_semicolon(const char *input)
{
char *p = strrchr(input, ';');
if (p++) {
while (isspace(*p)) p++;
if (*p == '\0' || *p == '#')
return 1;
}
return 0;
}
static int detect_color()
{
#ifdef WIN32
return 0;
#else
int tput = system("tput setaf 0 >/dev/null");
if (tput == 0) return 1;
if (tput == 1) return 0;
char *term = getenv("TERM");
if (term == NULL) return 0;
return (!strcmp(term,"xterm") || !strcmp(term,"xterm-color"));
#endif
}
// called when we detect an event on stdin
DLLEXPORT void jl_stdin_callback()
{
repl_stdin_callback();
}
static int exec_program()
{
int err = 0;
again: ;
JL_TRY {
if (err) {
jl_show(jl_exception_in_transit);
ios_printf(ios_stdout, "\n");
JL_EH_POP();
return 1;
}
jl_load(program);
}
JL_CATCH {
err = 1;
goto again;
}
return 0;
}
static void exit_repl(int code)
{
exit_repl_environment();
if (have_color) {
ios_printf(ios_stdout, jl_color_normal);
ios_flush(ios_stdout);
}
#ifdef JL_GF_PROFILE
print_profile();
#endif
exit(code);
}
void jl_show_full_function(jl_value_t *v);
static void repl_show_value(jl_value_t *v)
{
if (jl_is_function(v) && !jl_is_struct_type(v)) {
// show method table when a function is shown at the top level.
jl_show_full_function(v);
return;
}
jl_show(v);
if (jl_is_struct_type(v)) {
ios_t *s = jl_current_output_stream();
// for convenience, show constructor methods when
// a type is shown at the top level.
if (jl_is_gf(v)) {
ios_putc('\n', s);
jl_show_full_function(v);
}
}
}
DLLEXPORT void jl_eval_user_input(jl_value_t *ast, int show_value)
{
if (jl_have_event_loop) {
// with multi.j loaded the command line input callback can return
// before the command finishes running, so we have to
// disable rl to prevent the prompt from reappearing too soon.
repl_callback_disable();
}
JL_GC_PUSH(&ast);
assert(ast != NULL);
int iserr = 0;
again:
;
JL_TRY {
jl_register_toplevel_eh();
if (have_color) {
ios_printf(ios_stdout, jl_color_normal);
ios_flush(ios_stdout);
}
if (iserr) {
jl_show(jl_exception_in_transit);
ios_printf(ios_stdout, "\n");
JL_EH_POP();
break; // leave JL_TRY
}
jl_value_t *value = jl_toplevel_eval(ast);
jl_set_global(jl_system_module, jl_symbol("ans"), value);
if (value != (jl_value_t*)jl_nothing && show_value) {
if (have_color) {
ios_printf(ios_stdout, jl_answer_color);
ios_flush(ios_stdout);
}
repl_show_value(value);
ios_printf(ios_stdout, "\n");
}
}
JL_CATCH {
iserr = 1;
goto again;
}
ios_printf(ios_stdout, "\n");
ios_flush(ios_stdout);
JL_GC_POP();
repl_callback_enable();
}
// handle a command line input event
void handle_input(jl_value_t *ast, int end, int show_value)
{
if (end) {
ios_printf(ios_stdout, "\n");
exit_repl(0);
}
if (ast == NULL) {
ios_printf(ios_stdout, "\n");
repl_print_prompt();
ios_flush(ios_stdout);
return;
}
if (!jl_have_event_loop) {
jl_eval_user_input(ast, show_value);
}
else {
jl_value_t *f =
jl_get_global(jl_system_module,jl_symbol("repl_callback"));
assert(f != NULL);
jl_value_t *fargs[] = { ast, jl_box_int32(show_value) };
jl_apply((jl_function_t*)f, fargs, 2);
}
}
void jl_lisp_prompt();
#ifdef JL_GF_PROFILE
static void print_profile()
{
size_t i;
void **table = jl_system_module->bindings.table;
for(i=1; i < jl_system_module->bindings.size; i+=2) {
if (table[i] != HT_NOTFOUND) {
jl_binding_t *b = (jl_binding_t*)table[i];
if (b->value != NULL && jl_is_function(b->value) &&
jl_is_gf(b->value)) {
ios_printf(ios_stdout, "%d\t%s\n",
jl_gf_mtable(b->value)->ncalls,
jl_gf_name(b->value)->name);
}
}
}
}
#endif
int true_main(int argc, char *argv[])
{
if (lisp_prompt) {
jl_lisp_prompt();
return 0;
}
jl_array_t *args = jl_alloc_cell_1d(argc);
jl_set_global(jl_system_module, jl_symbol("ARGS"), (jl_value_t*)args);
int i;
for (i=0; i < argc; i++) {
jl_arrayset(args, i, (jl_value_t*)jl_cstr_to_string(argv[i]));
}
jl_set_global(jl_system_module, jl_symbol("JULIA_HOME"),
jl_cstr_to_string(julia_home));
// run program if specified, otherwise enter REPL
if (program) {
return exec_program();
}
init_repl_environment();
have_color = detect_color();
char *prompt = have_color ? jl_prompt_color : jl_prompt_plain;
prompt_length = strlen(jl_prompt_plain);
prompt_string = prompt;
jl_function_t *start_client =
(jl_function_t*)
jl_get_global(jl_system_module, jl_symbol("_start"));
if (start_client == NULL) {
repl_print_prompt();
ios_flush(ios_stdout);
// client event loop not available; use fallback blocking version
int iserr = 0;
again:
;
JL_TRY {
if (iserr) {
if (have_color) {
ios_printf(ios_stdout, jl_color_normal);
ios_flush(ios_stdout);
}
jl_show(jl_exception_in_transit);
ios_printf(ios_stdout, "\n\n");
ios_flush(ios_stdout);
iserr = 0;
}
while (1) {
read_expr(prompt);
}
}
JL_CATCH {
iserr = 1;
goto again;
}
}
else {
jl_have_event_loop = 1;
jl_apply(start_client, NULL, 0);
}
exit_repl(0);
return 0;
}
int main(int argc, char *argv[])
{
llt_init();
parse_opts(&argc, &argv);
julia_init(lisp_prompt ? NULL : image_file);
return julia_trampoline(argc, argv, true_main);
}