62 lines
1.5 KiB
C
62 lines
1.5 KiB
C
#include <compiler/fzl_parser.h>
|
|
#include <compiler/internals/parser.h>
|
|
#include <stdio.h>
|
|
|
|
// Parse if statement
|
|
fzl_node_t *pa_if(fzl_parser_t *parser)
|
|
{
|
|
if (!parser)
|
|
return NULL;
|
|
|
|
fzl_token_t *if_tok = consume(parser);
|
|
fzl_token_t *lparen = consume(parser);
|
|
if (lparen->type != FZL_TOKTYPE_LPAREN)
|
|
{
|
|
snprintf(plast_err, sizeof(plast_err),
|
|
"'%s': expected '(' before condition at %ld:%ld", lparen->filename,
|
|
lparen->ln, lparen->cn);
|
|
return NULL;
|
|
}
|
|
|
|
fzl_node_t *condition = pa_expression(parser);
|
|
if (!condition)
|
|
return NULL;
|
|
|
|
fzl_token_t *rparen = consume(parser);
|
|
if (rparen->type != FZL_TOKTYPE_RPAREN)
|
|
{
|
|
fzl_freeast(condition);
|
|
snprintf(plast_err, sizeof(plast_err),
|
|
"'%s': expected ')' after condition at %ld:%ld", rparen->filename,
|
|
rparen->ln, rparen->cn);
|
|
return NULL;
|
|
}
|
|
|
|
fzl_node_t *else_block = NULL;
|
|
fzl_node_t *true_block = pa_statement(parser);
|
|
if (!true_block)
|
|
{
|
|
fzl_freeast(condition);
|
|
return NULL;
|
|
}
|
|
|
|
if (is(parser, FZL_TOKTYPE_ELSE))
|
|
{
|
|
consume(parser); // else
|
|
else_block = pa_statement(parser);
|
|
if (!else_block)
|
|
{
|
|
fzl_freeast(condition);
|
|
fzl_freeast(true_block);
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
fzl_node_t *stmt_node = fzl_mknode(FZL_NODETYPE_CONDITIONAL, if_tok);
|
|
stmt_node->cond.condition = condition;
|
|
stmt_node->cond.true_block = true_block;
|
|
stmt_node->cond.else_block = else_block;
|
|
|
|
return stmt_node;
|
|
}
|