-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.c
117 lines (86 loc) · 2.98 KB
/
ast.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include "./ast.h"
static double convert_to_double(M_Token *token) {
char buffer[token->size + 1];
memcpy(buffer, token->value, token->size);
buffer[token->size] = '\0';
return strtod(buffer, NULL);
}
static M_Expression_Operator from_token_kind_to_expression_operator(M_Token_Kind kind) {
switch (kind) {
case M_PLUS:
return M_OP_PLUS;
case M_TIMES:
return M_OP_TIMES;
case M_MINUS:
return M_OP_SUBTRACT;
case M_DIVIDE:
return M_OP_DIVIDE;
case M_MOD:
return M_OP_MOD;
case M_POW:
return M_OP_POW;
default:
assert(0 && "from_token_kind_to_expression_operator: unreacheable");
}
}
static M_Expression *parse_primary_expression(M_Token **tokens) {
if (*tokens == NULL) return NULL;
M_Token *current = *tokens;
if (current->kind == M_NUMBER) {
M_Expression *expr = malloc(sizeof(M_Expression));
expr->kind = M_EK_NUMBER;
expr->number = convert_to_double(current);
*tokens = current->next;
return expr;
} else if (current->kind == M_LPAREN) {
*tokens = current->next;
M_Expression *expr = parse_expression(tokens);
if (*tokens == NULL || (*tokens)->kind != M_RPAREN) {
fprintf(stderr, "syntax error\n");
exit(1);
}
*tokens = (*tokens)->next;
return expr;
} else {
fprintf(stderr, "unexpected token\n");
exit(1);
}
return NULL;
}
static M_Expression *parse_term(M_Token **tokens) {
if (*tokens == NULL) return NULL;
M_Expression *left = parse_primary_expression(tokens);
while (*tokens != NULL && ((*tokens)->kind == M_TIMES || (*tokens)->kind == M_DIVIDE || (*tokens)->kind == M_MOD || (*tokens)->kind == M_POW)) {
M_Token *op_token = *tokens;
*tokens = (*tokens)->next;
M_Expression *right = parse_primary_expression(tokens);
M_Expression *expr = malloc(sizeof(M_Expression));
expr->kind = M_EK_BINARY;
expr->binary.operator = from_token_kind_to_expression_operator(op_token->kind);
expr->binary.left = left;
expr->binary.right = right;
left = expr;
}
return left;
}
M_Expression *parse_expression(M_Token **tokens) {
if (*tokens == NULL) return NULL;
M_Expression *left = parse_term(tokens);
while (*tokens != NULL && ((*tokens)->kind == M_PLUS || (*tokens)->kind == M_MINUS)) {
M_Token *op_token = *tokens;
*tokens = (*tokens)->next;
M_Expression *right = parse_term(tokens);
M_Expression *expr = malloc(sizeof(M_Expression));
expr->kind = M_EK_BINARY;
expr->binary.operator = from_token_kind_to_expression_operator(op_token->kind);
expr->binary.left = left;
expr->binary.right = right;
left = expr;
}
return left;
}