-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.c
103 lines (83 loc) · 2.62 KB
/
parsing.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
#include <stdio.h>
#include <stdlib.h>
#include <editline/readline.h>
#include <histedit.h>
#include "mpc.h"
long eval_op(long x, char *op, long y) {
switch (op[0]) {
case '+':
return x + y;
case '-':
return x - y;
case '*':
return x * y;
case '/':
return x / y;
case '%':
return x % y;
}
printf("Unknown operator: %s", op);
return 0;
}
long eval(mpc_ast_t *t) {
/* Number: arrived at leaf */
if (strstr(t->tag, "number")) {
return atoi(t->contents);
}
/* The operator always comes after '(' which is the 0'th child */
char *op = t->children[1]->contents;
/* We store the third child in 'x' */
long x = eval(t->children[2]);
/* Iterate over remaining children */
int i = 3;
while (strstr(t->children[i]->tag, "expr")) {
x = eval_op(x, op, eval(t->children[i]));
i++;
}
return x;
}
int main(int argc, char **argv) {
// printf("4 * 2: %li\n", eval_op(4, "*", 2));
/* Create some parsers */
mpc_parser_t *Number = mpc_new("number");
mpc_parser_t *Operator = mpc_new("operator");
mpc_parser_t *Expr = mpc_new("expr");
mpc_parser_t *Lispy = mpc_new("lispy");
/* Define the parsers with the following language */
mpca_lang(MPCA_LANG_DEFAULT,
" \
number : /(-?[0-9]+)/ ; \
operator : '+' | '-' | '*' | '/' | '%' ; \
expr : <number> | '(' <operator> <expr>+ ')' ; \
lispy : /^/ <operator> <expr>+ /$/ ; \
",
Number, Operator, Expr, Lispy);
/* Version and exit information */
puts("Lispy version 0.0.0.0.1\n");
puts("Press CTRL-C to exit\n");
puts("Example expression: * 2 2 or * (+ 1 5) (* 1 3 7)");
while (1) {
char *input = readline("cispy >> ");
add_history(input);
printf("%s\n", input);
/* Attempt to parse the input */
mpc_result_t r;
if (mpc_parse("<stdin>", input, Lispy, &r)) {
/* Evaluate */
long result = eval(r.output);
printf("%li\n", result);
/* Success - print the AST */
mpc_ast_print(r.output);
mpc_ast_delete(r.output);
} else {
/* Not parsed. Print error */
mpc_err_print(r.error);
mpc_err_delete(r.error);
}
/* readline does malloc */
free(input);
}
/* Undefine and delete the parsers */
mpc_cleanup(4, Number, Operator, Expr, Lispy);
return 0;
}