-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
153 lines (132 loc) · 3.23 KB
/
main.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
#include "mpc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "eval.h"
#include "errors.h"
/* If we are compiling on Windows compile these functions */
#ifdef _WIN32
static char buffer[2048];
/* Fake readline function */
char *readline(char *prompt)
{
fputs(prompt, stdout);
fgets(buffer, 2048, stdin);
size_t len = strlen(buffer);
char *cpy = malloc(len + 1);
strcpy(cpy, buffer);
if (len > 0 && cpy[len - 1] == '\n')
{
cpy[len - 1] = '\0'; // Remove newline character if present
}
return cpy;
}
/* Fake add_history function */
void add_history(char *unused) {}
#else
#include <editline/readline.h>
#include <histedit.h>
#endif
mpc_parser_t *Number;
mpc_parser_t *Symbol;
mpc_parser_t *Sexpr;
mpc_parser_t *Expr;
mpc_parser_t *maki;
devilval *devilval_read_num(mpc_ast_t *t)
{
errno = 0;
long x = strtol(t->contents, NULL, 10);
return errno != ERANGE ? devilval_num(x) : devilval_err("invalid number");
}
devilval *devilval_add(devilval *v, devilval *x)
{
v->count++;
v->cell = realloc(v->cell, sizeof(devilval *) * v->count);
v->cell[v->count - 1] = x;
return v;
}
devilval *devilval_read(mpc_ast_t *t)
{
/* If Symbol or Number return conversion to that type */
if (strstr(t->tag, "number"))
{
return devilval_read_num(t);
}
if (strstr(t->tag, "symbol"))
{
return devilval_sym(t->contents);
}
/* If root (>) or sexpr then create empty list */
devilval *x = NULL;
if (strcmp(t->tag, ">") == 0)
{
x = devilval_sexpr();
}
if (strstr(t->tag, "sexpr"))
{
x = devilval_sexpr();
}
/* Fill this list with any valid expression contained within */
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]->tag, "regex") == 0)
{
continue;
}
x = devilval_add(x, devilval_read(t->children[i]));
}
return x;
}
int main(int argc, char *argv[])
{
// Create parsers
Number = mpc_new("number");
Symbol = mpc_new("symbol");
Sexpr = mpc_new("sexpr");
Expr = mpc_new("expr");
maki = mpc_new("maki");
// Define them with the following Language
mpca_lang(MPCA_LANG_DEFAULT,
" \
number : /-?[0-9]+(\\.[0-9]+)?/ ; \
symbol : '+' | '-' | '*' | '/' | '^' | \"max\" | \"min\"; \
sexpr : '(' <expr>* ')' ; \
expr : <number> | <symbol> | <sexpr> ; \
maki : /^/ <expr>* /$/ ; \
",
Number, Symbol, Sexpr, Expr, maki);
puts("makima version 0.0.0.2");
puts("Press Ctrl + C to exit ");
while (1)
{
char *input = readline("makima> ");
add_history(input);
mpc_result_t r;
if (mpc_parse("<stdin>", input, maki, &r))
{
/* On Success Print the AST */
mpc_ast_print(r.output);
devilval *x = devilval_eval(devilval_read(r.output));
devilval_println(x);
devilval_del(x);
}
else
{
/* Otherwise Print the Error */
mpc_err_print(r.error);
mpc_err_delete(r.error);
}
free(input);
}
/* Cleanup */
mpc_cleanup(5, Number, Symbol, Sexpr, Expr, maki);
return 0;
}