diff --git a/src/compiler/fzl_parser.c b/src/compiler/fzl_parser.c index 6bf159d..559bf55 100644 --- a/src/compiler/fzl_parser.c +++ b/src/compiler/fzl_parser.c @@ -1,5 +1,5 @@ -#include "compiler/fzl_token.h" #include +#include #include #include #include @@ -166,22 +166,29 @@ fzl_node_t *parse_primary(fzl_parser_t *parser) while (peek(parser) && peek(parser)->type != FZL_TOKTYPE_STR_FORMAT_END) { - if (remainder <= count) - { - remainder *= 2; - entries = realloc(entries, remainder * sizeof(fzl_node_t *)); - if (!entries) - { - snprintf(plast_err, sizeof(plast_err), "out of memory"); - return NULL; - } - } - if (is(parser, FZL_TOKTYPE_STR_FORMAT_END)) break; + if (remainder <= count) + { + remainder *= 2; + fzl_node_t **newentries = + realloc(entries, remainder * sizeof(fzl_node_t *)); + if (!newentries) + { + snprintf(plast_err, sizeof(plast_err), "out of memory"); + for (size_t i = 0; i < count; i++) + fzl_freeast(entries[i]); + + free(entries); + return NULL; + } + + entries = newentries; + } + fzl_node_t *expr = parse_expression(parser); - if (!expr) + if (expr == NULL) { for (size_t i = 0; i < count; i++) fzl_freeast(entries[i]); @@ -192,6 +199,7 @@ fzl_node_t *parse_primary(fzl_parser_t *parser) entries[count++] = expr; } + fzl_token_t *end = consume(parser); if (end->type != FZL_TOKTYPE_STR_FORMAT_END) { @@ -644,8 +652,8 @@ fzl_node_t *parse_new(fzl_parser_t *parser) fzl_token_t *new = consume(parser); fzl_node_t *objname = parse_or(parser); - if (objname->type != FZL_NODETYPE_IDENTIFIER && - objname->type != FZL_NODETYPE_FIELD) + if (!objname || (objname->type != FZL_NODETYPE_IDENTIFIER && + objname->type != FZL_NODETYPE_FIELD)) { fzl_freeast(objname); snprintf(plast_err, sizeof(plast_err), @@ -1332,7 +1340,10 @@ fzl_node_t *parse_program(fzl_parser_t *parser) { node = parse_statement(parser); if (!node) + { + fzl_freeast(first); return NULL; + } if (!last) { @@ -1398,50 +1409,35 @@ void fzl_freeast(fzl_node_t *root) if (root->list.entries) { for (size_t i = 0; i < root->list.entry_count; i++) - if (root->list.entries[i]) - fzl_freeast(root->list.entries[i]); + fzl_freeast(root->list.entries[i]); free(root->list.entries); } break; case FZL_NODETYPE_CALL: - if (root->call.callee) - fzl_freeast(root->call.callee); + fzl_freeast(root->call.callee); if (root->call.args_count) { for (size_t i = 0; i < root->call.args_count; i++) - if (root->call.args[i]) - fzl_freeast(root->call.args[i]); + fzl_freeast(root->call.args[i]); } if (root->call.args) free(root->call.args); break; case FZL_NODETYPE_CONDITIONAL: - if (root->cond.condition) - fzl_freeast(root->cond.condition); - - if (root->cond.true_block) - fzl_freeast(root->cond.true_block); - - if (root->cond.else_block) - fzl_freeast(root->cond.else_block); + fzl_freeast(root->cond.condition); + fzl_freeast(root->cond.true_block); + fzl_freeast(root->cond.else_block); break; case FZL_NODETYPE_LOOP: - if (root->loop.init) - fzl_freeast(root->loop.init); - - if (root->loop.cond) - fzl_freeast(root->loop.cond); - - if (root->loop.step) - fzl_freeast(root->loop.step); - - if (root->loop.body) - fzl_freeast(root->loop.body); + fzl_freeast(root->loop.init); + fzl_freeast(root->loop.cond); + fzl_freeast(root->loop.step); + fzl_freeast(root->loop.body); break; case FZL_NODETYPE_ADD: @@ -1459,17 +1455,14 @@ void fzl_freeast(fzl_node_t *root) case FZL_NODETYPE_GREATER: case FZL_NODETYPE_GREATER_EQUAL: case FZL_NODETYPE_ASSIGN: - if (root->binary_op.left) - fzl_freeast(root->binary_op.left); - if (root->binary_op.right) - fzl_freeast(root->binary_op.right); + fzl_freeast(root->binary_op.left); + fzl_freeast(root->binary_op.right); break; case FZL_NODETYPE_UNARY_PLUS: case FZL_NODETYPE_UNARY_MINUS: case FZL_NODETYPE_NOT: - if (root->unary_op.operand) - fzl_freeast(root->unary_op.operand); + fzl_freeast(root->unary_op.operand); break; case FZL_NODETYPE_FUNCTION: @@ -1481,8 +1474,7 @@ void fzl_freeast(fzl_node_t *root) break; case FZL_NODETYPE_RETURN: - if (root->ret.value) - fzl_freeast(root->ret.value); + fzl_freeast(root->ret.value); break; case FZL_NODETYPE_CLASS: @@ -1507,8 +1499,7 @@ void fzl_freeast(fzl_node_t *root) break; case FZL_NODETYPE_DECLARATION: - if (root->var.identifier) - fzl_freeast(root->var.identifier); + fzl_freeast(root->var.identifier); break; } diff --git a/tests/programs/neg_fmtstr_with_unknown_inside.fzs b/tests/programs/neg_fmtstr_with_unknown_inside.fzs new file mode 100644 index 0000000..95839a5 --- /dev/null +++ b/tests/programs/neg_fmtstr_with_unknown_inside.fzs @@ -0,0 +1,2 @@ +printline($"Something's on the {way}") + diff --git a/tests/programs/neg_unterminatd_fmtstr.fzs b/tests/programs/neg_unterminatd_fmtstr.fzs new file mode 100644 index 0000000..5181e3b --- /dev/null +++ b/tests/programs/neg_unterminatd_fmtstr.fzs @@ -0,0 +1 @@ +$"jopa {abc} diff --git a/tests/programs/pos_fmtstr.fzs b/tests/programs/pos_fmtstr.fzs new file mode 100644 index 0000000..daa4931 --- /dev/null +++ b/tests/programs/pos_fmtstr.fzs @@ -0,0 +1,6 @@ +int a1 = 2 * 2; +printline($"First tests: build string and print. 2 * 2 = {a1}"); + +str a2 = "Mike Tyson"; +printline($"Hello, {a2}"); +