-
Notifications
You must be signed in to change notification settings - Fork 0
/
idelisp.c
113 lines (99 loc) · 3.18 KB
/
idelisp.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
#include "core.c"
#include <editline/readline.h>
enum { RUNMODE_REPL, RUNMODE_FILE };
int main(int argc, char** argv) {
int run_mode = RUNMODE_REPL;
char* source_file = NULL;
for (int i=0; i<argc; i++) {
if (strcmp(argv[i], "-f") == 0 && i<argc-1) {
source_file = argv[i+1];
run_mode = RUNMODE_FILE;
i++;
}
}
Decimal = mpc_new("decimal");
Number = mpc_new("number");
String = mpc_new("string");
Symbol = mpc_new("symbol");
Keyword = mpc_new("keyword");
Comment = mpc_new("comment");
HashMap = mpc_new("hashmap");
Sexpr = mpc_new("sexpr");
Qexpr = mpc_new("qexpr");
Expr = mpc_new("expr");
IdeLISP = mpc_new("idelisp");
mpca_lang(MPCA_LANG_DEFAULT,
" \
decimal : /-?[0-9]+\\.[0-9]+/ ; \
number : /-?[0-9]+/ ; \
string : /\"(\\\\.|[^\"])*\"/ ; \
keyword : /:[a-zA-Z0-9_+^\\-*\\/\\\\=<>!&%\\?]+/ ; \
symbol : /[a-zA-Z0-9_+^\\-*\\/\\\\=<>!&%\\?]+/ ; \
comment : /;[^\\r\\n]*/ ; \
hashmap : '{' <expr>* '}' ; \
sexpr : '(' <expr>* ')' ; \
qexpr : \"'(\" <expr>* ')' ; \
expr : <decimal> | <number> | <string> | <keyword> | <symbol> \
| <sexpr> | <qexpr> | <comment> | <hashmap> ; \
idelisp : /^/ <expr>* /$/ ; \
",
Decimal,
Number,
String,
Keyword,
Symbol,
Comment,
HashMap,
Sexpr,
Qexpr,
Expr,
IdeLISP);
ideenv* env = ideenv_new();
env->depth = 0;
ideenv_add_builtins(env);
if (run_mode == RUNMODE_FILE) {
ideobj* args = ideobj_list_add(ideobj_sexpr(), ideobj_str(source_file));
ideobj* expression = builtin_load(env, args);
if (expression->type == IDEOBJ_ERR) {
ideobj_println(expression);
return 1;
}
ideobj_del(expression);
return 0;
}
puts("IdeLISP (type exit() to quit)");
while(1) {
char* source = readline(">> ");
add_history(source);
mpc_result_t result;
if (mpc_parse("<stdin>", source, IdeLISP, &result)) {
mpc_ast_t* root_node = result.output;
ideobj* v = ideobj_eval(
env,
ideobj_read(root_node)
);
ideobj_println(v);
ideobj_del(v);
} else {
mpc_err_print(result.error);
}
mpc_ast_delete(result.output);
free(source);
}
ideenv_del(env);
mpc_cleanup(
11,
Decimal,
Number,
String,
Keyword,
Symbol,
Comment,
HashMap,
Sexpr,
Qexpr,
Expr,
IdeLISP
);
return 0;
}