Repository navigation
Expand file tree
/
Copy pathparsing.c
More file actions
106 lines (91 loc) · 2.8 KB
/
Copy pathparsing.c
File metadata and controls
106 lines (91 loc) · 2.8 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
#include "lib/mpc.h"
#include "include/lval.h"
#include "include/evaluation.h"
#ifdef _WIN32
static char buffer[2048];
char *readline(char *prompt)
{
fputs(prompt, stdout);
fgets(buffer, 2048, stdin);
char *cpy = malloc(strlen(buffer) + 1);
strcpy(cpy, buffer);
cpy[strlen(cpy) - 1] = '\0';
return cpy;
}
void add_history(char *unused) {}
#else
#include <editline/readline.h>
#include <editline/history.h>
#endif
mpc_parser_t *Number;
mpc_parser_t *Symbol;
mpc_parser_t *String;
mpc_parser_t *Comment;
mpc_parser_t *Sexpr;
mpc_parser_t *Qexpr;
mpc_parser_t *Expr;
mpc_parser_t *LispB;
int main(int argc, char *argv[])
{
Number = mpc_new("number");
Symbol = mpc_new("symbol");
String = mpc_new("string");
Comment = mpc_new("comment");
Sexpr = mpc_new("sexpr");
Qexpr = mpc_new("qexpr");
Expr = mpc_new("expr");
LispB = mpc_new("lispb");
// defining the parsers
mpca_lang(MPCA_LANG_DEFAULT,
" \
number : /-?([0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+|[0-9]+)/ ; \
symbol : /[a-zA-Z0-9_+\\-*\\/\\\\^=<>!&]+/ ; \
string : /\"(\\\\.|[^\"])*\"/ ; \
comment : /;[^\\r\\n]*/ ; \
sexpr : '(' <expr>* ')' ; \
qexpr : '{' <expr>* '}' ; \
expr : <number> | <symbol> | <string> \
| <comment> | <sexpr> | <qexpr> ; \
lispb : /^/ <expr>* /$/ ; \
",
Number, Symbol, String, Comment, Sexpr, Qexpr, Expr, LispB);
lenv *e = lenv_new();
lenv_add_builtins(e);
if (argc >= 2)
{
for (int i = 1; i < argc; i++)
{
lval *args = lval_add(lval_sexpr(), lval_str(argv[i]));
lval *x = builtin_load(e, args);
if (x->type == LVAL_ERR)
lval_println(x);
lval_del(x);
}
}
printf("LispB Interpreter\n");
printf("Press Ctrl+c to Exit\n\n");
while (1)
{
char *input = readline("lispb> ");
add_history(input);
mpc_result_t r;
if (mpc_parse("<stdin>", input, LispB, &r))
{
lval *x = lval_eval(e, lval_read(r.output));
if (x->count > 0)
lval_println(x);
lval_del(x);
// mpc_ast_print(r.output);
mpc_ast_delete(r.output);
}
else
{
mpc_err_print(r.error);
mpc_err_delete(r.error);
}
free(input);
}
lenv_del(e);
mpc_cleanup(8, Number, Symbol, String, Comment, Sexpr, Qexpr, Expr, LispB);
return 0;
}