-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
160 lines (130 loc) · 3.84 KB
/
main.c
File metadata and controls
160 lines (130 loc) · 3.84 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
/* main.c */
#include <string.h>
#include "ast.h"
#include "ring.h"
#include "tokenizer.h"
void ast_print(struct AST *ast) { print_ast(stdout, ast); }
void token_print(struct token token) { printf("%s(%" PRId64 ")", TOKENS_STR[token.type], token.value); }
DECLARE_RING(oper, struct token)
DEFINE_RING(oper, struct token)
DEFINE_RING_PRINT(oper, token_print)
DECLARE_RING(ast, struct AST*)
DEFINE_RING(ast, struct AST*)
DEFINE_RING_PRINT(ast, ast_print)
//DECLARE_RING(token, struct token) //declared in "tokenizer.h"
DEFINE_RING(token, struct token)
DEFINE_RING_PRINT(token, token_print)
#define RETURN_ERROR(code, msg) return printf(msg), code
const int PRIORITY[] = {
[TOK_PLUS] = 1,
[TOK_MINUS] = 1,
[TOK_MUL] = 5,
[TOK_DIV] = 5,
[TOK_NEG] = 5,
[TOK_OPEN] = 0,
[TOK_CLOSE] = 0,
[TOK_LIT] = 100,
[TOK_END] = 0,
[TOK_ERROR] = 100
};
const int TOKEN_TO_AST[] = {
[TOK_PLUS] = BIN_PLUS,
[TOK_MINUS] = BIN_MINUS,
[TOK_MUL] = BIN_MUL,
[TOK_DIV] = BIN_DIV,
[TOK_NEG] = UN_NEG
};
void exit_error(int code, char* msg){
fprintf(stderr, "%s \n", msg);
exit(code);
}
int token_is_unop(struct token token){
return (token.type == TOK_NEG);
}
void exec_top(struct ring_oper* opers, struct ring_ast* asts){
struct token top = ring_oper_pop(&opers);
struct AST* new;
if (token_is_unop(top)){
struct AST* operand = ring_ast_pop(&asts);
if (operand == NULL){ exit_error(-1, "Incorrect expression: no operand."); }
new = unop(TOKEN_TO_AST[top.type], operand);
}
else{
struct AST* right = ring_ast_pop(&asts);
if (right == NULL){ exit_error(-1, "Incorrect expression: no args!"); }
struct AST* left = ring_ast_pop(&asts);
if (left == NULL){ exit_error(-1, "Incorrect expression: need one more arg!"); }
new = binop(TOKEN_TO_AST[top.type], left, right);
}
ring_ast_push(&asts, new);
}
void exec_until(enum token_type const end_type, struct ring_oper* opers, struct ring_ast* asts){
while(1){
struct token top = ring_oper_last(opers);
if (top.type == end_type){ ring_oper_pop(&opers); break; }
if (top.type == TOK_END){ exit_error(-1, "Incorrect expression: rich end-token before expected!"); }
exec_top(opers, asts);
}
}
void process_one_token(struct token next, struct ring_oper* opers, struct ring_ast* asts){
struct token top = ring_oper_last(opers);
if(next.type == TOK_LIT){
ring_ast_push(&asts, lit(next.value));
return;
}
if(next.type == TOK_OPEN){
ring_oper_push(&opers, next);
return;
}
if(next.type == TOK_CLOSE){
exec_until(TOK_OPEN, opers, asts);
return;
}
if (PRIORITY[next.type] > PRIORITY[top.type]){
ring_oper_push(&opers, next);
}
else{
exec_top(opers, asts);
ring_oper_push(&opers, next);
}
}
struct AST *build_ast(char *str)
{
struct ring_token *tokens = tokenize(str);
if (tokens == NULL)
RETURN_ERROR(NULL, "Tokenization error.\n");
ring_token_print(tokens);
struct token end = {TOK_END, 0};
struct ring_oper* opers = ring_oper_create(end);
struct ring_ast* asts = ring_ast_create(NULL);
while(ring_token_first(tokens).type != TOK_END){
struct token next = ring_token_pop_top(&tokens);
//token_print(next), printf("\n");
process_one_token(next, opers, asts);
//ring_oper_print(opers), printf("\n");
}
exec_until(TOK_END, opers, asts);
ring_token_free(&tokens);
ring_oper_free(&opers);
return ring_ast_pop(&asts);
}
int main()
{
const int MAX_LEN = 1024;
char str[MAX_LEN];
if (fgets(str, MAX_LEN, stdin) == NULL)
RETURN_ERROR(0, "Input is empty.");
if (str[strlen(str) - 1] == '\n')
str[strlen(str) - 1] = '\0';
struct AST *ast = build_ast(str);
if (ast == NULL)
printf("AST build error.\n");
else
{
print_ast(stdout, ast);
printf("\n\n%s = %" PRId64 "\n", str, calc_ast(ast));
p_print_ast(stdout, ast);
printf(" = %" PRId64 "\n", calc_ast(ast));
}
return 0;
}