-
Notifications
You must be signed in to change notification settings - Fork 0
/
pussy.c
280 lines (225 loc) · 6.87 KB
/
pussy.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <stdio.h>
#include <stdlib.h>
#include "mpc.h"
#ifdef _WIN32
#include <string.h>
static char buffer[2048];
/* Fake readline function */
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;
}
/* Fake add_history function */
void add_history(char* unused) {}
/* Otherwise include the editline headers */
#elif __OpenBSD__
#include <readline/readline.h>
#include <readline/history.h>
#else
#include <editline/readline.h>
#include <editline/history.h>
#endif
/* Declare New pval Struct */
typedef struct pval {
int type;
long num;
char* err;
char* sym;
int count;
struct pval** cell;
} pval;
/* Create Enumeration of Possible pval Types */
enum { PVAL_NUM, PVAL_ERR, PVAL_SYM, PVAL_SEXPR };
/* Create Enumeration of Possible Error Types */
enum { PERR_DIV_ZERO, PERR_BAD_OP, PERR_BAD_NUM };
/* Create a new number type pointer pval */
pval* pval_num(long x) {
pval* v = malloc(sizeof(pval));
v->type = PVAL_NUM;
v->num = x;
return v;
}
/* Create a new error type pointer pval */
pval* pval_err(char* m) {
pval* v = malloc(sizeof(pval));
v->type = PVAL_ERR;
v->err = malloc(strlen(m)+1);
strcpy(v->err, m);
return v;
}
/* Create a new symbol type pointer pval */
pval* pval_sym(char* s) {
pval* v = malloc(sizeof(pval));
v->type = PVAL_SYM;
v->sym = malloc(strlen(s)+1);
strcpy(v->sym, m);
return v;
}
/* Create a new S-Expression type pointer pval */
pval* pval_sexpr(void) {
pval* v = malloc(sizeof(pval));
v->type = PVAL_SEXPR;
v->count = 0;
v->cell = NULL;
return v;
}
/*delete and free pval */
void pval_delete (pval* v) {
/* detect type and prepare content of structure for clean free */
switch (v->type) {
case PVAL_NUM: break;
case PVAL_ERR: free(v->err); break;
case PVAL_SYM: free(v->sym); break;
case PVAL_SEXPR:
for (int i = 0; i < v->count; i++) {
pval_del(v->cell[i]);
}
free(v->cell);
break;
}
free(v);
}
pval* pval_add(pval* v,pval* x) {
v->count++;
v->cell = realloc(v->cell, sizeof(pval*) * v->count);
v->cell[v->count-1] = x;
return v;
}
pval* pval_read_num(mpc_ast_t* t) {
errno = 0;
long x = strtol(t->contents, NULL, 10);
return errno != ERANGE ? pval_num(x) : pval_err("Invalid number");
}
pval* pval_read(mpc_ast_t* t) {
if (strstr(t->tag, "number")) { return pval_read_num(t); }
if (strstr(t->tag, "symbol")) { return pval_sym(t->contents); }
pval* x = NULL;
if (strcmp(t->tag, ">") == 0) { x = pval_sexpr(); }
if (strstr(t->tag, "sexpr")) { x = pval_sexpr(); }
for (int i = 0; i < t->children_num; i++) {
if (strcmp(t->children[i]->contents, "(") == 0) { continue; }
if (strcmp(t->children[i]->contents, ")") == 0) { continue; }
if (strcmp(t->children[i]->contents, "{") == 0) { continue; }
if (strcmp(t->children[i]->contents, "}") == 0) { continue; }
if (strcmp(t->children[i]->tag, "regex") == 0) { continue; }
x = pval_add(x, pval_read(t->children[i]));
}
return x;
}
void pval_expr_print(pval* v, char open, char close) {
putchar(open);
for (int i = 0; i < v->count; i++) {
pval_print(v->cell[i]);
if (i != (v->count-1)) {
putchar(' ');
}
}
putchar(close);
}
void pval_print(pval* v) {
switch (v->type) {
case PVAL_NUM: printf("%li", v->num); break;
case PVAL_ERR: printf("Error: %s", v->err); break;
case PVAL_SYM: printf("%s", v->sym); break;
case PVAL_SEXPR: pval_expr_print(v, '(', ')'); break;
}
}
/* Print an "pval"
void pval_print(pval v) {
switch (v.type) {
/* In the case the type is a number print it, then 'break' out of the switch. */
case PVAL_NUM: printf("%li\n\nPurr.", v.num); break;
/* In the case the type is an error */
case PVAL_ERR:
/* Check What exact type of error it is and print it */
if (v.err == PERR_DIV_ZERO) { printf("Error: Division By Zero!"); }
if (v.err == PERR_BAD_OP) { printf("Error: Invalid Operator!"); }
if (v.err == PERR_BAD_NUM) { printf("Error: Invalid Number!"); }
printf("\n\nMeow?");
break;
}
}
*/
void pval_println(pval v) { pval_print(v); putchar('\n'); }
pval eval_op(pval x, char* op, pval y) {
/* If either value is an error return it */
if (x.type == PVAL_ERR) { return x; }
if (y.type == PVAL_ERR) { return y; }
/* do math */
if (strcmp(op, "+") == 0) { return pval_num(x.num + y.num); }
if (strcmp(op, "-") == 0) { return pval_num(x.num - y.num); }
if (strcmp(op, "*") == 0) { return pval_num(x.num * y.num); }
if (strcmp(op, "/") == 0) {
/* If second operand is zero return error instead of result */
return y.num == 0 ? pval_err(PERR_DIV_ZERO) : pval_num(x.num / y.num);
}
return pval_err(PERR_BAD_OP);
}
pval eval(mpc_ast_t* t) {
/* If tagged as number return it directly, otherwise expression. */
if (strstr(t->tag, "number")) {
errno = 0;
long x = strtol(t->contents, NULL, 10);
return errno != ERANGE ? pval_num(x) : pval_err(PERR_BAD_NUM);
}
/* The operator is always second child. */
char* op = t->children[1]->contents;
pval x = eval(t->children[2]);
/* Iterate the remaining children, combining using our operator */
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) {
/* Create Some Parsers */
mpc_parser_t* Number = mpc_new("number");
mpc_parser_t* Symbol = mpc_new("symbol");
mpc_parser_t* Sexpr = mpc_new("sexpr");
mpc_parser_t* Expr = mpc_new("expr");
mpc_parser_t* Pussy = mpc_new("pussy");
/* Define them with the following Language */
mpca_lang(MPCA_LANG_DEFAULT,
" \
number : /-?[0-9]+/ ; \
symbol : '+' | '-' | '*' | '/' ; \
sexpr : '(' <expr>* ')' ; \
expr : <number> | <symbol> | <sexpr> ; \
pussy : /^/ <expr>* /$/ ; \
",
Number, Symbol, Sexpr, Expr, Pussy);
/* Print Version and Exit Information */
puts("Pussy Version 0.0.0.0.1");
puts("Press Ctrl+c to Exit\n");
/* In a never ending loop */
while (1) {
/* Output our prompt */
char* input = readline("=^.^= ? ");
/* Adding entered command to history */
add_history(input);
/* Attempt to Parse the user Input */
mpc_result_t r;
if (mpc_parse("<stdin>", input, Pussy, &r)) {
/* On Success Print the AST */
pval result = eval(r.output);
pval_println(result);
mpc_ast_delete(r.output);
} else {
/* Otherwise Print the Error */
mpc_err_print(r.error);
printf("\nMeow! \n");
mpc_err_delete(r.error);
}
/* Freeing variable's memory */
free(input);
}
/* Undefine and Delete our Parsers */
mpc_cleanup(5, Number, Symbol, Sexpr, Expr, Pussy);
return 0;
}