Skip to content

Commit

Permalink
🎨 ast cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
fennecdjay committed Jan 30, 2024
1 parent 9250ae0 commit 0ba7b3b
Show file tree
Hide file tree
Showing 10 changed files with 1,619 additions and 1,747 deletions.
102 changes: 47 additions & 55 deletions include/absyn.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,15 @@ typedef MP_Vector *ID_List;
typedef MP_Vector *Capture_List;
typedef struct Fptr_Def_ * Fptr_Def;
typedef MP_Vector * Arg_List;
typedef MP_Vector *Type_List;
typedef MP_Vector *TmplArg_List;
typedef struct Type_Decl_ Type_Decl;

typedef struct Tag {
Symbol sym;
loc_t loc;
} Tag;
#define MK_TAG(a, b) (Tag){ .sym = (a), .loc = (b) }

typedef struct Type_Decl_ Type_Decl;
struct Type_Decl_ {
Tag tag;
Array_Sub array;
Type_List types;
Type_Decl *next;
Fptr_Def fptr;
uint8_t option;
ae_flag flag;
bool ref;
};
ANEW ANN AST_NEW(Type_Decl *, type_decl, const Symbol, const loc_t pos);
ANN void free_type_decl(MemPool p, Type_Decl *);

/** variable declaration **/
typedef struct Var_Decl_ {
Tag tag;
Expand All @@ -57,6 +44,19 @@ typedef struct Variable_ {
typedef MP_Vector *Variable_List;
ANN void free_variable_list(MemPool p, Variable_List);

struct Type_Decl_ {
Tag tag;
Array_Sub array;
TmplArg_List types;
Type_Decl *next;
Fptr_Def fptr;
uint8_t option;
ae_flag flag;
bool ref;
};
ANEW ANN AST_NEW(Type_Decl *, type_decl, const Symbol, const loc_t pos);
ANN void free_type_decl(MemPool p, Type_Decl *);

enum tmplarg_t {
tmplarg_td,
tmplarg_exp
Expand All @@ -70,7 +70,7 @@ typedef struct TmplArg {
enum tmplarg_t type;
} TmplArg;

ANN static inline uint32_t tmplarg_ntypes(Type_List tl) {
ANN static inline uint32_t tmplarg_ntypes(TmplArg_List tl) {
uint32_t ret = 0;
for(uint32_t i = 0; i < tl->len; i++) {
TmplArg *ta = mp_vector_at(tl, TmplArg, i);
Expand Down Expand Up @@ -110,10 +110,8 @@ typedef struct {
Symbol xid;
} Exp_Dot;


typedef struct Capture {
Tag tag;
struct Value_ *orig;
Var_Decl var;
struct Value_ *temp;
uint32_t offset;
bool is_ref;
Expand Down Expand Up @@ -174,7 +172,7 @@ typedef struct Specialized {
typedef MP_Vector *Specialized_List;
ANN void free_specialized_list(MemPool p, Specialized_List);

ANN void free_type_list(MemPool p, Type_List);
ANN void free_tmplarg_list(MemPool p, TmplArg_List);

ANN void free_arg_list(MemPool p, Arg_List);

Expand Down Expand Up @@ -258,11 +256,11 @@ typedef struct {

typedef struct Tmpl_ {
Specialized_List list;
Type_List call;
TmplArg_List call;
} Tmpl;

ANN ANEW AST_NEW(Tmpl *, tmpl, const Specialized_List);
ANN ANEW AST_NEW(Tmpl *, tmpl_call, Type_List);
ANN ANEW AST_NEW(Tmpl *, tmpl_call, TmplArg_List);
ANN void free_tmpl(MemPool p, Tmpl *);

static inline bool tmpl_base(const Tmpl *a) {
Expand Down Expand Up @@ -482,44 +480,39 @@ typedef enum {
ae_stmt_spread
} ae_stmt_t;

typedef struct Stmt_Exp_ * Stmt_Exp;
typedef struct Stmt_Code_ * Stmt_Code;
typedef struct Stmt_For_ * Stmt_For;
typedef struct Stmt_Flow_ * Stmt_Flow;
typedef struct Stmt_Each_ * Stmt_Each;
typedef struct Stmt_Loop_ * Stmt_Loop;
typedef struct Stmt_If_ * Stmt_If;
typedef struct Stmt_Match_ * Stmt_Match;
typedef struct Stmt_Index_ * Stmt_Index;
typedef struct Stmt_PP_ * Stmt_PP;
typedef struct Stmt_Defer_ * Stmt_Defer;

struct Stmt_Exp_ {
typedef struct Stmt_Exp_ {
Exp val;
};
} *Stmt_Exp;

struct Stmt_Index_ {
typedef struct Stmt_Index_ {
m_int idx;
};
} *Stmt_Index;

struct Stmt_Flow_ {
typedef struct Stmt_Flow_ {
Exp cond;
Stmt body;
bool is_do;
};
} *Stmt_Flow;

struct Stmt_Match_ {
typedef struct Stmt_Match_ {
Exp cond;
Stmt_List list;
union {
Stmt where;
Exp when;
};
};
} *Stmt_Match;

struct Stmt_Code_ {
typedef struct Stmt_Code_ {
Stmt_List stmt_list;
};
} *Stmt_Code;

struct Stmt_For_ {
Stmt c1;
Expand All @@ -528,24 +521,23 @@ struct Stmt_For_ {
Stmt body;
};

struct EachIdx_ {
Tag tag;
struct Value_ *v;
typedef struct EachIdx_ {
Var_Decl var;
bool is_var;
};
} EachIdx;

struct Stmt_Each_ {
Tag tag;
Exp exp;
Stmt body;
struct EachIdx_ *idx;
struct Value_ * v;
Var_Decl var;
Tag tag;
Exp exp;
Stmt body;
EachIdx *idx;
};

struct Stmt_Loop_ {
Exp cond;
Stmt body;
struct EachIdx_ *idx;
Exp cond;
Stmt body;
EachIdx *idx;
};

struct Stmt_If_ {
Expand Down Expand Up @@ -575,15 +567,15 @@ typedef struct EnumValue {
struct gwint gwint;
bool set;
} EnumValue;
typedef MP_Vector *Enum_List;
typedef MP_Vector *EnumValue_List;
typedef struct Enum_Def_ {
Tag tag;
ID_List list;
struct Type_ *type;
ae_flag flag;
Tag tag;
EnumValue_List list;
struct Type_ *type;
ae_flag flag;
} *Enum_Def;
ANN2(1, 2)
ANEW AST_NEW(Enum_Def, enum_def, const Enum_List, struct Symbol_ *,
ANEW AST_NEW(Enum_Def, enum_def, const EnumValue_List, struct Symbol_ *,
const loc_t);
ANN void free_enum_def(MemPool p, Enum_Def);

Expand Down
2 changes: 1 addition & 1 deletion include/cpy_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ANN Class_Def cpy_class_def(MemPool, Class_Def);
ANN Extend_Def cpy_extend_def(MemPool, Extend_Def);
ANN Fptr_Def cpy_fptr_def(MemPool p, const Fptr_Def src);
ANN Union_Def cpy_union_def(MemPool, Union_Def);
ANN Type_List cpy_type_list(MemPool p, const Type_List src);
ANN TmplArg_List cpy_tmplarg_list(MemPool p, const TmplArg_List src);
ANN ID_List cpy_id_list(MemPool p, const ID_List src);
ANN Tmpl *cpy_tmpl(MemPool p, const Tmpl *src);
ANN Ast cpy_ast(MemPool p, Ast src);
Expand Down
6 changes: 3 additions & 3 deletions include/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ extern int gwion_debug;
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
union YYSTYPE
{
#line 30 "src/gwion.y"
#line 38 "src/gwion.y"

bool yybool;
ae_stmt_t stmt_t;
Expand Down Expand Up @@ -344,7 +344,7 @@ union YYSTYPE
Arg arg;
Func_Def func_def;
EnumValue enum_value;
Enum_List enum_list;
EnumValue_List enum_list;
Enum_Def enum_def;
Union_Def union_def;
Fptr_Def fptr_def;
Expand All @@ -354,7 +354,7 @@ union YYSTYPE
Specialized specialized;
Specialized_List specialized_list;
TmplArg tmplarg;
Type_List type_list;
TmplArg_List tmplarg_list;
Variable variable;
Variable_List variable_list;
Extend_Def extend_def;
Expand Down
52 changes: 32 additions & 20 deletions pass/xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,21 @@ ANN static void xxx_specialized_list(XXX *a, Specialized_List b) {
}
}

ANN static void xxx_type_list(XXX *a, Type_List b) {
ANN static void xxx_tmplarg(XXX *a, TmplArg *b) {
if (b->type == tmplarg_td) xxx_type_decl(a, b->d.td);
else xxx_exp(a, b->d.exp);
}

ANN static void xxx_tmplarg_list(XXX *a, TmplArg_List b) {
for(uint32_t i = 0; i < b->len; i++) {
Type_Decl * c = *mp_vector_at(b, Type_Decl*, i);
xxx_type_decl(a, c);
TmplArg * c = mp_vector_at(b, TmplArg, i);
xxx_tmplarg(a, c);
}
}

ANN static void xxx_tmpl(XXX *a, Tmpl *b) {
if(b->list) xxx_id_list(a, b->list);
if(b->call) xxx_type_list(a, b->call);
if(b->list) xxx_specialized_list(a, b->list);
if(b->call) xxx_tmplarg_list(a, b->call);
}

ANN static void xxx_range(XXX *a, Range *b) {
Expand All @@ -60,7 +65,7 @@ ANN static void xxx_range(XXX *a, Range *b) {
ANN static void xxx_type_decl(XXX *a, Type_Decl *b) {
xxx_tag(a, &b->tag);
if(b->array) xxx_array_sub(a, b->array);
if(b->types) xxx_type_list(a, b->types);
if(b->types) xxx_tmplarg_list(a, b->types);
}

ANN static void xxx_prim_id(XXX *a, Symbol *b) {
Expand Down Expand Up @@ -98,10 +103,6 @@ ANN static void xxx_prim_hack(XXX *a, Exp *b) {
xxx_exp(a, *b);
}

ANN static void xxx_prim_typeof(XXX *a, Exp *b) {
xxx_exp(a, *b);
}

ANN static void xxx_prim_interp(XXX *a, Exp *b) {
xxx_exp(a, *b);
}
Expand Down Expand Up @@ -149,7 +150,7 @@ ANN static void xxx_exp_binary(XXX *a, Exp_Binary *b) {
}

ANN static void xxx_capture(XXX *a, Capture *b) {
xxx_tag(a, &b->tag);
xxx_tag(a, &b->var.tag);
}

ANN static void xxx_captures(XXX *a, Capture_List b) {
Expand Down Expand Up @@ -263,14 +264,12 @@ ANN static void xxx_stmt_code(XXX *a, Stmt_Code b) {
if(b->stmt_list) xxx_stmt_list(a, b->stmt_list);
}

ANN static void xxx_stmt_break(XXX *a, Stmt_Exp b) {
(void)a;
(void)b;
ANN static void xxx_stmt_break(XXX *a, Stmt_Index b) {
xxx_stmt_index(a, b);
}

ANN static void xxx_stmt_continue(XXX *a, Stmt_Exp b) {
(void)a;
(void)b;
ANN static void xxx_stmt_continue(XXX *a, Stmt_Index b) {
xxx_stmt_index(a, b);
}

ANN static void xxx_stmt_return(XXX *a, Stmt_Exp b) {
Expand Down Expand Up @@ -311,8 +310,21 @@ ANN static void xxx_stmt_retry(XXX *a, Stmt_Exp b) {
(void)b;
}

ANN static void xxx_handler(XXX *a, Handler *b) {
xxx_tag(a, &b->tag);
xxx_stmt(a, b->stmt);
}

ANN static void xxx_handler_list(XXX *a, Handler_List b) {
for(uint32_t i = 0; i < b->len; i++) {
Handler *handler = mp_vector_at(b, Handler, i);
xxx_handler(a, handler);
}
}

ANN static void xxx_stmt_try(XXX *a, Stmt_Try b) {
xxx_stmt(a, b->stmt);
xxx_handler_list(a, b->handler);
}

ANN static void xxx_stmt_defer(XXX *a, Stmt_Defer b) {
Expand Down Expand Up @@ -376,11 +388,11 @@ ANN static void xxx_trait_def(XXX *a, Trait_Def b) {
}

ANN static void xxx_enumvalue(XXX *a, EnumValue *b) {
(void)a;
(void)b;
xxx_tag(a, &b->tag);
// gwint, set
}

ANN static void xxx_enum_list(XXX *a, Enum_List b) {
ANN static void xxx_enum_list(XXX *a, EnumValue_List b) {
for(uint32_t i = 0; i < b->len; i++) {
EnumValue *c = mp_vector_at(b, EnumValue, i);
xxx_enumvalue(a, c);
Expand Down
6 changes: 2 additions & 4 deletions pass/xxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ ANN static void xxx_prim_array(XXX *a, Array_Sub *b);
ANN static void xxx_prim_range(XXX *a, Range* *b);
ANN static void xxx_prim_dict(XXX *a, Exp *b);
ANN static void xxx_prim_hack(XXX *a, Exp *b);
ANN static void xxx_prim_typeof(XXX *a, Exp *b);
ANN static void xxx_prim_interp(XXX *a, Exp *b);
ANN static void xxx_prim_char(XXX *a, m_str *b);
ANN static void xxx_prim_nil(XXX *a, void *b);
Expand Down Expand Up @@ -47,8 +46,8 @@ ANN static void xxx_stmt_each(XXX *a, Stmt_Each b);
ANN static void xxx_stmt_loop(XXX *a, Stmt_Loop b);
ANN static void xxx_stmt_if(XXX *a, Stmt_If b);
ANN static void xxx_stmt_code(XXX *a, Stmt_Code b);
ANN static void xxx_stmt_break(XXX *a, Stmt_Exp b);
ANN static void xxx_stmt_continue(XXX *a, Stmt_Exp b);
ANN static void xxx_stmt_break(XXX *a, Stmt_Index b);
ANN static void xxx_stmt_continue(XXX *a, Stmt_Index b);
ANN static void xxx_stmt_return(XXX *a, Stmt_Exp b);
ANN static void xxx_case_list(XXX *a, Stmt_List b);
ANN static void xxx_stmt_match(XXX *a, Stmt_Match b);
Expand All @@ -71,6 +70,5 @@ ANN static void xxx_union_def(XXX *a, Union_Def b);
ANN static void xxx_fptr_def(XXX *a, Fptr_Def b);
ANN static void xxx_type_def(XXX *a, Type_Def b);
ANN static void xxx_extend_def(XXX *a, Extend_Def b);
ANN static void xxx_spread_def(XXX *a, Spread_Def b);
ANN static void xxx_section(XXX *a, Section *b);
ANN static void xxx_ast(XXX *a, Ast b);
Loading

0 comments on commit 0ba7b3b

Please sign in to comment.