Adding struts
This commit is contained in:
74
new_ast.cpp
74
new_ast.cpp
@@ -76,7 +76,7 @@ thread_local Parse_Ctx *pctx;
|
||||
//-----------------------------------------------------------------------------
|
||||
// AST
|
||||
//-----------------------------------------------------------------------------
|
||||
enum Ast_Kind{
|
||||
enum Ast_Kind: U32{
|
||||
AST_NONE,
|
||||
|
||||
AST_PACKAGE,
|
||||
@@ -108,6 +108,9 @@ enum Ast_Kind{
|
||||
typedef U32 Ast_Flag;
|
||||
enum{
|
||||
AST_EXPR = 1,
|
||||
AST_STMT = 2,
|
||||
AST_BINDING = 4,
|
||||
AST_AGGREGATE = 8,
|
||||
};
|
||||
|
||||
struct Ast{
|
||||
@@ -213,10 +216,9 @@ struct Ast_Array: Ast_Expr{
|
||||
Ast_Expr *expr;
|
||||
};
|
||||
|
||||
struct Ast_Struct: Ast{
|
||||
struct Ast_Struct: Ast_Expr{
|
||||
// Required to be Ast_Struct or Ast_Var or Ast_Const
|
||||
Array<Ast *> members;
|
||||
|
||||
};
|
||||
|
||||
struct Ast_Named:Ast{
|
||||
@@ -229,7 +231,7 @@ struct Ast_Var: Ast_Named{
|
||||
};
|
||||
|
||||
struct Ast_Const: Ast_Named{
|
||||
Ast *value;
|
||||
Ast_Expr *value;
|
||||
};
|
||||
|
||||
struct Ast_Package:Ast{
|
||||
@@ -241,40 +243,37 @@ struct Ast_Package:Ast{
|
||||
//-----------------------------------------------------------------------------
|
||||
// AST Constructors beginning with expressions
|
||||
//-----------------------------------------------------------------------------
|
||||
#define AST_NEW(T,ikind,ipos) \
|
||||
#define AST_NEW(T,ikind,ipos,iflags) \
|
||||
Ast_##T *result = exp_alloc_type(pctx->perm, Ast_##T, AF_ZeroMemory);\
|
||||
result->flags = iflags; \
|
||||
result->kind = AST_##ikind; \
|
||||
result->pos = ipos; \
|
||||
result->id = ++pctx->unique_ids
|
||||
|
||||
function Ast_Atom *
|
||||
ast_str(Token *pos, Intern_String string, Ast_Flag flags = 0){
|
||||
AST_NEW(Atom, STR, pos);
|
||||
result->flags = flags;
|
||||
AST_NEW(Atom, STR, pos,flags);
|
||||
result->intern_val = string;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Atom *
|
||||
ast_ident(Token *pos, Intern_String string, Ast_Flag flags = 0){
|
||||
AST_NEW(Atom, IDENT, pos);
|
||||
result->flags = flags;
|
||||
AST_NEW(Atom, IDENT, pos,flags);
|
||||
result->intern_val = string;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Atom *
|
||||
ast_int(Token *pos, S64 integer, Ast_Flag flags){
|
||||
AST_NEW(Atom, INT, pos);
|
||||
result->flags = flags;
|
||||
ast_int(Token *pos, S64 integer, Ast_Flag flags = 0){
|
||||
AST_NEW(Atom, INT, pos,flags);
|
||||
result->int_val = integer;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_binary(Ast_Expr *left, Ast_Expr *right, Token *op){
|
||||
AST_NEW(Binary, BINARY, op);
|
||||
result->flags = AST_EXPR;
|
||||
AST_NEW(Binary, BINARY, op, AST_EXPR);
|
||||
result->op = op->kind;
|
||||
result->left = left;
|
||||
result->right = right;
|
||||
@@ -285,8 +284,7 @@ ast_expr_binary(Ast_Expr *left, Ast_Expr *right, Token *op){
|
||||
|
||||
function Ast_Compound *
|
||||
ast_expr_compound(Token *pos, Ast_Expr *typespec, Array<Ast_Compound_Item *> exprs){
|
||||
AST_NEW(Compound, COMPOUND, pos);
|
||||
result->flags = AST_EXPR;
|
||||
AST_NEW(Compound, COMPOUND, pos, AST_EXPR);
|
||||
result->typespec = typespec;
|
||||
result->exprs = exprs.tight_copy(pctx->perm);
|
||||
if(result->typespec) result->typespec->parent = result;
|
||||
@@ -296,8 +294,7 @@ ast_expr_compound(Token *pos, Ast_Expr *typespec, Array<Ast_Compound_Item *> exp
|
||||
|
||||
function Ast_Compound_Item *
|
||||
ast_expr_compound_item(Token *pos, Ast_Expr *index, Ast_Atom *name, Ast_Expr *item){
|
||||
AST_NEW(Compound_Item, COMPOUND_ITEM, pos);
|
||||
result->flags = AST_EXPR;
|
||||
AST_NEW(Compound_Item, COMPOUND_ITEM, pos, AST_EXPR);
|
||||
result->name = name;
|
||||
result->index = index;
|
||||
result->item = item;
|
||||
@@ -309,7 +306,7 @@ ast_expr_compound_item(Token *pos, Ast_Expr *index, Ast_Atom *name, Ast_Expr *it
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_cast(Token *pos, Ast_Expr *expr, Ast_Expr *typespec){
|
||||
AST_NEW(Cast, CAST, pos);
|
||||
AST_NEW(Cast, CAST, pos, AST_EXPR);
|
||||
result->flags = AST_EXPR;
|
||||
result->expr = expr;
|
||||
result->typespec = typespec;
|
||||
@@ -320,7 +317,7 @@ ast_expr_cast(Token *pos, Ast_Expr *expr, Ast_Expr *typespec){
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_unary(Token *pos, Token_Kind op, Ast_Expr *expr){
|
||||
AST_NEW(Unary, UNARY, pos);
|
||||
AST_NEW(Unary, UNARY, pos, AST_EXPR);
|
||||
result->flags = AST_EXPR;
|
||||
result->expr = expr;
|
||||
result->op = op;
|
||||
@@ -330,7 +327,7 @@ ast_expr_unary(Token *pos, Token_Kind op, Ast_Expr *expr){
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_index(Token *pos, Ast_Expr *expr, Ast_Expr *index){
|
||||
AST_NEW(Index, INDEX, pos);
|
||||
AST_NEW(Index, INDEX, pos, AST_EXPR);
|
||||
result->flags = AST_EXPR;
|
||||
result->expr = expr;
|
||||
result->index = index;
|
||||
@@ -341,7 +338,7 @@ ast_expr_index(Token *pos, Ast_Expr *expr, Ast_Expr *index){
|
||||
|
||||
function Ast_Lambda *
|
||||
ast_lambda(Token *pos, Array<Ast_Lambda_Arg *> params, Ast_Expr *ret, Ast_Block *block){
|
||||
AST_NEW(Lambda, LAMBDA, pos);
|
||||
AST_NEW(Lambda, LAMBDA, pos, AST_EXPR);
|
||||
result->flags = AST_EXPR;
|
||||
result->args = params.tight_copy(pctx->perm);
|
||||
result->block = block;
|
||||
@@ -356,7 +353,7 @@ ast_lambda(Token *pos, Array<Ast_Lambda_Arg *> params, Ast_Expr *ret, Ast_Block
|
||||
|
||||
function Ast_Lambda_Arg *
|
||||
ast_expr_lambda_arg(Token *pos, Intern_String name, Ast_Expr *typespec){
|
||||
AST_NEW(Lambda_Arg, LAMBDA_ARG, pos);
|
||||
AST_NEW(Lambda_Arg, LAMBDA_ARG, pos, AST_EXPR);
|
||||
result->flags = AST_EXPR;
|
||||
result->name = name;
|
||||
result->typespec = typespec;
|
||||
@@ -366,7 +363,7 @@ ast_expr_lambda_arg(Token *pos, Intern_String name, Ast_Expr *typespec){
|
||||
|
||||
function Ast_Block *
|
||||
ast_block(Token *pos, Array<Ast *> stmts){
|
||||
AST_NEW(Block, BLOCK, pos);
|
||||
AST_NEW(Block, BLOCK, pos, AST_STMT);
|
||||
result->stmts = stmts.tight_copy(pctx->perm);
|
||||
For(result->stmts) it[0]->parent = result;
|
||||
return result;
|
||||
@@ -374,15 +371,26 @@ ast_block(Token *pos, Array<Ast *> stmts){
|
||||
|
||||
function Ast_If *
|
||||
ast_if(Token *pos, Array<Ast_If_Node *> ifs){
|
||||
AST_NEW(If, IF, pos);
|
||||
AST_NEW(If, IF, pos, AST_STMT);
|
||||
result->ifs = ifs.tight_copy(pctx->perm);
|
||||
For(result->ifs) it[0]->parent = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Return *
|
||||
ast_return(Token *pos, Ast_Expr *expr){
|
||||
AST_NEW(Return, RETURN, pos, AST_STMT);
|
||||
if(expr){
|
||||
assert(is_flag_set(expr->flags, AST_EXPR));
|
||||
result->expr = expr;
|
||||
result->expr->parent = result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_If_Node *
|
||||
ast_if_node(Token *pos, Ast_Init *init, Ast_Expr *expr, Ast_Block *block){
|
||||
AST_NEW(If_Node, IF_NODE, pos);
|
||||
AST_NEW(If_Node, IF_NODE, pos, AST_STMT);
|
||||
result->block = block;
|
||||
result->expr = expr;
|
||||
result->init = init;
|
||||
@@ -394,7 +402,7 @@ ast_if_node(Token *pos, Ast_Init *init, Ast_Expr *expr, Ast_Block *block){
|
||||
|
||||
function Ast_Init *
|
||||
ast_init(Token *pos, Token_Kind op, Ast_Atom *ident, Ast_Expr *expr){
|
||||
AST_NEW(Init, INIT, pos);
|
||||
AST_NEW(Init, INIT, pos, AST_STMT);
|
||||
result->op = op;
|
||||
result->ident = ident;
|
||||
result->expr = expr;
|
||||
@@ -405,7 +413,7 @@ ast_init(Token *pos, Token_Kind op, Ast_Atom *ident, Ast_Expr *expr){
|
||||
|
||||
function Ast_Array *
|
||||
ast_array(Token *pos, Ast_Expr *expr){
|
||||
AST_NEW(Array, ARRAY, pos);
|
||||
AST_NEW(Array, ARRAY, pos, AST_EXPR);
|
||||
result->flags = AST_EXPR;
|
||||
result->expr = expr;
|
||||
result->expr->parent = result;
|
||||
@@ -414,7 +422,7 @@ ast_array(Token *pos, Ast_Expr *expr){
|
||||
|
||||
function Ast_Struct *
|
||||
ast_struct(Token *pos, Array<Ast *> members){
|
||||
AST_NEW(Struct, STRUCT, pos);
|
||||
AST_NEW(Struct, STRUCT, pos, AST_AGGREGATE);
|
||||
result->members = members.tight_copy(pctx->perm);
|
||||
For(result->members) {
|
||||
assert(it[0]->kind == AST_VAR || it[0]->kind == AST_CONST || it[0]->kind == AST_STRUCT);
|
||||
@@ -428,7 +436,7 @@ ast_struct(Token *pos, Array<Ast *> members){
|
||||
//-----------------------------------------------------------------------------
|
||||
function Ast_Var *
|
||||
ast_var(Token *pos, Ast_Expr *typespec, Intern_String name, Ast_Expr *expr){
|
||||
AST_NEW(Var, VAR, pos);
|
||||
AST_NEW(Var, VAR, pos, AST_BINDING);
|
||||
result->expr = expr;
|
||||
result->typespec = typespec;
|
||||
result->name = name;
|
||||
@@ -438,8 +446,8 @@ ast_var(Token *pos, Ast_Expr *typespec, Intern_String name, Ast_Expr *expr){
|
||||
}
|
||||
|
||||
function Ast_Const *
|
||||
ast_const(Token *pos, Intern_String name, Ast *value){
|
||||
AST_NEW(Const, CONST, pos);
|
||||
ast_const(Token *pos, Intern_String name, Ast_Expr *value){
|
||||
AST_NEW(Const, CONST, pos, AST_BINDING);
|
||||
|
||||
assert(value->kind == AST_STRUCT || is_flag_set(value->flags, AST_EXPR));
|
||||
result->value = value;
|
||||
@@ -450,7 +458,7 @@ ast_const(Token *pos, Intern_String name, Ast *value){
|
||||
|
||||
function Ast_Package *
|
||||
ast_package(Token *pos, String name, Array<Ast_Named *> decls){
|
||||
AST_NEW(Package, PACKAGE, pos);
|
||||
AST_NEW(Package, PACKAGE, pos, 0);
|
||||
result->decls = decls.tight_copy(pctx->perm);
|
||||
result->ordered = array_make<Ast_Named *>(pctx->perm, decls.len);
|
||||
result->name = intern_string(&pctx->interns, name);
|
||||
|
||||
Reference in New Issue
Block a user