forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.c
230 lines (209 loc) · 6.13 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
/*
repl.c
system startup, main(), and console interaction
*/
#include "repl.h"
static int lisp_prompt = 0;
static char *program = NULL;
char *image_file = "sys.ji";
int tab_width = 2;
static const char *usage = "julia [options] [program] [args...]\n";
static const char *opts =
" -v --version Display version information\n"
" -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>\n"
" -E --print=<expr> Evaluate and show <expr>\n"
" -P --post-boot=<expr> Evaluate <expr> right after boot\n"
" -L --load=file Load <file> right after boot\n"
" -J --sysimage=file Start up with the given system image file\n\n"
" -p n Run n local processes\n"
" --machinefile file Run processes on hosts listed in file\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;
int ind = 1;
while ((c = getopt_long(*argcp,*argvp,shortopts,longopts,0)) != -1) {
switch (c) {
case 0:
break;
case '?':
break;
case 'H':
julia_home = strdup(optarg);
ind+=2;
break;
case 'T':
// TODO: more robust error checking.
tab_width = atoi(optarg);
ind+=2;
break;
case 'b':
image_file = NULL;
ind+=1;
break;
case 'J':
image_file = optarg;
ind+=2;
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);
}
}
*argvp += ind;
*argcp -= ind;
if (image_file==NULL && *argcp > 0) {
if (strcmp((*argvp)[0], "-")) {
program = (*argvp)[0];
}
}
}
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 exec_program(void)
{
int err = 0;
again: ;
JL_TRY {
jl_register_toplevel_eh();
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;
}
// handle a command line input event
void handle_input(jl_value_t *ast, int end, int show_value)
{
if (end) {
show_value = -1;
ast = jl_nothing;
}
jl_value_t *f = jl_get_global(jl_base_module,jl_symbol("repl_callback"));
assert(f);
jl_value_t *fargs[] = { ast, jl_box_long(show_value) };
jl_apply((jl_function_t*)f, fargs, 2);
}
void jl_lisp_prompt();
#ifdef JL_GF_PROFILE
static void print_profile(void)
{
size_t i;
void **table = jl_base_module->bindings.table;
for(i=1; i < jl_base_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_current_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_const(jl_current_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();
jl_function_t *start_client =
(jl_function_t*)jl_get_global(jl_base_module, jl_symbol("_start"));
if (start_client) {
jl_apply(start_client, NULL, 0);
return 0;
}
// client event loop not available; use fallback blocking version
int iserr = 0;
again:
;
JL_TRY {
if (iserr) {
jl_show(jl_exception_in_transit);
ios_printf(ios_stdout, "\n\n");
iserr = 0;
}
while (1) {
char *input = read_expr("julia> ");
if (!input || ios_eof(ios_stdin)) {
ios_printf(ios_stdout, "\n");
break;
}
jl_value_t *ast = jl_parse_input_line(input);
jl_value_t *value = jl_toplevel_eval(ast);
jl_show(value);
ios_printf(ios_stdout, "\n\n");
}
}
JL_CATCH {
iserr = 1;
goto again;
}
return 0;
}
int main(int argc, char *argv[])
{
libsupport_init();
parse_opts(&argc, &argv);
julia_init(lisp_prompt ? NULL : image_file);
return julia_trampoline(argc, argv, true_main);
}