This repository was archived by the owner on Sep 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
83 lines (65 loc) · 1.9 KB
/
Copy pathmain.c
File metadata and controls
83 lines (65 loc) · 1.9 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tokens.h"
#include "lexer.h"
#include "nodes.h"
#include "parser.h"
#include "interpreter.h"
#include "values.h"
int main(void) {
char buff[256];
printf("calc > ");
while (fgets(buff, sizeof(buff), stdin) != NULL) {
buff[strcspn(buff, "\n")] = 0;
if (!*buff) {
printf("calc > ");
continue;
}
Lexer lexer;
Tokens *tokens = lexer_get_tokens(&lexer, buff, sizeof(buff));
if (tokens != NULL) {
//print_tokens(tokens);
}
if (*lexer.error) {
fprintf(stderr, "%s\n", lexer.error);
printf("calc > ");
continue;
// If there is an error, the lexer actually frees the tokens for you
}
Parser parser;
Node *tree;
if (tokens != NULL) {
tree = parser_parse(&parser, &tokens);
if (tokens != NULL && !*parser.error) {
free_tokens(tokens);
tokens = NULL;
}
}
if (tree != NULL) {
//print_node(tree); putchar('\n');
}
if (*parser.error && !*lexer.error) {
fprintf(stderr, "%s\n", parser.error);
tokens = NULL;
printf("calc > ");
continue;
// If there is an error, the parser actually frees the nodes for you
}
if (tree != NULL) {
Interpreter interpreter;
Number result = interpreter_visit(&interpreter, tree);
if (interpreter.error == NULL && !*parser.error) {
print_number(&result);
} else {
fprintf(stderr, "%s\n", interpreter.error);
}
if (tree != NULL) {
free_node(tree);
tree = NULL;
}
}
printf("calc > ");
}
return 0;
}