Skip to content

Commit a67e855

Browse files
Liviarodrigues1BeyondMagic
authored andcommitted
feat(semantic): validar condições e retornos em if/else
1 parent 79838e5 commit a67e855

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/semantic.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,37 @@ static int analyze_statement(SemanticInfo *info, AstFunction *fn, SymbolTable *s
362362
}
363363
break;
364364
}
365+
366+
case STMT_IF:
367+
{
368+
TypeKind cond_type = analyze_expression(info, symbols, stmt->data.if_stmt.condition);
369+
stmt->data.if_stmt.condition->type = cond_type;
370+
371+
if (!is_boolean_like(cond_type))
372+
{
373+
semantic_error("if condition in function '%s' must be boolean-compatible but found %s",
374+
fn->name,
375+
ast_type_name(cond_type));
376+
return 0;
377+
}
378+
379+
int then_has_return = 0;
380+
if (!analyze_statement(info, fn, symbols, stmt->data.if_stmt.then_branch, &then_has_return))
381+
return 0;
382+
383+
int else_has_return = 0;
384+
if (stmt->data.if_stmt.else_branch)
385+
{
386+
if (!analyze_statement(info, fn, symbols, stmt->data.if_stmt.else_branch, &else_has_return))
387+
return 0;
388+
}
389+
390+
if (then_has_return && stmt->data.if_stmt.else_branch && else_has_return)
391+
*has_return = 1;
392+
393+
break;
394+
}
395+
365396
case STMT_EXPR:
366397
if (stmt->data.expr)
367398
{

0 commit comments

Comments
 (0)