Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
440 lines (359 loc) · 8.79 KB
/
Copy pathmain.c
File metadata and controls
440 lines (359 loc) · 8.79 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include "mpc.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
// ints unsigned
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
// ints signed
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
// ptrs (unsigned & signed)
typedef uintptr_t uptr;
typedef intptr_t iptr;
// size_t unsigned + signed
// (usize and isize are now defined in mpc.h)
// floats & doubles
typedef float f32;
typedef double f64;
// bool
typedef bool b8;
#include <string.h>
typedef struct lval {
i32 type;
i64 num;
i32 err;
char* sym;
i32 count;
struct lval** cell;
} lval;
/* enumeration for possible lval types (lisp values) */
enum {
LVAL_NUM,
LVAL_ERR,
LVAL_SYM,
LVAL_SEXPR
};
/* enumeration of possible err types */
enum {
LERR_DIV_BY_ZERO,
LERR_BAD_OP,
LERR_BAD_NUM,
LERR_BAD_TYPE
};
// IF we are using a win32 sys
#ifdef _WIN32
static char buffer[2048]; // buffer for user input
/* fake readline func */
char* readline(char* prompt) {
fputs(prompt, stdout);
fgets(buffer, 2048, stdin);
usize len = strlen(buffer);
char* cpy = malloc(len+1); // + 1 because we include the null terminator
strcpy(cpy, buffer);
cpy[len-1] = '\0'; //removes null terminator from cpy
return cpy;
}
/* fake add_history func */
void add_history(char* unused) {}
#else
#include <editline/readline.h>
// #include <editline/history.h>
#endif
lval* lval_num(i64 x) {
lval* v = malloc(sizeof(lval));
v->type = LVAL_NUM;
v->num = x;
return v;
}
lval* lval_err(i32 x) {
lval* v = malloc(sizeof(lval));
v->type = LVAL_ERR;
v->err = x;
return v;
}
lval* lval_sym(char* s) {
lval* v = malloc(sizeof(lval));
v->type = LVAL_SYM;
v->sym = malloc(strlen(s) + 1);
strcpy(v->sym, s);
return v;
}
lval* lval_sexpr(void) {
lval* v = malloc(sizeof(lval));
v->type = LVAL_SEXPR;
v->count = 0;
v->cell = NULL;
return v;
}
void lval_print(lval* v);
void lval_expr_print(lval* v, char open, char close);
void lval_expr_print(lval* v, char open, char close) {
putchar(open);
for (i32 i = 0; i < v->count; i++) {
lval_print(v->cell[i]);
// dont print trailing space if last element
if (i != v->count-1) {
putchar(' ');
}
}
putchar(close);
}
/* prints lisp val */
void lval_print(lval* v) {
switch(v->type) {
case LVAL_NUM:
printf("%lld", v->num);
break;
case LVAL_ERR:
if(v->err == LERR_DIV_BY_ZERO) {
printf("Error: Division By Zero");
}
else if(v->err == LERR_BAD_OP) {
printf("Error: Bad Operator");
}
else if(v->err == LERR_BAD_NUM) {
printf("Error: Invalid Number");
}
break;
case LVAL_SYM:
printf("%s", v->sym);
break;
case LVAL_SEXPR:
lval_expr_print(v, '(', ')');
break;
}
}
void lval_println(lval* v) {
lval_print(v);
putchar('\n');
}
void lval_del(lval* v) {
switch(v->type) {
case LVAL_NUM:
break;
case LVAL_ERR:
break;
case LVAL_SYM:
free(v->sym);
break;
case LVAL_SEXPR:
for (i64 i = 0; i < v->count; i++) {
lval_del(v->cell[i]);
}
free(v->cell);
break;
}
free(v);
}
lval* lval_add(lval* v, lval* x);
lval* lval_read_num(mpc_ast_t* t) {
errno = 0;
i64 x = strtol(t->contents, NULL, 10);
return errno != ERANGE ? lval_num(x) : lval_err(LERR_BAD_NUM);
}
lval* lval_read(mpc_ast_t* t) {
if (strstr(t->tag, "number")) {
return lval_read_num(t);
}
if(strstr(t->tag, "symbol")) {
return lval_sym(t->contents);
}
lval* x = lval_sexpr();
for (i32 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]->tag, "regex") == 0) {
continue;
}
x = lval_add(x, lval_read(t->children[i]));
}
return x;
}
lval* lval_add(lval* v, lval* x) {
v->count++;
lval** temp = realloc(v->cell, sizeof(lval*) * v->count);
if (temp == NULL) {
free(v);
return NULL;
}
v->cell = temp;
v->cell[v->count-1] = x;
return v;
}
lval* eval_op(lval* x, char* op, lval* y);
lval* eval(mpc_ast_t* t);
lval* lval_eval(lval* v);
lval* eval(mpc_ast_t* t) {
/* if tagged as num return */
if (strstr(t->tag, "number")) {
errno = 0;
i64 x = strtol(t->contents, NULL, 10);
if (errno != 0) {
return lval_err(LERR_BAD_NUM);
}
return lval_num(x);
}
/* operator gives second child */
char* op = t->children[1]->contents;
lval* x = eval(t->children[2]);
usize i = 3;
while (i < (usize)t->children_num && 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) {
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_BY_ZERO)
: lval_num(x->num / y->num);
}
return lval_err(LERR_BAD_OP);
}
lval* lval_pop(lval* v, i32 i) {
/* find the item @ i*/
lval* x = v->cell[i];
/* Shift memory after the item at "i" over the top */
memmove(&v->cell[i], &v->cell[i+1], sizeof(lval*) * (v->count - 1));
v->count--;
/* realloc memory used */
v->cell = realloc(v->cell, sizeof(lval*) * v->count);
return x;
}
lval* lval_take(lval* v, i32 i) {
lval* x = lval_pop(v, i);
lval_del(v);
return x;
}
lval* builtin_op(lval* a, char* op) {
/* ensures all arguments are numbers */
for (i32 i = 0; i < a->count; i++){
if(a->cell[i]->type != LVAL_NUM) {
lval_del(a);
return lval_err(LERR_BAD_TYPE);
}
}
/* pops first element */
lval* x = lval_pop(a, 0);
/* if no args provided and subtraction, then perform unary negation */
if((strcmp(op, "-") == 0) && a->count == 0) {
x->num = -x->num;
}
while (a->count > 0) {
// pops next element
lval *y = lval_pop(a, 0);
if (strcmp(op, "+") == 0) { x->num += y->num; }
if (strcmp(op, "-") == 0) { x->num -= y->num; }
if (strcmp(op, "*") == 0) { x->num *= y->num; }
if (strcmp(op, "/") == 0) {
if (y->num == 0) {
lval_del(y);
lval_del(a);
lval_del(x);
return lval_err(LERR_DIV_BY_ZERO);
}
x->num /= y->num;
}
lval_del(y);
}
lval_del(a);
return x;
}
lval* lval_expr_sexpr(lval* v) {
/* Evaluating children */
for (i32 i = 0; i < v->count; i++) {
v->cell[i] = lval_eval(v->cell[i]);
}
for (i32 i = 0; i < v->count; i++) {
if(v->cell[i]->type == LVAL_ERR) {
lval_take(v, i);
}
}
if (v->count == 0) {
return v;
}
if (v->count == 1) {
return lval_take(v, 0);
}
lval* f = lval_pop(v, 0);
if (f->type != LVAL_SYM) {
lval_del(f);
lval_del(v);
return lval_err(LERR_BAD_OP);
}
/* Call builtin with operator */
lval* result = builtin_op(v, f->sym);
lval_del(f);
return result;
}
lval* lval_eval(lval* v) {
if (v->type == LVAL_SEXPR){
return lval_expr_sexpr(v);
}
return v;
}
i32 main(i32 argc, char** argv) {
(void)argc;
(void)argv;
/* 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* Miso = mpc_new("miso");
/* Defined with the following regular expressions language */
mpca_lang(MPCA_LANG_DEFAULT,
" \
number : /-?[0-9]+/ ; \
symbol : '+' | '-' | '*' | '/' ; \
sexpr : '(' <expr>* ')' ; \
expr : <number> | <symbol> | <sexpr> ; \
miso : /^/ <symbol> <expr>+ /$/ ; \
",
Number, Symbol, Sexpr, Expr, Miso, (mpc_parser_t*)NULL);
const char *name = "Miso";
const char *version = "0.0.1";
const char *credits = "contribute @ github.com/andrefetch/miso";
printf("%s Version %s\n", name, version);
puts("Press Ctrl+c to Exit");
printf("%s\n\n", credits);
while (1) {
char* input = readline("miso >> "); //outputs prompt
if (input == NULL) {
break;
}
add_history(input);
// printf("Your input was.. %s\n", input);
mpc_result_t r;
if (mpc_parse("<stdin>", input, Miso, &r)) {
lval* x = lval_eval(lval_read(r.output));
lval_println(x);
lval_del(x);
mpc_ast_delete(r.output);
} else {
mpc_err_print(r.error);
mpc_err_delete(r.error);
}
free(input);
}
/* Before return, undefine and del parsers */
mpc_cleanup(5, Number, Symbol, Sexpr, Expr, Miso);
return 0;
}