-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouselisp.c
More file actions
108 lines (90 loc) · 2.36 KB
/
mouselisp.c
File metadata and controls
108 lines (90 loc) · 2.36 KB
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
#include <setjmp.h>
#include <stdio.h>
#include <string.h>
#include "mouselisp.h"
char *ml_read(void) {
ml_string ret = ml_string_new_str("");
int c;
for (;;) {
c = fgetc(stdin);
if (feof(stdin))
return NULL;
if (c == '\n')
return ret.str;
ml_string_concat_char(&ret, c);
}
}
void ml_repl(void) {
ml_machine machine = ml_machine_new();
for (;;) {
printf("ml> ");
char *str = ml_read();
/* EOF */
if (str == NULL)
return;
ml_parser parser = ml_parser_new_str(str);
ml_object *root;
/* top level error handling */
ml_object *prev_named_objs = machine.named_objs;
jmp_buf curr_jmpbuf;
machine.last_exc_handler = &curr_jmpbuf;
machine.exc = the_nil;
if (setjmp(curr_jmpbuf) == 0) {
root = ml_parser_parse(&parser, &machine);
} else {
machine.named_objs = prev_named_objs;
logmsg("error: %s", machine.exc->cons.cdr->cons.car->str.str);
continue;
}
for (ml_object *list = root; list != the_nil; list = list->cons.cdr) {
/* top level error handling */
ml_object *prev_named_objs = machine.named_objs;
jmp_buf curr_jmpbuf;
machine.last_exc_handler = &curr_jmpbuf;
machine.exc = the_nil;
if (setjmp(curr_jmpbuf) == 0) {
ml_object_debug_dump(ml_machine_eval(&machine, list->cons.car));
} else {
machine.named_objs = prev_named_objs;
logmsg("error: %s", machine.exc->cons.cdr->cons.car->str.str);
continue;
}
}
}
}
void ml_execute_file(const char *filename) {
FILE *f = fopen(filename, "rb");
if (f == NULL)
fatal("fopen");
ml_machine machine = ml_machine_new();
ml_parser parser = ml_parser_new_file(f);
ml_object *root = ml_parser_xparse(&parser, &machine);
(void)ml_machine_xeval_top(&machine, root);
if (fclose(f) != 0)
fatal("fclose");
}
int main(int argc, char **argv) {
mouselisp_init();
if (argc == 1) {
ml_repl();
return 0;
}
if (argc > 1 && strcmp(argv[1], "--test") == 0) {
test_main();
return 0;
}
if (argc > 2 && strcmp(argv[1], "--test-object-dump") == 0) {
test_object_dump_main(argv[2]);
return 0;
}
if (argc > 1 && strcmp(argv[1], "--test-unique") == 0) {
test_unique();
return 0;
}
if (argc == 2) {
ml_execute_file(argv[1]);
return 0;
}
fprintf(stderr, "unknown options.");
return 1;
}