-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.c
178 lines (143 loc) · 4.3 KB
/
prompt.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <stdio.h>
#include <stdlib.h>
#include "mpc.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.h>
#endif
// Enumeration of possible error types
enum { LERR_DIV_ZERO, LERR_BAD_OP, LERR_BAD_NUM };
// Enumeration of possible lval types
enum { LVAL_NUM, LVAL_ERR };
typedef struct {
int type;
long num;
int err;
} lval;
int number_of_nodes(mpc_ast_t* t);
lval eval(mpc_ast_t* t);
lval eval_op(lval x, char* op, lval y);
lval lval_num(long x);
lval lval_err(int x);
void lval_print(lval v);
void lval_println(lval v);
int main(int argc, char** argv) {
// Parser init
mpc_parser_t* Number = mpc_new("number");
mpc_parser_t* Operator = mpc_new("operator");
mpc_parser_t* Expr = mpc_new("expr");
mpc_parser_t* Lizp = mpc_new("lizp");
// Definitions
mpca_lang(MPCA_LANG_DEFAULT,
" \
number : /-?[0-9]+/ ; \
operator : '+' | '-' | '*' | '/' ; \
expr : <number> | '(' <operator> <expr>+ ')' ; \
lizp : /^/ <operator> <expr>+ /$/ ; \
",
Number, Operator, Expr, Lizp);
printf("Lizp Version -2\nPress Ctrl+c to exit\n");
// C doesn't support bools in stdio.h so 1 suffices for true
while(1) {
char* input = readline("lizp> ");
add_history(input);
mpc_result_t r;
if(mpc_parse("<stdin>", input, Lizp, &r)) {
lval result = eval(r.output);
lval_println(result);
mpc_ast_delete(r.output);
} else {
mpc_err_print(r.error);
mpc_err_delete(r.error);
}
free(input);
}
mpc_cleanup(4, Number, Operator, Expr, Lizp);
return 0;
}
int number_of_nodes(mpc_ast_t* t) {
if (t->children_num == 0) { return 1; }
if (t->children_num >= 1) {
int total = 1;
for (int i = 0; i < t->children_num; i++) {
total = total + number_of_nodes(t->children[i]);
}
return total;
}
return 0;
}
lval eval(mpc_ast_t* t) {
//returns number if it's tagged as such
if (strstr(t->tag, "number")) {
errno = 0;
long x = strtol(t->contents, NULL, 10);
return errno != ERANGE ? lval_num(x) : lval_err(LERR_BAD_NUM);
}
// Operator is always second child
char* op = t->children[1]->contents;
// Store third value
lval x = eval(t->children[2]);
// Iterate remaining values and combine
int i = 3;
while (strstr(t->children[i]->tag, "expr")) {
x = eval_op(x, op, eval(t->children[i]));
i++;
}
return x;
}
lval eval_op(lval x, char* op, lval y) {
// Returns either val if it's an error
if (x.type == LVAL_ERR) { return x; }
if (y.type == LVAL_ERR) { return y; }
if (strcmp(op, "+") == 0) { return lval_num(x.num + y.num); }
if (strcmp(op, "-") == 0) { return lval_num(x.num - y.num); }
if (strcmp(op, "*") == 0) { return lval_num(x.num * y.num); }
if (strcmp(op, "/") == 0) {
return y.num == 0
? lval_err(LERR_DIV_ZERO)
: lval_num(x.num / y.num);
}
return lval_err(LERR_BAD_OP);
}
lval lval_num(long x) {
lval v;
v.type = LVAL_NUM;
v.num = x;
return v;
}
lval lval_err(int x) {
lval v;
v.type = LVAL_ERR;
v.err = x;
return v;
}
// Print lval
void lval_print(lval v) {
switch(v.type) {
// If the type is a number, print, then break out of switch
case LVAL_NUM: printf("%li", v.num); break;
// If it's an error check what it is and print
case LVAL_ERR:
if (v.err == LERR_DIV_ZERO) {
printf("ERROR: Division by zero");
}
if (v.err == LERR_BAD_OP) {
printf("ERROR: Invalid operator");
}
if (v.err == LERR_BAD_NUM) {
printf("ERROR: Invalid number");
}
break;
}
}
void lval_println(lval v) { lval_print(v); putchar('\n'); }