Skip to content

Commit def4b8e

Browse files
Liviarodrigues1BeyondMagic
authored andcommitted
feat(ast): adicionar suporte a if/else na AST
1 parent 1ea0f4f commit def4b8e

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/ast.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@ void ast_stmt_destroy(AstStmt *stmt)
270270
ast_stmt_destroy(stmt->data.for_stmt.post);
271271
ast_stmt_destroy(stmt->data.for_stmt.body);
272272
break;
273+
case STMT_IF:
274+
ast_expr_destroy(stmt->data.if_stmt.condition);
275+
ast_stmt_destroy(stmt->data.if_stmt.then_branch);
276+
if (stmt->data.if_stmt.else_branch)
277+
ast_stmt_destroy(stmt->data.if_stmt.else_branch);
278+
break;
273279
case STMT_EXPR:
274280
case STMT_RETURN:
275281
if (stmt->data.expr)
@@ -377,6 +383,16 @@ AstStmt *ast_stmt_make_for(AstStmt *init, AstExpr *condition, AstStmt *post, Ast
377383
return stmt;
378384
}
379385

386+
AstStmt *ast_stmt_make_if(AstExpr *condition, AstStmt *then_branch, AstStmt *else_branch)
387+
{
388+
AstStmt *stmt = xcalloc(1, sizeof(AstStmt));
389+
stmt->kind = STMT_IF;
390+
stmt->data.if_stmt.condition = condition;
391+
stmt->data.if_stmt.then_branch = then_branch;
392+
stmt->data.if_stmt.else_branch = else_branch;
393+
return stmt;
394+
}
395+
380396
AstStmt *ast_stmt_make_expr(AstExpr *expr)
381397
{
382398
AstStmt *stmt = xcalloc(1, sizeof(AstStmt));

src/ast.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ typedef struct AstStmt
145145
STMT_ARRAY_ASSIGN,
146146
STMT_WHILE,
147147
STMT_FOR,
148+
STMT_IF,
148149
STMT_EXPR,
149150
STMT_RETURN
150151
} kind;
@@ -188,6 +189,14 @@ typedef struct AstStmt
188189
struct AstStmt *post;
189190
struct AstStmt *body;
190191
} for_stmt;
192+
193+
struct
194+
{
195+
AstExpr *condition;
196+
struct AstStmt *then_branch;
197+
struct AstStmt *else_branch;
198+
} if_stmt;
199+
191200
AstExpr *expr;
192201
} data;
193202
} AstStmt;
@@ -234,6 +243,7 @@ AstStmt *ast_stmt_make_array_decl(TypeKind type, char *name, size_t size, AstExp
234243
AstStmt *ast_stmt_make_array_assign(char *name, AstExpr *index, AstExpr *value);
235244
AstStmt *ast_stmt_make_while(AstExpr *condition, AstStmt *body);
236245
AstStmt *ast_stmt_make_for(AstStmt *init, AstExpr *condition, AstStmt *post, AstStmt *body);
246+
AstStmt *ast_stmt_make_if(AstExpr *condition, AstStmt *then_branch, AstStmt *else_branch);
237247
AstStmt *ast_stmt_make_expr(AstExpr *expr);
238248
AstStmt *ast_stmt_make_return(AstExpr *expr);
239249
AstExpr *ast_expr_make_array_literal(AstExprList *elements);

0 commit comments

Comments
 (0)