Parsing for stmt

This commit is contained in:
Krzosa Karol
2022-05-31 16:45:20 +02:00
parent 41697dec80
commit 20accf8293
4 changed files with 75 additions and 13 deletions

View File

@@ -93,7 +93,7 @@ enum Ast_Kind: U32{
AST_POINTER,
AST_ARRAY,
AST_INIT,
AST_FOR,
AST_IF,
AST_IF_NODE,
AST_RETURN,
@@ -188,6 +188,15 @@ struct Ast_If: Ast{
Array<Ast_If_Node *> ifs;
};
struct Ast_Pass: Ast{}; // @todo
struct Ast_For: Ast{
Ast_Expr *init;
Ast_Expr *cond;
Ast_Expr *iter;
Ast_Block *block;
};
struct Ast_Lambda_Arg: Ast_Expr{
Intern_String name;
Ast_Expr *typespec;
@@ -387,6 +396,20 @@ ast_if(Token *pos, Array<Ast_If_Node *> ifs){
return result;
}
function Ast_For *
ast_for(Token *pos, Ast_Expr *init, Ast_Expr *cond, Ast_Expr *iter, Ast_Block *block){
AST_NEW(For, FOR, pos, AST_STMT);
result->init = init;
result->cond = cond;
result->iter = iter;
result->block = block;
if(result->init) result->init->parent = result;
if(result->cond) result->cond->parent = result;
if(result->iter) result->iter->parent = result;
result->block->parent = result;
return result;
}
function Ast_Return *
ast_return(Token *pos, Ast_Expr *expr){
AST_NEW(Return, RETURN, pos, AST_STMT);
@@ -399,14 +422,17 @@ ast_return(Token *pos, Ast_Expr *expr){
}
function Ast_If_Node *
ast_if_node(Token *pos, Ast_Binary *init, Ast_Expr *expr, Ast_Block *block){
ast_if_node(Token *pos, Ast_Expr *init, Ast_Expr *expr, Ast_Block *block){
AST_NEW(If_Node, IF_NODE, pos, AST_STMT);
result->block = block;
result->expr = expr;
result->init = init;
result->init = (Ast_Binary *)init;
if(result->block) result->block->parent = result;
if(result->expr) result->expr->parent = result;
if(result->init) result->init->parent = result;
if(result->init) {
assert(init->kind == AST_BINARY);
result->init->parent = result;
}
return result;
}