-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
49 lines (38 loc) · 768 Bytes
/
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
#include "rdp_calc.h"
void prompt(char *expr)
{
printf("Enter expression: ");
fflush(stdout);
int nbytes = read(0, expr, MAXSIZE);
expr[nbytes] = '\0';
}
int main(void)
{
char expr[MAXSIZE];
lexer_t *tokens;
ASTreeNode_t *AbstractSyntaxTree;
while (true)
{
prompt(expr);
if (!*expr)
continue ;
tokens = lexer(expr);
if (!tokens)
continue ;
if (tokens->len <= 1) {
list_clear(tokens);
continue ;
}
AbstractSyntaxTree = parser(tokens);
if (!AbstractSyntaxTree)
continue ;
// displays
displayASTree(AbstractSyntaxTree);
displayExpression(AbstractSyntaxTree);
printf("%s\nResult: %d\n", KGRN, eval(AbstractSyntaxTree));
printf(KNRM);
// Clear memory
list_clear(tokens);
ast_clear(AbstractSyntaxTree);
}
}