-
Notifications
You must be signed in to change notification settings - Fork 23
/
arcadia.c
98 lines (89 loc) · 1.78 KB
/
arcadia.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
#include "arc.h"
#define VERSION "0.38"
void print_logo() {
printf("Arcadia %s\n", VERSION);
}
void repl() {
struct string input;
while ((input.str = readline("> ")) != NULL) {
input.cap = (input.len = strlen(input.str)) + 1;
read_start:;
const char *p = input.str;
error err;
atom expr;
err = read_expr(p, &p, &expr);
if (err == ERROR_FILE) { /* read more lines */
char *line = readline(" ");
if (!line) break;
string_cat(&input, "\n");
string_cat(&input, line);
free(line);
goto read_start;
}
#ifdef READLINE
add_history(input.str);
#endif
if (!err) {
while (1) {
atom result;
error err = macex_eval(expr, &result);
if (err) {
print_error(err);
printf("error in expression:\n");
print_expr(expr);
putchar('\n');
break;
}
else {
print_expr(result);
puts("");
}
err = read_expr(p, &p, &expr);
if (err != ERROR_OK) {
break;
}
}
} else {
print_error(err);
}
free(input.str);
}
}
int main(int argc, char **argv)
{
if (argc == 1) { /* REPL */
print_logo();
arc_init(argv[0]);
repl();
puts("");
return 0;
}
else if (argc == 2) {
char *opt = argv[1];
if (strcmp(opt, "-h") == 0) {
puts("Usage: arcadia [OPTIONS...] [FILES...]");
puts("");
puts("OPTIONS:");
puts(" -h print this screen.");
puts(" -v print version.");
return 0;
}
else if (strcmp(opt, "-v") == 0) {
puts(VERSION);
return 0;
}
}
/* execute files */
arc_init(argv[0]);
int i;
error err;
for (i = 1; i < argc; i++) {
err = arc_load_file(argv[i]);
if (err) {
fprintf(stderr, "In file %s:\n", argv[i]);
print_error(err);
break;
}
}
return 0;
}