diff options
author | Bad Diode <bd@badd10de.dev> | 2022-03-31 08:18:36 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2022-03-31 08:18:36 +0200 |
commit | 483a64aa0c5ee8dc925b7957e39c42744b892288 (patch) | |
tree | 86564a19bfc3255a04872815f7e1ac76d3b19cdc /src/parser.c | |
parent | 4d5d49b0d1282df08e3dc7dec3c550c544fbfccb (diff) | |
download | bdl-483a64aa0c5ee8dc925b7957e39c42744b892288.tar.gz bdl-483a64aa0c5ee8dc925b7957e39c42744b892288.zip |
Add type signature to def statements
Currently mandatory, may be optional once we have type inference.
Diffstat (limited to 'src/parser.c')
-rw-r--r-- | src/parser.c | 75 |
1 files changed, 13 insertions, 62 deletions
diff --git a/src/parser.c b/src/parser.c index 434ae98..cc94fd2 100644 --- a/src/parser.c +++ b/src/parser.c | |||
@@ -1,15 +1,6 @@ | |||
1 | #include "parser.h" | 1 | #include "parser.h" |
2 | #include "darray.h" | 2 | #include "darray.h" |
3 | 3 | ||
4 | Node * | ||
5 | alloc_node(NodeType type) { | ||
6 | // TODO: Use a bump allocator? | ||
7 | // TODO: Free memory! | ||
8 | Node *node = malloc(sizeof(Node)); | ||
9 | node->type = type; | ||
10 | return node; | ||
11 | } | ||
12 | |||
13 | Token | 4 | Token |
14 | next_token(Parser *parser) { | 5 | next_token(Parser *parser) { |
15 | return parser->tokens[parser->current++]; | 6 | return parser->tokens[parser->current++]; |
@@ -154,7 +145,18 @@ parse_def(Parser *parser) { | |||
154 | return NULL; | 145 | return NULL; |
155 | } | 146 | } |
156 | 147 | ||
157 | // TODO: Check if it has type annotation. | 148 | // TODO: Making type checking mandatory for now until we introduce |
149 | // type inference. | ||
150 | Token next = next_token(parser); | ||
151 | if (next.type != TOKEN_COLON) { | ||
152 | push_error(ERR_TYPE_PARSER, ERR_MALFORMED_EXPR, op.line, op.col); | ||
153 | return NULL; | ||
154 | } | ||
155 | Token type_name = next_token(parser); | ||
156 | if (type_name.type != TOKEN_SYMBOL) { | ||
157 | push_error(ERR_TYPE_PARSER, ERR_MALFORMED_EXPR, op.line, op.col); | ||
158 | return NULL; | ||
159 | } | ||
158 | 160 | ||
159 | Node *value = parse_next(parser); | 161 | Node *value = parse_next(parser); |
160 | if (value == NULL) { | 162 | if (value == NULL) { |
@@ -171,9 +173,7 @@ parse_def(Parser *parser) { | |||
171 | Node *node = alloc_node(NODE_DEF); | 173 | Node *node = alloc_node(NODE_DEF); |
172 | node->def.symbol = symbol; | 174 | node->def.symbol = symbol; |
173 | node->def.value = value; | 175 | node->def.value = value; |
174 | 176 | node->def.type = type_name.value; | |
175 | // TODO: Register variable in symbol table. | ||
176 | |||
177 | return node; | 177 | return node; |
178 | } | 178 | } |
179 | 179 | ||
@@ -221,55 +221,6 @@ parse_next(Parser *parser) { | |||
221 | } | 221 | } |
222 | 222 | ||
223 | void | 223 | void |
224 | print_node(Node *node) { | ||
225 | switch (node->type) { | ||
226 | case NODE_NUMBER: { | ||
227 | if (node->number.negative) { | ||
228 | printf("-"); | ||
229 | } | ||
230 | if (node->number.fractional != 0) { | ||
231 | printf("%zu.%zu", node->number.integral, node->number.fractional); | ||
232 | } else { | ||
233 | printf("%zu", node->number.integral); | ||
234 | } | ||
235 | } break; | ||
236 | case NODE_SYMBOL: | ||
237 | case NODE_STRING: { | ||
238 | sv_write(&node->string); | ||
239 | } break; | ||
240 | case NODE_BOOL: { | ||
241 | if (node->boolean) { | ||
242 | printf("true"); | ||
243 | } else { | ||
244 | printf("false"); | ||
245 | } | ||
246 | } break; | ||
247 | case NODE_BUILTIN: { | ||
248 | printf("({%s}", token_str[node->builtin.type]); | ||
249 | size_t n_args = array_size(node->builtin.args); | ||
250 | if (n_args != 0) { | ||
251 | printf(" "); | ||
252 | } | ||
253 | for (size_t i = 0; i < n_args; ++i) { | ||
254 | print_node(node->builtin.args[i]); | ||
255 | if (i < n_args - 1) { | ||
256 | printf(" "); | ||
257 | } | ||
258 | } | ||
259 | printf(")"); | ||
260 | } break; | ||
261 | case NODE_DEF: { | ||
262 | printf("(def "); | ||
263 | print_node(node->def.symbol); | ||
264 | printf(" "); | ||
265 | print_node(node->def.value); | ||
266 | printf(")"); | ||
267 | } break; | ||
268 | default: { printf("{#unknown#}"); } break; | ||
269 | } | ||
270 | } | ||
271 | |||
272 | void | ||
273 | parse(Token *tokens) { | 224 | parse(Token *tokens) { |
274 | Parser parser = { | 225 | Parser parser = { |
275 | .tokens = tokens, | 226 | .tokens = tokens, |