Adding parent nodes to asts
This commit is contained in:
@@ -278,7 +278,7 @@ gen_ast(Ast *ast){
|
||||
gen("String %s = LIT(\"%s\");", node->name.str, sym->intern_val.str);
|
||||
}
|
||||
else if(sym->type == type_type){
|
||||
gen("typedef ");
|
||||
gen("// typedef ");
|
||||
gen_simple_decl(sym->type_val, node->name);
|
||||
gen(";");
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
|
||||
type :: int
|
||||
a_type :: int
|
||||
pointer_type :: *int
|
||||
null_pointer: pointer_type = null
|
||||
|
||||
@@ -31,3 +31,4 @@ returning_void :: (insert: int)
|
||||
return
|
||||
|
||||
|
||||
type :: a_type
|
||||
4
main.cpp
4
main.cpp
@@ -24,8 +24,8 @@ int main(){
|
||||
lex_test();
|
||||
|
||||
// String result = compile_file("globals.kl"_s);
|
||||
// String result = compile_file("lambdas.kl"_s);
|
||||
String result = compile_file("order_independent_globals.kl"_s);
|
||||
String result = compile_file("lambdas.kl"_s);
|
||||
// String result = compile_file("order_independent_globals.kl"_s);
|
||||
printf("%s", result.str);
|
||||
|
||||
// compile_file("lambdas.kl"_s);
|
||||
|
||||
45
new_ast.cpp
45
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;
|
||||
}
|
||||
|
||||
@@ -268,6 +276,8 @@ ast_expr_compound(Token *pos, Ast_Expr *typespec, Array<Ast_Compound_Item *> exp
|
||||
AST_NEW(Compound, COMPOUND, pos);
|
||||
result->typespec = typespec;
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -350,6 +367,9 @@ ast_if_node(Token *pos, Ast_Init *init, Ast_Expr *expr, Ast_Block *block){
|
||||
result->block = block;
|
||||
result->expr = expr;
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -378,45 +378,6 @@ parse_expr(S64 rbp){
|
||||
return left;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Parsing declarations
|
||||
//-----------------------------------------------------------------------------
|
||||
// [10]*int - Array of 10 pointers to ints
|
||||
// function Ast_Typespec *
|
||||
// parse_typespec_recurse(){
|
||||
// Token *token = token_get();
|
||||
// if(token_match(TK_Pointer)){
|
||||
// Ast_Typespec *result = parse_typespec_recurse();
|
||||
// result = ast_typespec_pointer(token, result);
|
||||
// return result;
|
||||
// }
|
||||
// else if(token_match(TK_OpenBracket)){
|
||||
// Ast_Expr *expr = parse_expr();
|
||||
// token_expect(TK_CloseBracket);
|
||||
// Ast_Typespec *result = parse_typespec_recurse();
|
||||
// result = ast_typespec_array(token, result, expr);
|
||||
// return result;
|
||||
// }
|
||||
// else if(token_match(TK_OpenParen)){
|
||||
// Ast_Lambda *result = parse_lambda(token, true);
|
||||
// return ast_typespec_lambda(token, result);
|
||||
// }
|
||||
// else if(token_match(TK_Identifier)){
|
||||
// Ast_Typespec *result = ast_typespec_name(token, token->intern_val);
|
||||
// return result;
|
||||
// }
|
||||
// else{
|
||||
// parsing_error(token, "Failed to parse type, unexpected token of kind", token_kind_string(token->kind).str);
|
||||
// return 0;
|
||||
// }
|
||||
// }
|
||||
|
||||
// function Ast_Typespec *
|
||||
// parse_typespec(){
|
||||
// Ast_Typespec *result = parse_typespec_recurse();
|
||||
// return result;
|
||||
// }
|
||||
|
||||
function Ast_Expr *
|
||||
parse_assign_expr(){
|
||||
Ast_Expr *result = 0;
|
||||
|
||||
@@ -39,8 +39,6 @@ struct Operand{
|
||||
|
||||
global Ast_Named empty_decl = {};
|
||||
|
||||
// @todo: currently inserting into sym table type of [Type] instead of what type represents
|
||||
|
||||
function void
|
||||
sym_insert(Sym *sym){
|
||||
U64 hash = hash_string(sym->name.s);
|
||||
@@ -229,7 +227,10 @@ eval_stmt(Ast *ast, Ast_Resolved_Type *ret){
|
||||
}
|
||||
}
|
||||
|
||||
enum{AST_CAN_BE_NULL = 1};
|
||||
enum{
|
||||
AST_CAN_BE_NULL = 1
|
||||
};
|
||||
|
||||
function Ast_Resolved_Type *
|
||||
eval_typespec(Ast_Expr *ast, B32 ast_can_be_null){
|
||||
if(ast_can_be_null && ast == 0) return 0;
|
||||
|
||||
Reference in New Issue
Block a user