fix: clear first AST node if got error inside parse_program

This commit is contained in:
samsonium
2026-10-28 19:37:17 +03:00
parent e11089c90f
commit d93c315359
4 changed files with 50 additions and 50 deletions
+41 -50
View File
@@ -1,5 +1,5 @@
#include "compiler/fzl_token.h"
#include <compiler/fzl_parser.h>
#include <compiler/fzl_token.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
@@ -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;
}
@@ -0,0 +1,2 @@
printline($"Something's on the {way}")
@@ -0,0 +1 @@
$"jopa {abc}
+6
View File
@@ -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}");