Adding parent nodes to asts
This commit is contained in:
49
new_ast.cpp
49
new_ast.cpp
@@ -114,6 +114,7 @@ struct Ast{
|
||||
Token *pos;
|
||||
|
||||
Ast_Kind kind;
|
||||
Ast *parent;
|
||||
// @todo?
|
||||
// bool is_atom: 1;
|
||||
// bool is_stmt: 1;
|
||||
@@ -189,6 +190,11 @@ struct Ast_If: Ast{
|
||||
Array<Ast_If_Node *> ifs;
|
||||
};
|
||||
|
||||
struct Ast_Enum : Ast{
|
||||
Ast_Expr *typespec;
|
||||
Array<Ast_Init *> children;
|
||||
};
|
||||
|
||||
struct Ast_Lambda_Arg: Ast_Expr{
|
||||
Intern_String name;
|
||||
Ast_Expr *typespec;
|
||||
@@ -260,6 +266,8 @@ ast_expr_binary(Ast_Expr *left, Ast_Expr *right, Token *op){
|
||||
result->op = op->kind;
|
||||
result->left = left;
|
||||
result->right = right;
|
||||
result->left->parent = result;
|
||||
result->right->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -267,7 +275,9 @@ function Ast_Compound *
|
||||
ast_expr_compound(Token *pos, Ast_Expr *typespec, Array<Ast_Compound_Item *> exprs){
|
||||
AST_NEW(Compound, COMPOUND, pos);
|
||||
result->typespec = typespec;
|
||||
result->exprs = exprs.tight_copy(pctx->perm);
|
||||
result->exprs = exprs.tight_copy(pctx->perm);
|
||||
result->typespec->parent = result;
|
||||
For(result->exprs) it[0]->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -277,6 +287,9 @@ ast_expr_compound_item(Token *pos, Ast_Expr *index, Ast_Atom *name, Ast_Expr *it
|
||||
result->name = name;
|
||||
result->index = index;
|
||||
result->item = item;
|
||||
name->parent = result;
|
||||
index->parent = result;
|
||||
item->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -285,6 +298,8 @@ ast_expr_cast(Token *pos, Ast_Expr *expr, Ast_Expr *typespec){
|
||||
AST_NEW(Cast, CAST, pos);
|
||||
result->expr = expr;
|
||||
result->typespec = typespec;
|
||||
expr->parent = result;
|
||||
typespec->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -293,6 +308,7 @@ ast_expr_unary(Token *pos, Token_Kind op, Ast_Expr *expr){
|
||||
AST_NEW(Unary, UNARY, pos);
|
||||
result->expr = expr;
|
||||
result->op = op;
|
||||
expr->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -301,6 +317,8 @@ ast_expr_index(Token *pos, Ast_Expr *expr, Ast_Expr *index){
|
||||
AST_NEW(Index, INDEX, pos);
|
||||
result->expr = expr;
|
||||
result->index = index;
|
||||
expr->parent = result;
|
||||
index->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -308,17 +326,13 @@ function Ast_Lambda *
|
||||
ast_lambda(Token *pos, Array<Ast_Lambda_Arg *> params, Ast_Expr *ret, Ast_Block *block){
|
||||
AST_NEW(Lambda, LAMBDA, pos);
|
||||
result->args = params.tight_copy(pctx->perm);
|
||||
result->ret = ret;
|
||||
result->block = block;
|
||||
if(!ret){
|
||||
result->ret = ast_ident(0, intern_void);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
result->ret = ret;
|
||||
if(!ret) result->ret = ast_ident(result->pos, intern_void);
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_lambda_empty(Token *pos){
|
||||
AST_NEW(Expr, LAMBDA, pos);
|
||||
result->block->parent = result;
|
||||
result->ret->parent = result;
|
||||
For(result->args) it[0]->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -327,6 +341,7 @@ ast_expr_lambda_arg(Token *pos, Intern_String name, Ast_Expr *typespec){
|
||||
AST_NEW(Lambda_Arg, LAMBDA_ARG, pos);
|
||||
result->name = name;
|
||||
result->typespec = typespec;
|
||||
result->typespec->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -334,6 +349,7 @@ function Ast_Block *
|
||||
ast_block(Token *pos, Array<Ast *> stmts){
|
||||
AST_NEW(Block, BLOCK, pos);
|
||||
result->stmts = stmts.tight_copy(pctx->perm);
|
||||
For(result->stmts) it[0]->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -341,6 +357,7 @@ function Ast_If *
|
||||
ast_if(Token *pos, Array<Ast_If_Node *> ifs){
|
||||
AST_NEW(If, IF, pos);
|
||||
result->ifs = ifs.tight_copy(pctx->perm);
|
||||
For(result->ifs) it[0]->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -349,7 +366,10 @@ ast_if_node(Token *pos, Ast_Init *init, Ast_Expr *expr, Ast_Block *block){
|
||||
AST_NEW(If_Node, IF_NODE, pos);
|
||||
result->block = block;
|
||||
result->expr = expr;
|
||||
result->init = init;
|
||||
result->init = init;
|
||||
if(result->block) result->block->parent = result;
|
||||
if(result->expr) result->expr->parent = result;
|
||||
if(result->init) result->init->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -359,6 +379,8 @@ ast_init(Token *pos, Token_Kind op, Ast_Atom *ident, Ast_Expr *expr){
|
||||
result->op = op;
|
||||
result->ident = ident;
|
||||
result->expr = expr;
|
||||
result->ident->parent = result;
|
||||
result->expr->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -366,6 +388,7 @@ function Ast_Array *
|
||||
ast_array(Token *pos, Ast_Expr *expr){
|
||||
AST_NEW(Array, ARRAY, pos);
|
||||
result->expr = expr;
|
||||
result->expr->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -378,6 +401,8 @@ ast_var(Token *pos, Ast_Expr *typespec, Intern_String name, Ast_Expr *expr){
|
||||
result->expr = expr;
|
||||
result->typespec = typespec;
|
||||
result->name = name;
|
||||
if(result->expr) result->expr->parent = result;
|
||||
if(result->typespec) result->typespec->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -386,6 +411,7 @@ ast_const(Token *pos, Intern_String name, Ast_Expr *expr){
|
||||
AST_NEW(Const, CONST, pos);
|
||||
result->expr = expr;
|
||||
result->name = name;
|
||||
result->expr->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -395,5 +421,6 @@ ast_package(Token *pos, String name, Array<Ast_Named *> decls){
|
||||
result->decls = decls.tight_copy(pctx->perm);
|
||||
result->ordered = array_make<Ast_Named *>(pctx->perm, decls.len);
|
||||
result->name = intern_string(&pctx->interns, name);
|
||||
For(result->decls) it[0]->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user