Basic resolving, codegen with packages
This commit is contained in:
96
ast.cpp
96
ast.cpp
@@ -17,6 +17,7 @@ enum Ast_Kind: U32{
|
||||
AST_CALL_ITEM,
|
||||
AST_CALL,
|
||||
|
||||
AST_TYPE,
|
||||
AST_VAR,
|
||||
AST_CONST,
|
||||
AST_POINTER,
|
||||
@@ -73,6 +74,7 @@ struct Value{VALUE_FIELDS};
|
||||
// BigInt big_int_val;
|
||||
|
||||
struct Ast_Atom: Ast_Expr{
|
||||
Ast_Decl *resolved_decl;
|
||||
INLINE_VALUE_FIELDS;
|
||||
};
|
||||
|
||||
@@ -91,7 +93,8 @@ struct Ast_Call: Ast_Expr{
|
||||
struct Ast_Unary: Ast_Expr{
|
||||
Token_Kind op;
|
||||
Ast_Expr *expr;
|
||||
U64 padding[3]; // For folding constants into atoms
|
||||
Ast_Resolved_Type *type;
|
||||
U64 padding[2]; // For folding constants into atoms
|
||||
};
|
||||
|
||||
struct Ast_Cast: Ast_Expr{
|
||||
@@ -116,19 +119,13 @@ struct Ast_Binary: Ast_Expr{
|
||||
// look into global scope and to the locals list.
|
||||
//
|
||||
|
||||
struct Ast_Block : Ast {
|
||||
// Stmts for global scope
|
||||
Array<Ast *> stmts;
|
||||
// Array<Ast_Named *> members;
|
||||
};
|
||||
|
||||
struct Ast_Return: Ast{
|
||||
Ast_Expr *expr;
|
||||
};
|
||||
|
||||
struct Ast_If_Node: Ast{
|
||||
Ast_Expr *expr ;
|
||||
Ast_Block *block;
|
||||
Ast_Scope *scope;
|
||||
Ast_Binary*init;
|
||||
};
|
||||
|
||||
@@ -142,20 +139,14 @@ 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;
|
||||
Ast_Expr *default_value;
|
||||
Ast_Scope *scope;
|
||||
};
|
||||
|
||||
struct Ast_Lambda : Ast_Expr {
|
||||
Array<Ast_Lambda_Arg *> args;
|
||||
Ast_Expr *ret;
|
||||
Ast_Block *block;
|
||||
B32 has_var_args;
|
||||
Array<Ast_Decl *> args;
|
||||
Ast_Expr *ret;
|
||||
Ast_Scope *scope;
|
||||
B32 has_var_args;
|
||||
};
|
||||
|
||||
struct Ast_Array: Ast_Expr{
|
||||
@@ -190,7 +181,6 @@ enum Ast_Decl_State{
|
||||
|
||||
struct Ast_Decl;
|
||||
struct Ast_Scope: Ast{
|
||||
Array<Ast_Decl *> resolved;
|
||||
Array<Ast_Decl *> decls;
|
||||
Array<Ast *> stmts;
|
||||
};
|
||||
@@ -201,8 +191,11 @@ struct Ast_Decl: Ast{
|
||||
Intern_String name;
|
||||
|
||||
Ast_Scope *scope;
|
||||
Ast_Expr *expr;
|
||||
Ast_Expr *typespec;
|
||||
union{
|
||||
Ast_Expr *expr;
|
||||
Ast_Lambda *lambda;
|
||||
};
|
||||
|
||||
INLINE_VALUE_FIELDS;
|
||||
};
|
||||
@@ -339,41 +332,21 @@ ast_expr_index(Token *pos, Ast_Expr *expr, Ast_Expr *index){
|
||||
}
|
||||
|
||||
function Ast_Lambda *
|
||||
ast_lambda(Token *pos, Array<Ast_Lambda_Arg *> params, B32 has_var_args, Ast_Expr *ret, Ast_Block *block){
|
||||
ast_lambda(Token *pos, Array<Ast_Decl *> params, B32 has_var_args, Ast_Expr *ret, Ast_Scope *scope){
|
||||
AST_NEW(Lambda, LAMBDA, pos, AST_EXPR);
|
||||
result->flags = AST_EXPR;
|
||||
result->args = params.tight_copy(pctx->perm);
|
||||
result->block = block;
|
||||
result->scope = scope;
|
||||
result->ret = ret;
|
||||
result->has_var_args = has_var_args;
|
||||
if(!ret) result->ret = ast_ident(result->pos, intern_void);
|
||||
|
||||
if(result->block) result->block->parent = result;
|
||||
if(result->scope) result->scope->parent = result;
|
||||
result->ret->parent = result;
|
||||
For(result->args) it->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Lambda_Arg *
|
||||
ast_expr_lambda_arg(Token *pos, Intern_String name, Ast_Expr *typespec, Ast_Expr *default_value){
|
||||
AST_NEW(Lambda_Arg, LAMBDA_ARG, pos, AST_EXPR);
|
||||
result->flags = AST_EXPR;
|
||||
result->name = name;
|
||||
result->typespec = typespec;
|
||||
result->default_value = default_value;
|
||||
result->typespec->parent = result;
|
||||
if(result->default_value) result->default_value->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Block *
|
||||
ast_block(Token *pos, Array<Ast *> stmts){
|
||||
AST_NEW(Block, BLOCK, pos, AST_STMT);
|
||||
result->stmts = stmts.tight_copy(pctx->perm);
|
||||
For(result->stmts) it->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_If *
|
||||
ast_if(Token *pos, Array<Ast_If_Node *> ifs){
|
||||
AST_NEW(If, IF, pos, AST_STMT);
|
||||
@@ -383,16 +356,16 @@ ast_if(Token *pos, Array<Ast_If_Node *> ifs){
|
||||
}
|
||||
|
||||
function Ast_For *
|
||||
ast_for(Token *pos, Ast_Expr *init, Ast_Expr *cond, Ast_Expr *iter, Ast_Block *block){
|
||||
ast_for(Token *pos, Ast_Expr *init, Ast_Expr *cond, Ast_Expr *iter, Ast_Scope *scope){
|
||||
AST_NEW(For, FOR, pos, AST_STMT);
|
||||
result->init = init;
|
||||
result->cond = cond;
|
||||
result->iter = iter;
|
||||
result->block = block;
|
||||
result->scope = scope;
|
||||
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;
|
||||
result->scope->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -414,15 +387,15 @@ ast_return(Token *pos, Ast_Expr *expr){
|
||||
}
|
||||
|
||||
function Ast_If_Node *
|
||||
ast_if_node(Token *pos, Ast_Expr *init, Ast_Expr *expr, Ast_Block *block){
|
||||
ast_if_node(Token *pos, Ast_Expr *init, Ast_Expr *expr, Ast_Scope *scope){
|
||||
AST_NEW(If_Node, IF_NODE, pos, AST_STMT);
|
||||
result->block = block;
|
||||
result->scope = scope;
|
||||
result->expr = expr;
|
||||
result->init = (Ast_Binary *)init;
|
||||
if(result->block) result->block->parent = result;
|
||||
if(result->scope) result->scope->parent = result;
|
||||
if(result->expr) result->expr->parent = result;
|
||||
if(result->init) {
|
||||
assert(init->kind == AST_BINARY);
|
||||
assert(init->kind == AST_VAR);
|
||||
result->init->parent = result;
|
||||
}
|
||||
return result;
|
||||
@@ -440,7 +413,6 @@ function Ast_Scope *
|
||||
ast_decl_scope(Token *pos, Array<Ast_Decl *> decls){
|
||||
AST_NEW(Scope, SCOPE, pos, AST_DECL);
|
||||
result->decls = decls.tight_copy(pctx->perm);
|
||||
result->resolved = array_make<Ast_Decl *>(pctx->perm, result->decls.len);
|
||||
For(result->decls){
|
||||
it->parent = result;
|
||||
}
|
||||
@@ -451,6 +423,7 @@ function Ast_Scope *
|
||||
ast_stmt_scope(Token *pos, Array<Ast *> stmts){
|
||||
AST_NEW(Scope, SCOPE, pos, AST_STMT);
|
||||
result->stmts = stmts.tight_copy(pctx->perm);
|
||||
result->decls = {pctx->heap};
|
||||
For(result->stmts){
|
||||
it->parent = result;
|
||||
}
|
||||
@@ -486,6 +459,14 @@ ast_var(Token *pos, Ast_Expr *typespec, Intern_String name, Ast_Expr *expr){
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Decl *
|
||||
ast_const(Token *pos, Intern_String name, Value value){
|
||||
AST_NEW(Decl, CONST, pos, AST_DECL);
|
||||
result->value = value;
|
||||
result->name = name;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Decl *
|
||||
ast_const(Token *pos, Intern_String name, Ast_Expr *expr){
|
||||
AST_NEW(Decl, CONST, pos, AST_DECL);
|
||||
@@ -495,13 +476,22 @@ ast_const(Token *pos, Intern_String name, Ast_Expr *expr){
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Decl *
|
||||
ast_type(Token *pos, Intern_String name, Ast_Resolved_Type *type){
|
||||
AST_NEW(Decl, CONST, pos, AST_DECL);
|
||||
result->type = type_type;
|
||||
result->type_val = type;
|
||||
result->name = name;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Package
|
||||
ast_package(Allocator *allocator, Intern_String name, Array<Ast_Decl *> decls){
|
||||
Ast_Package result = {};
|
||||
result.kind = AST_PACKAGE;
|
||||
result.decls = decls.copy(allocator);
|
||||
result.resolved = {allocator};
|
||||
result.name = name;
|
||||
For(result.decls) it->parent = &result;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user