-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
161 lines (155 loc) · 4.04 KB
/
parser.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
154
155
156
157
158
159
160
161
#include "compiler.h"
#include "helpers/vector.h"
static struct compile_process *current_process;
static struct token *parser_last_token;
struct history
{
int flags;
};
int parse_expressionable_single(struct history *history);
void parse_expressionable(struct history *history);
struct history *history_begin(int flags)
{
struct history *history = calloc(1, sizeof(struct history));
history->flags = flags;
return history;
}
struct history *history_down(struct history *history, int flags)
{
struct history *new_history = calloc(1, sizeof(struct history));
memcpy(new_history, history, sizeof(struct history));
new_history->flags = flags;
return new_history;
}
static void parser_ignore_nl_or_comment(struct token *token)
{
while (token && token_is_nl_or_comment_or_newline_separator(token))
{
vector_peek(current_process->token_vec);
token = vector_peek_no_increment(current_process->token_vec);
}
}
static struct token *token_next()
{
struct token *next_token = vector_peek_no_increment(current_process->token_vec);
parser_ignore_nl_or_comment(next_token);
current_process->pos = next_token->pos;
parser_last_token = next_token;
return vector_peek(current_process->token_vec);
}
static struct token *token_peek_next()
{
struct token *next_token = vector_peek_no_increment(current_process->token_vec);
parser_ignore_nl_or_comment(next_token);
return vector_peek_no_increment(current_process->token_vec);
}
void parse_single_token_to_node()
{
struct token *token = token_next();
struct node *node = NULL;
switch (token->type)
{
case TOKEN_TYPE_NUMBER:
node = node_create(&(struct node){.type = NODE_TYPE_NUMBER, .llnum = token->llnum});
break;
case TOKEN_TYPE_IDENTIFIER:
node = node_create(&(struct node){.type = NODE_TYPE_IDENTIFIER, .sval = token->sval});
break;
case TOKEN_TYPE_STRING:
node = node_create(&(struct node){.type = NODE_TYPE_STRING, .sval = token->sval});
break;
default:
compiler_error(current_process, "This is not a single token that can be converted to a node");
}
}
void parse_expressionable_for_op(struct history *history, const char *op)
{
parse_expressionable(history);
}
void parse_exp_normal(struct history *history)
{
struct token *op_token = token_peek_next();
const char *op = op_token->sval;
struct node *node_left = node_peek_expressionable_or_null();
if (!node_left)
{
return;
}
// pop off operator token
token_next();
// pot off left node
node_pop();
node_left->flags = NODE_FLAG_INSIDE_EXPRESSION;
parse_expressionable_for_op(history_down(history, history->flags), op);
struct node *node_right = node_pop();
node_right->flags |= NODE_FLAG_INSIDE_EXPRESSION;
make_exp_node(node_left, node_right, op);
struct node *exp_node = node_pop();
// reorder expression
node_push(exp_node);
}
int parse_exp(struct history *history)
{
parse_exp_normal(history);
return 0;
}
int parse_expressionable_single(struct history *history)
{
struct token *token = token_peek_next();
if (!token)
{
return -1;
}
history->flags |= NODE_FLAG_INSIDE_EXPRESSION;
int res = -1;
switch (token->type)
{
case TOKEN_TYPE_NUMBER:
parse_single_token_to_node();
res = 0;
break;
case TOKEN_TYPE_OPERATOR:
parse_exp(history);
res = 0;
break;
}
return res;
}
void parse_expressionable(struct history *history)
{
while (parse_expressionable_single(history) == 0)
{
}
}
int parse_next()
{
struct token *token = token_peek_next();
if (!token)
{
return -1;
}
int rest = 0;
switch (token->type)
{
case TOKEN_TYPE_NUMBER:
case TOKEN_TYPE_IDENTIFIER:
case TOKEN_TYPE_STRING:
parse_expressionable(history_begin(0));
break;
}
return 0;
}
int parse(struct compile_process *process)
{
current_process = process;
parser_last_token = NULL;
node_set_vector(process->node_vec, process->node_tree_vec);
struct node *node = NULL;
vector_set_peek_pointer(process->token_vec, 0);
while (parse_next() == 0)
{
node = node_peek();
vector_push(process->node_tree_vec, &node);
}
return PARSE_ALL_OK;
}