From 483a64aa0c5ee8dc925b7957e39c42744b892288 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Thu, 31 Mar 2022 08:18:36 +0200 Subject: Add type signature to def statements Currently mandatory, may be optional once we have type inference. --- src/parser.c | 75 +++++++++++------------------------------------------------- 1 file changed, 13 insertions(+), 62 deletions(-) (limited to 'src/parser.c') 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 @@ #include "parser.h" #include "darray.h" -Node * -alloc_node(NodeType type) { - // TODO: Use a bump allocator? - // TODO: Free memory! - Node *node = malloc(sizeof(Node)); - node->type = type; - return node; -} - Token next_token(Parser *parser) { return parser->tokens[parser->current++]; @@ -154,7 +145,18 @@ parse_def(Parser *parser) { return NULL; } - // TODO: Check if it has type annotation. + // TODO: Making type checking mandatory for now until we introduce + // type inference. + Token next = next_token(parser); + if (next.type != TOKEN_COLON) { + push_error(ERR_TYPE_PARSER, ERR_MALFORMED_EXPR, op.line, op.col); + return NULL; + } + Token type_name = next_token(parser); + if (type_name.type != TOKEN_SYMBOL) { + push_error(ERR_TYPE_PARSER, ERR_MALFORMED_EXPR, op.line, op.col); + return NULL; + } Node *value = parse_next(parser); if (value == NULL) { @@ -171,9 +173,7 @@ parse_def(Parser *parser) { Node *node = alloc_node(NODE_DEF); node->def.symbol = symbol; node->def.value = value; - - // TODO: Register variable in symbol table. - + node->def.type = type_name.value; return node; } @@ -220,55 +220,6 @@ parse_next(Parser *parser) { } } -void -print_node(Node *node) { - switch (node->type) { - case NODE_NUMBER: { - if (node->number.negative) { - printf("-"); - } - if (node->number.fractional != 0) { - printf("%zu.%zu", node->number.integral, node->number.fractional); - } else { - printf("%zu", node->number.integral); - } - } break; - case NODE_SYMBOL: - case NODE_STRING: { - sv_write(&node->string); - } break; - case NODE_BOOL: { - if (node->boolean) { - printf("true"); - } else { - printf("false"); - } - } break; - case NODE_BUILTIN: { - printf("({%s}", token_str[node->builtin.type]); - size_t n_args = array_size(node->builtin.args); - if (n_args != 0) { - printf(" "); - } - for (size_t i = 0; i < n_args; ++i) { - print_node(node->builtin.args[i]); - if (i < n_args - 1) { - printf(" "); - } - } - printf(")"); - } break; - case NODE_DEF: { - printf("(def "); - print_node(node->def.symbol); - printf(" "); - print_node(node->def.value); - printf(")"); - } break; - default: { printf("{#unknown#}"); } break; - } -} - void parse(Token *tokens) { Parser parser = { -- cgit v1.2.1