/
tora.c
268 lines (239 loc) · 5.87 KB
/
tora.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//
// TORA.c
// TORA
//
// Created by Nial on 30/08/2015.
// Copyright © 2015 Nial. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "tora.h"
E4C_DEFINE_EXCEPTION(ParserException, "Parser Exception.", RuntimeException);
E4C_DEFINE_EXCEPTION(InterpretterException, "Interpretter Exception.", RuntimeException);
regex_t digit_regex;
regex_t op_regex;
regex_t id_regex;
regex_t id_start_regex;
regex_t punctuation_regex;
regex_t whitespace_regex;
regex_t not_newline_regex;
TORALinkedList *token_queue = NULL;
TORALinkedList *environment_queue = NULL;
TORALinkedList *interpretter_queue = NULL;
const char *tora_keywords[] = {
"if",
"else",
"func",
"true",
"false",
"while",
"return"
};
const TORAOperatorPrecendence tora_operator_precedence[] = {
{ "=", 1 },
{ "||", 2 },
{ "&&", 3 },
{ "<", 7 }, { ">", 7 }, { "<=", 7 }, { ">=", 7 }, { "==", 7 }, { "!=", 7 },
{ "+", 10 }, { "-", 10 },
{ "*", 20 }, { "/", 20 }, { "%", 20 }, { ":", 20 }
};
// General
bool tora_init()
{
num_malloc = 0;
num_free = 0;
int r = 0;
r = regcomp(&digit_regex, "[[:digit:]]", REG_EXTENDED|REG_ICASE);
if (r)
{
return false;
}
r = regcomp(&op_regex, "\\+|-|\\*|/|%|=|&|\\||<|>|!", REG_EXTENDED|REG_ICASE);
if (r)
{
return false;
}
r = regcomp(&punctuation_regex, ",|;|:|\\(|\\)|{|}|\\[|\\]", REG_EXTENDED|REG_ICASE);
if (r)
{
return false;
}
r = regcomp(&whitespace_regex, "[ \n\t]", REG_EXTENDED|REG_NEWLINE|REG_ICASE);
if (r)
{
return false;
}
r = regcomp(¬_newline_regex, "[^\n]", REG_EXTENDED|REG_NEWLINE|REG_ICASE);
if (r)
{
return false;
}
// A regex to match the beginning of variable names
r = regcomp(&id_start_regex, "[a-z_]", REG_EXTENDED|REG_ICASE);
if (r)
{
return false;
}
// A regex to match the end of variable names
r = regcomp(&id_regex, "[0-9]", REG_EXTENDED|REG_ICASE);
if (r)
{
return false;
}
return true;
}
void tora_shutdown()
{
regfree(&digit_regex);
regfree(&op_regex);
regfree(&punctuation_regex);
regfree(&whitespace_regex);
regfree(¬_newline_regex);
regfree(&id_start_regex);
regfree(&id_regex);
}
// String helpers
char *tora_strcpy(char *str)
{
if(str == NULL) return NULL;
char *ret = tora_malloc(strlen(str)+1);
if(ret != NULL)
{
return strcpy(ret, str);
}
return NULL;
}
// Error handling
void tora_parser_exception(uint64_t line, uint64_t col, char *msg)
{
char extra[255];
snprintf(extra, 255, "[line %llu, col: %llu] %s", line, col, msg);
e4c_throw(&ParserException, __FILE__, __LINE__, extra);
}
char *tora_variadic_string(const char *fmt, ...)
{
int len = 0, size = 100;
va_list ap;
char *msg = NULL;
if((msg = malloc((size_t)size)) == NULL)
{
return NULL;
}
char *tmp_msg = NULL;
while(1)
{
va_start(ap, fmt);
len = vsnprintf(msg, size, fmt, ap);
va_end(ap);
// If we've now malloc'd enough space, we can ditch out
if(len > -1 && len < size) break;
// Otherwise, try resizing our new_msg
size = (len > -1) ? len+1 : size*2;
if ((tmp_msg = realloc(msg, (size_t)size)) == NULL)
{
free(msg);
return NULL;
}
else
{
msg = tmp_msg;
}
}
return msg;
}
// Syntax helpers
int get_operator_precendence(char *cmp)
{
int i = 0;
for(i = 0; i < TORA_NUM_OPERATORS; i++)
{
const TORAOperatorPrecendence current_operator = tora_operator_precedence[i];
if(!strcmp(current_operator.op, cmp))
{
return current_operator.level;
}
}
return -1;
}
bool is_keyword(char *keyword)
{
int i = 0;
for(i = 0; i < TORA_NUM_KEYWORDS; i++)
{
if(strlen(keyword) == strlen(tora_keywords[i]) && strncmp(keyword, tora_keywords[i], strlen(keyword)) == 0)
{
return true;
}
}
return false;
}
bool is_digit(char ch, char *value)
{
TORA_UNUSED(value);
char test_value[2] = {ch, '\0'};
int r = regexec(&digit_regex, test_value, 0, NULL, 0);
return r == 0;
}
bool is_numeric_value(char ch, char *value)
{
assert(ch);
assert(value);
char test_value[2] = {ch, '\0'};
// dots are valid for numeric values, but only if we don't already have one
if(test_value[0] == '.')
{
char *existing_dot = strchr(value, '.');
if(existing_dot)
{
return false;
}
return true;
}
else
{
return is_digit(ch, value);
}
}
bool is_op_char(char ch, char *value)
{
TORA_UNUSED(value);
char test_value[2] = {ch, '\0'};
int r = regexec(&op_regex, test_value, 0, NULL, 0);
return r == 0;
}
bool is_id_start(char ch, char *value)
{
TORA_UNUSED(value);
char test_value[2] = {ch, '\0'};
int r = regexec(&id_start_regex, test_value, 0, NULL, 0);
return r == 0;
}
bool is_id(char ch, char *value)
{
TORA_UNUSED(value);
char test_value[2] = {ch, '\0'};
return is_id_start(ch, value) || regexec(&id_regex, test_value, 0, NULL, 0) == 0;
}
bool is_punctuation(char ch, char *value)
{
TORA_UNUSED(value);
char test_value[2] = {ch, '\0'};
int r = regexec(&punctuation_regex, test_value, 0, NULL, 0);
return r == 0;
}
bool is_whitespace(char ch, char *value)
{
TORA_UNUSED(value);
char test_value[2] = {ch, '\0'};
int r = regexec(&whitespace_regex, test_value, 0, NULL, 0);
return r == 0;
}
bool is_not_newline(char ch, char *value)
{
TORA_UNUSED(value);
char test_value[2] = {ch, '\0'};
int r = regexec(¬_newline_regex, test_value, 0, NULL, 0);
return r == 0;
}