918 lines
20 KiB
C
918 lines
20 KiB
C
typedef struct Expr Expr;
|
|
typedef struct Note Note;
|
|
typedef struct Decl Decl;
|
|
typedef struct Stmt Stmt;
|
|
typedef struct Stmt_If Stmt_If;
|
|
typedef struct Typespec Typespec;
|
|
typedef struct Decl_Enum_Child Decl_Enum_Child;
|
|
typedef struct Decl_Function_Arg Decl_Function_Arg;
|
|
typedef struct Expr_Compound_Field Expr_Compound_Field;
|
|
|
|
typedef struct AST AST;
|
|
typedef struct AST_Parent AST_Parent;
|
|
|
|
typedef enum AST_Kind{
|
|
AST_None,
|
|
|
|
AST_Decl_Struct, //
|
|
AST_Decl_SubStruct,
|
|
AST_Decl_SubUnion,
|
|
AST_Decl_Union,
|
|
AST_Decl_Enum,
|
|
AST_Decl_Note,
|
|
AST_Decl_Func,
|
|
AST_List,
|
|
AST_Stmt_If,
|
|
AST_Stmt_Decl,
|
|
AST_Stmt_Expr,
|
|
AST_Stmt_Return,
|
|
AST_Stmt_For,
|
|
AST_FirstParent = AST_Decl_Struct,
|
|
AST_LastParent = AST_Stmt_For,
|
|
|
|
AST_Decl_Func_Arg,
|
|
|
|
AST_Decl_Variable,
|
|
}AST_Kind;
|
|
|
|
#define AST_FIELDS AST *next, *prev; AST *notes; AST_Parent *parent; Token *pos; AST_Kind kind
|
|
#define AST_PARENT_FIELDS AST *first, *last
|
|
#define AST_UNION union{ struct{ AST_FIELDS; }; AST ast; }
|
|
#define AST_PARENT_UNION union{ struct{ AST_PARENT_FIELDS; }; AST_Parent child; }
|
|
|
|
struct AST{
|
|
AST_FIELDS;
|
|
};
|
|
|
|
typedef struct AST_Parent{
|
|
AST_UNION;
|
|
AST_PARENT_FIELDS;
|
|
} AST_Parent;
|
|
|
|
typedef struct AST_Note{
|
|
AST_PARENT_UNION;
|
|
Intern_String name;
|
|
Expr *expr;
|
|
}AST_Note;
|
|
|
|
typedef struct Decl_Struct{
|
|
AST_PARENT_UNION;
|
|
Intern_String name;
|
|
}Decl_Struct;
|
|
|
|
typedef struct Decl_Func_Arg{
|
|
AST_UNION;
|
|
Intern_String name;
|
|
Typespec *typespec;
|
|
}Decl_Func_Arg;
|
|
|
|
typedef struct AST_Decl_Enum_Child{
|
|
AST_UNION;
|
|
Intern_String name;
|
|
Expr *expr;
|
|
}AST_Decl_Enum_Child;
|
|
|
|
typedef struct Decl_Enum{
|
|
AST_PARENT_UNION;
|
|
Typespec *typespec;
|
|
}Decl_Enum;
|
|
|
|
typedef struct Decl_Func{
|
|
AST_PARENT_UNION;
|
|
Typespec *ret;
|
|
AST *body;
|
|
}Decl_Func;
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Expressions
|
|
//-----------------------------------------------------------------------------
|
|
typedef enum Expr_Sizeof_Kind{
|
|
SIZEOF_Expr,
|
|
SIZEOF_Type,
|
|
}Expr_Sizeof_Kind;
|
|
|
|
typedef enum Expr_Kind{
|
|
EK_None,
|
|
EK_Int,
|
|
EK_String,
|
|
EK_Identifier,
|
|
EK_Compound,
|
|
EK_Paren,
|
|
EK_PostfixUnary,
|
|
EK_Unary,
|
|
EK_Binary,
|
|
EK_Ternary,
|
|
EK_Cast,
|
|
EK_Field,
|
|
EK_Call,
|
|
EK_Index,
|
|
EK_SizeType,
|
|
EK_SizeExpr,
|
|
} Expr_Kind;
|
|
|
|
typedef enum Expr_Compound_Kind{
|
|
COMPOUND_None,
|
|
COMPOUND_Default,
|
|
COMPOUND_Named,
|
|
COMPOUND_Index,
|
|
}Expr_Compound_Kind;
|
|
|
|
struct Expr_Compound_Field{
|
|
Expr_Compound_Field *next;
|
|
Expr_Compound_Kind kind;
|
|
Token *pos;
|
|
Expr *init;
|
|
union{
|
|
Expr *index;
|
|
Intern_String name;
|
|
};
|
|
};
|
|
|
|
struct Expr {
|
|
Expr_Kind kind;
|
|
Token *token;
|
|
Expr *next;
|
|
union {
|
|
U64 int_val;
|
|
Intern_String intern_val;
|
|
double float_val;
|
|
struct {
|
|
Expr *expr;
|
|
} paren;
|
|
struct {
|
|
Typespec *typespec;
|
|
Expr* expr;
|
|
} cast;
|
|
struct{
|
|
Intern_String name;
|
|
Expr *expr;
|
|
}field;
|
|
|
|
struct{
|
|
Typespec *typespec;
|
|
Expr_Compound_Field *first;
|
|
Expr_Compound_Field *last;
|
|
}compound;
|
|
|
|
struct {
|
|
Expr *atom;
|
|
Expr_Compound_Field *first;
|
|
Expr_Compound_Field *last;
|
|
} call;
|
|
struct{
|
|
|
|
}arg;
|
|
struct {
|
|
Expr *atom;
|
|
Expr *index;
|
|
} index;
|
|
|
|
struct {
|
|
Token_Kind op;
|
|
Expr* expr;
|
|
} unary;
|
|
struct {
|
|
Token_Kind op;
|
|
Expr *expr;
|
|
} postfix_unary;
|
|
struct {
|
|
Token_Kind op;
|
|
Expr* left;
|
|
Expr* right;
|
|
} binary;
|
|
struct {
|
|
Expr* cond;
|
|
Expr* on_true;
|
|
Expr* on_false;
|
|
} ternary;
|
|
|
|
struct{
|
|
Typespec *typespec;
|
|
} size_type;
|
|
|
|
struct{
|
|
Expr *expr;
|
|
} size_expr;
|
|
};
|
|
};
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Type specifiers
|
|
//-----------------------------------------------------------------------------
|
|
typedef enum Typespec_Kind{
|
|
TS_None,
|
|
TS_Name,
|
|
TS_Pointer,
|
|
TS_Array,
|
|
TS_Function,
|
|
TS_NamedArgument,
|
|
}Typespec_Kind;
|
|
|
|
struct Typespec{
|
|
Typespec *next;
|
|
Typespec_Kind kind;
|
|
Token *pos;
|
|
union{
|
|
Typespec *base;
|
|
Intern_String name;
|
|
struct{
|
|
Intern_String name;
|
|
Typespec *base;
|
|
}named;
|
|
struct{
|
|
Typespec *first;
|
|
Typespec *last;
|
|
Typespec *ret;
|
|
}func;
|
|
struct{
|
|
Typespec *base;
|
|
Expr *size;
|
|
}arr;
|
|
};
|
|
};
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Declarations
|
|
//-----------------------------------------------------------------------------
|
|
typedef enum Decl_Kind{
|
|
DECL_None,
|
|
DECL_Struct,
|
|
DECL_Union,
|
|
DECL_SubStruct,
|
|
DECL_SubUnion,
|
|
DECL_Enum,
|
|
DECL_Variable,
|
|
DECL_Const,
|
|
DECL_Typedef,
|
|
DECL_Function,
|
|
DECL_List,
|
|
}Decl_Kind;
|
|
|
|
|
|
struct Decl{
|
|
Decl_Kind kind;
|
|
Decl *next;
|
|
B32 is_incomplete;
|
|
|
|
Decl *prev; // Doubly linked list?
|
|
Decl *parent; // ?
|
|
|
|
Intern_String name;
|
|
Token *pos;
|
|
|
|
Note *first_note;
|
|
Note *last_note;
|
|
union{
|
|
struct{
|
|
Decl_Enum_Child *first;
|
|
Decl_Enum_Child *last;
|
|
Typespec *typespec;
|
|
}enum_decl;
|
|
struct{
|
|
Decl *first;
|
|
Decl *last;
|
|
}struct_decl;
|
|
struct{
|
|
Typespec *typespec;
|
|
Expr *expr;
|
|
}var_decl;
|
|
struct{
|
|
Typespec *typespec;
|
|
}typedef_decl;
|
|
struct{
|
|
Decl_Function_Arg *first;
|
|
Decl_Function_Arg *last;
|
|
Typespec *ret;
|
|
Stmt *body;
|
|
}func_decl;
|
|
struct{
|
|
Decl *first;
|
|
Decl *last;
|
|
}list_decl;
|
|
};
|
|
};
|
|
|
|
struct Note{
|
|
Token *pos;
|
|
Intern_String name;
|
|
Expr *expr;
|
|
Note *next;
|
|
Note *first;
|
|
Note *last;
|
|
};
|
|
|
|
struct Decl_Function_Arg{
|
|
Decl_Function_Arg *next;
|
|
Token *pos;
|
|
Intern_String name;
|
|
Typespec *typespec;
|
|
};
|
|
|
|
struct Decl_Enum_Child{
|
|
Decl_Enum_Child *next;
|
|
Intern_String name;
|
|
Token *pos;
|
|
Expr *expr;
|
|
Note *first_note;
|
|
Note *last_note;
|
|
};
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Statements
|
|
//-----------------------------------------------------------------------------
|
|
typedef enum Stmt_Kind{
|
|
STMT_None,
|
|
STMT_Decl,
|
|
STMT_Expr,
|
|
STMT_List,
|
|
STMT_Return,
|
|
STMT_If,
|
|
STMT_For,
|
|
}Stmt_Kind;
|
|
|
|
struct Stmt_If{
|
|
Stmt_If *next;
|
|
Expr *cond;
|
|
Stmt *body;
|
|
};
|
|
|
|
struct Stmt{
|
|
Stmt_Kind kind;
|
|
Stmt *next;
|
|
Token *pos;
|
|
union{
|
|
Stmt_If stmt_if;
|
|
Decl *decl;
|
|
Expr *expr;
|
|
struct{
|
|
Stmt *first;
|
|
Stmt *last;
|
|
}list;
|
|
struct{
|
|
Expr *expr;
|
|
}ret;
|
|
};
|
|
};
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Expression constructors
|
|
//-----------------------------------------------------------------------------
|
|
function Expr *
|
|
expr_new(Arena *p, Expr_Kind kind, Token *token){
|
|
Expr *expr = arena_push_struct(p, Expr);
|
|
expr->kind = kind;
|
|
expr->token = token;
|
|
return expr;
|
|
}
|
|
|
|
function Expr *
|
|
expr_int(Arena *p, Token *token){
|
|
assert(token->kind == TK_Int);
|
|
Expr *expr = expr_new(p, EK_Int, token);
|
|
expr->int_val = token->int_val;
|
|
return expr;
|
|
}
|
|
|
|
function Expr *
|
|
expr_str(Arena *p, Token *token){
|
|
assert(token->kind == TK_StringLit);
|
|
Expr *expr = expr_new(p, EK_String, token);
|
|
expr->intern_val = token->intern_val;
|
|
return expr;
|
|
}
|
|
|
|
function Expr *
|
|
expr_identifier(Arena *p, Token *token){
|
|
assert(token->kind == TK_Identifier);
|
|
Expr *expr = expr_new(p, EK_Identifier, token);
|
|
expr->intern_val = token->intern_val;
|
|
return expr;
|
|
}
|
|
|
|
function Expr *
|
|
expr_field(Arena *p, Token *token, Expr *inexpr){
|
|
assert(token->kind == TK_Identifier);
|
|
Expr *expr = expr_new(p, EK_Field, token);
|
|
expr->field.expr = inexpr;
|
|
expr->field.name = token->intern_val;
|
|
return expr;
|
|
}
|
|
|
|
function Expr *
|
|
expr_paren(Arena *p, Token *token, Expr *inexpr){
|
|
Expr *expr = expr_new(p, EK_Paren, token);
|
|
expr->paren.expr = inexpr;
|
|
return expr;
|
|
}
|
|
|
|
function Expr *
|
|
expr_postfix_unary(Arena *p, Token *op, Expr *exp){
|
|
Expr *expr = expr_new(p, EK_PostfixUnary, op);
|
|
expr->unary.op = op->kind;
|
|
expr->unary.expr = exp;
|
|
return expr;
|
|
}
|
|
|
|
function Expr *
|
|
expr_unary(Arena *p, Token *op, Expr *exp){
|
|
Expr *expr = expr_new(p, EK_Unary, op);
|
|
expr->unary.op = op->kind;
|
|
expr->unary.expr = exp;
|
|
return expr;
|
|
}
|
|
|
|
function Expr *
|
|
expr_binary(Arena *p, Token *op, Expr *left, Expr *right){
|
|
Expr *expr = expr_new(p, EK_Binary, op);
|
|
expr->binary.op = op->kind;
|
|
expr->binary.left = left;
|
|
expr->binary.right = right;
|
|
return expr;
|
|
}
|
|
|
|
function Expr *
|
|
expr_ternary(Arena *p, Token *op, Expr *cond, Expr *on_true, Expr *on_false){
|
|
Expr *expr = expr_new(p, EK_Ternary, op);
|
|
expr->ternary.cond = cond;
|
|
expr->ternary.on_true = on_true;
|
|
expr->ternary.on_false = on_false;
|
|
return expr;
|
|
}
|
|
|
|
function Expr *
|
|
expr_call(Arena *p, Token *token, Expr *atom){
|
|
Expr *expr = expr_new(p, EK_Call, token);
|
|
expr->call.atom = atom;
|
|
return expr;
|
|
}
|
|
|
|
function Expr *
|
|
expr_index(Arena *p, Token *token, Expr *atom, Expr *index){
|
|
Expr *expr = expr_new(p, EK_Index, token);
|
|
expr->index.atom = atom;
|
|
expr->index.index = index;
|
|
return expr;
|
|
}
|
|
|
|
function Expr *
|
|
expr_cast(Arena *p, Token *token, Typespec *type, Expr *exp){
|
|
Expr *expr = expr_new(p, EK_Cast, token);
|
|
expr->cast.typespec = type;
|
|
expr->cast.expr = exp;
|
|
return expr;
|
|
}
|
|
|
|
function Expr *
|
|
expr_size_type(Arena *p, Token *token, Typespec *type){
|
|
Expr *expr = expr_new(p, EK_SizeType, token);
|
|
expr->size_type.typespec = type;
|
|
return expr;
|
|
}
|
|
|
|
function Expr *
|
|
expr_sizeof_expr(Arena *p, Token *token, Expr *in_expr){
|
|
Expr *expr = expr_new(p, EK_SizeExpr, token);
|
|
expr->size_expr.expr = in_expr;
|
|
return expr;
|
|
}
|
|
|
|
function Expr *
|
|
expr_compound(Arena *arena, Token *pos, Typespec *typespec){
|
|
Expr *result = expr_new(arena, EK_Compound, pos);
|
|
result->compound.typespec = typespec;
|
|
return result;
|
|
}
|
|
|
|
function Expr_Compound_Field *
|
|
expr_compound_new(Arena *arena, Expr_Compound_Kind kind, Token *pos, Expr *init){
|
|
Expr_Compound_Field *result = arena_push_struct(arena, Expr_Compound_Field);
|
|
result->kind = kind;
|
|
result->pos = pos;
|
|
result->init = init;
|
|
return result;
|
|
}
|
|
|
|
function Expr_Compound_Field *
|
|
expr_compound_default(Arena *arena, Token *pos, Expr *init){
|
|
Expr_Compound_Field *result = expr_compound_new(arena, COMPOUND_Default, pos, init);
|
|
return result;
|
|
}
|
|
|
|
function Expr_Compound_Field *
|
|
expr_compound_named(Arena *arena, Token *pos, Intern_String name, Expr *init){
|
|
Expr_Compound_Field *result = expr_compound_new(arena, COMPOUND_Named, pos, init);
|
|
result->name = name;
|
|
return result;
|
|
}
|
|
|
|
function Expr_Compound_Field *
|
|
expr_compound_index(Arena *arena, Token *pos, Expr *index, Expr *init){
|
|
Expr_Compound_Field *result = expr_compound_new(arena, COMPOUND_Index, pos, init);
|
|
result->index = index;
|
|
return result;
|
|
}
|
|
|
|
function void
|
|
expr_call_push(Expr *list, Expr_Compound_Field *field){
|
|
SLLQueuePush(list->call.first, list->call.last, field);
|
|
}
|
|
|
|
function void
|
|
expr_compound_push(Expr *list, Expr_Compound_Field *field){
|
|
SLLQueuePush(list->compound.first, list->compound.last, field);
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Type specifier constructors
|
|
//-----------------------------------------------------------------------------
|
|
function Typespec *
|
|
typespec_new(Arena *p, Typespec_Kind kind, Token *pos){
|
|
Typespec *result = arena_push_struct(p, Typespec);
|
|
result->kind = kind;
|
|
result->pos = pos;
|
|
return result;
|
|
}
|
|
|
|
function Typespec *
|
|
typespec_name(Arena *p, Token *pos, Intern_String name){
|
|
Typespec *result = typespec_new(p, TS_Name, pos);
|
|
result->name = name;
|
|
return result;
|
|
}
|
|
|
|
function Typespec *
|
|
typespec_pointer(Arena *p, Token *pos, Typespec *base){
|
|
Typespec *result = typespec_new(p, TS_Pointer, pos);
|
|
result->base = base;
|
|
return result;
|
|
}
|
|
|
|
function Typespec *
|
|
typespec_array(Arena *p, Token *pos, Typespec *base, Expr *size){
|
|
Typespec *result = typespec_new(p, TS_Array, pos);
|
|
result->arr.base = base;
|
|
result->arr.size = size;
|
|
return result;
|
|
}
|
|
|
|
function Typespec *
|
|
typespec_function(Arena *p, Token *pos, Typespec *ret){
|
|
Typespec *result = typespec_new(p, TS_Function, pos);
|
|
result->func.ret = ret;
|
|
return result;
|
|
}
|
|
|
|
function Typespec *
|
|
typespec_named_argument(Arena *arena, Token *pos, Typespec *typespec, Intern_String name){
|
|
Typespec *result = typespec_new(arena, TS_NamedArgument, pos);
|
|
result->named.base = typespec;
|
|
result->named.name = name;
|
|
return result;
|
|
}
|
|
|
|
function void
|
|
typespec_function_push(Typespec *func, Typespec *arg){
|
|
SLLQueuePush(func->func.first, func->func.last, arg);
|
|
}
|
|
//-----------------------------------------------------------------------------
|
|
// Notes
|
|
//-----------------------------------------------------------------------------
|
|
function void
|
|
decl_pass_notes(Decl *a, Note *b){
|
|
a->first_note = b->first;
|
|
a->last_note = b->last;
|
|
}
|
|
|
|
function Note *
|
|
note_push_new(Arena *p, Note *parent, Token *pos, Intern_String name, Expr *expr){
|
|
Note *result = arena_push_struct(p, Note);
|
|
result->pos = pos;
|
|
result->name = name;
|
|
result->expr = expr;
|
|
SLLQueuePush(parent->first, parent->last, result);
|
|
return result;
|
|
}
|
|
|
|
function Note *
|
|
find_note(Note *first, String string){
|
|
for(Note *n = first; n; n=n->next){
|
|
if(string_compare(string, n->name.s)){
|
|
return n;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
function String
|
|
find_string_note(Note *first, String string, String default_string){
|
|
Note *note = find_note(first, string);
|
|
if(note){
|
|
return note->expr->token->intern_val.s;
|
|
}
|
|
return default_string;
|
|
}
|
|
|
|
function Note *
|
|
decl_find_note(Decl *decl, String string){
|
|
return find_note(decl->first_note, string);
|
|
}
|
|
|
|
function String
|
|
decl_find_string_note(Decl *decl, String string, String default_string){
|
|
return find_string_note(decl->first_note, string, default_string);
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Declarations
|
|
//-----------------------------------------------------------------------------
|
|
function Decl *
|
|
decl_new(Arena *p, Decl_Kind kind, Token *pos, Intern_String name){
|
|
Decl *result = arena_push_struct(p, Decl);
|
|
result->kind = kind;
|
|
result->pos = pos;
|
|
result->name = name;
|
|
return result;
|
|
}
|
|
|
|
function Decl *
|
|
decl_struct(Arena *p, Decl_Kind kind, Token *pos, Intern_String name){
|
|
assert(kind == DECL_Struct || kind == DECL_Union || kind == DECL_SubUnion || kind == DECL_SubStruct);
|
|
Decl *result = decl_new(p, kind, pos, name);
|
|
return result;
|
|
}
|
|
|
|
function Decl *
|
|
decl_typedef(Arena *p, Token *pos, Intern_String name, Typespec *type){
|
|
Decl *result = decl_new(p, DECL_Typedef, pos, name);
|
|
result->typedef_decl.typespec = type;
|
|
return result;
|
|
}
|
|
|
|
function Decl *
|
|
decl_const(Arena *arena, Token *pos, Intern_String name, Expr *expr, Typespec *typespec){
|
|
Decl *result = decl_new(arena, DECL_Const, pos, name);
|
|
result->var_decl.expr = expr;
|
|
result->var_decl.typespec = typespec;
|
|
return result;
|
|
}
|
|
|
|
function Decl *
|
|
decl_enum(Arena *p, Token *pos, Intern_String name, Typespec *typespec){
|
|
Decl *result = decl_new(p, DECL_Enum, pos, name);
|
|
result->enum_decl.typespec = typespec;
|
|
return result;
|
|
}
|
|
|
|
function Decl *
|
|
decl_variable(Arena *p, Token *pos, Intern_String name, Typespec *typespec, Expr *expr){
|
|
Decl *result = decl_new(p, DECL_Variable, pos, name);
|
|
result->var_decl.typespec = typespec;
|
|
result->var_decl.expr = expr;
|
|
return result;
|
|
}
|
|
|
|
function Decl *
|
|
decl_function(Arena *p, Token *pos, Intern_String name, Typespec *ret){
|
|
Decl *result = decl_new(p, DECL_Function, pos, name);
|
|
result->func_decl.ret = ret;
|
|
return result;
|
|
}
|
|
|
|
function void
|
|
decl_func_push(Arena *p, Decl *parent, Token *pos, Intern_String name, Typespec *type){
|
|
assert(parent->kind == DECL_Function);
|
|
Decl_Function_Arg *result = arena_push_struct(p, Decl_Function_Arg);
|
|
result->name = name;
|
|
result->typespec = type;
|
|
result->pos = pos;
|
|
SLLQueuePush(parent->func_decl.first, parent->func_decl.last, result);
|
|
}
|
|
|
|
function void
|
|
decl_enum_push(Arena *p, Decl *parent, Token *pos, Intern_String name, Expr *expr, Note *notes){
|
|
assert(parent->kind == DECL_Enum);
|
|
Decl_Enum_Child *child = arena_push_struct(p, Decl_Enum_Child);
|
|
child->pos = pos;
|
|
child->name = name;
|
|
child->expr = expr;
|
|
child->first_note = notes->first;
|
|
child->last_note = notes->last;
|
|
SLLQueuePush(parent->enum_decl.first, parent->enum_decl.last, child);
|
|
}
|
|
|
|
function void
|
|
decl_struct_push(Decl *parent, Decl *child){
|
|
assert(parent->kind == DECL_Struct || parent->kind == DECL_Union || parent->kind == DECL_SubUnion || parent->kind == DECL_SubStruct);
|
|
SLLQueuePush(parent->struct_decl.first, parent->struct_decl.last, child);
|
|
}
|
|
|
|
function void
|
|
decl_list_push(Decl *parent, Decl *child){
|
|
assert(parent->kind == DECL_List);
|
|
SLLQueuePush(parent->list_decl.first, parent->list_decl.last, child);
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Statements
|
|
//-----------------------------------------------------------------------------
|
|
function Stmt *
|
|
stmt_new(Arena *p, Stmt_Kind kind, Token *pos){
|
|
Stmt *result = arena_push_struct(p, Stmt);
|
|
result->kind = kind;
|
|
result->pos = pos;
|
|
return result;
|
|
}
|
|
|
|
function Stmt *
|
|
stmt_decl(Arena *p, Token *pos, Decl *decl){
|
|
Stmt *result = stmt_new(p, STMT_Decl, pos);
|
|
result->decl = decl;
|
|
return result;
|
|
}
|
|
|
|
function Stmt *
|
|
stmt_expr(Arena *p, Token *pos, Expr *expr){
|
|
Stmt *result = stmt_new(p, STMT_Expr, pos);
|
|
result->expr = expr;
|
|
return result;
|
|
}
|
|
|
|
function Stmt *
|
|
stmt_list(Arena *p, Token *pos){
|
|
Stmt *result = stmt_new(p, STMT_List, pos);
|
|
return result;
|
|
}
|
|
|
|
function Stmt *
|
|
stmt_return(Arena *p, Token *pos, Expr *expr){
|
|
Stmt *result = stmt_new(p, STMT_Return, pos);
|
|
result->ret.expr = expr;
|
|
return result;
|
|
}
|
|
|
|
function Stmt *
|
|
stmt_if(Arena *p, Token *pos, Stmt *body, Expr *cond){
|
|
Stmt *result = stmt_new(p, STMT_If, pos);
|
|
result->stmt_if.cond = cond;
|
|
result->stmt_if.body = body;
|
|
return result;
|
|
}
|
|
|
|
function void
|
|
stmt_push(Stmt *stmt, Stmt *child){
|
|
SLLQueuePush(stmt->list.first, stmt->list.last, child);
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Double linked list
|
|
//-----------------------------------------------------------------------------
|
|
function void
|
|
list_print(AST_Parent *decls){
|
|
printf("\n");
|
|
printf("next:");
|
|
for(AST *n = decls->first; n; n=n->next){
|
|
printf("%d", n->kind);
|
|
}
|
|
printf("prev:");
|
|
for(AST *n = decls->last; n; n=n->prev){
|
|
printf("%d", n->kind);
|
|
}
|
|
printf("parent:");
|
|
for(AST *n = decls->first; n; n=n->next){
|
|
printf("%d", n->parent->kind);
|
|
}
|
|
}
|
|
|
|
function void
|
|
ast_remove(AST *node){
|
|
AST_Parent *l = node->parent;
|
|
assert(l);
|
|
if (l->first==l->last){
|
|
assert(node==l->last);
|
|
l->first=l->last=0;
|
|
}
|
|
else if (l->last==node){
|
|
l->last=l->last->prev;
|
|
l->last->next=0;
|
|
}
|
|
else if (l->first==node){
|
|
l->first=l->first->next;
|
|
l->first->prev=0;
|
|
}
|
|
else {
|
|
node->prev->next=node->next;
|
|
node->next->prev=node->prev;
|
|
}
|
|
node->parent=0;
|
|
node->prev=0;
|
|
node->next=0;
|
|
}
|
|
|
|
function void
|
|
ast_push_first(AST_Parent *l, AST *node){
|
|
if (l->first==0){
|
|
l->first=l->last=node;
|
|
node->prev=0;
|
|
node->next=0;
|
|
}
|
|
else {
|
|
l->last->next=node;
|
|
node->prev=l->last;
|
|
node->next=0;
|
|
l->last=node;
|
|
}
|
|
node->parent=l;
|
|
}
|
|
|
|
function void
|
|
ast_push_last(AST_Parent *l, AST *node){
|
|
if(l->first == 0){
|
|
l->first = l->last = node;
|
|
node->prev = 0;
|
|
node->next = 0;
|
|
}
|
|
else {
|
|
node->next = l->first;
|
|
l->first->prev = node;
|
|
node->prev = 0;
|
|
l->first = node;
|
|
}
|
|
node->parent = l;
|
|
}
|
|
|
|
function void
|
|
ast_push_after(AST *in_list, AST *node){
|
|
AST_Parent *parent = in_list->parent;
|
|
assert(parent);
|
|
assert(parent->first && parent->last);
|
|
|
|
node->prev = in_list;
|
|
if(in_list == parent->last){
|
|
in_list->next = node;
|
|
parent->last = node;
|
|
}
|
|
else {
|
|
node->next = in_list->next;
|
|
in_list->next = node;
|
|
node->next->prev = node;
|
|
}
|
|
node->parent=parent;
|
|
}
|
|
|
|
function void
|
|
ast_push_before(AST *in_list, AST *node){
|
|
AST_Parent *parent = in_list->parent;
|
|
assert(parent);
|
|
assert(parent->first && parent->last);
|
|
|
|
node->next = in_list;
|
|
if(parent->first == in_list){
|
|
in_list->prev = node;
|
|
parent->first = node;
|
|
}
|
|
else{
|
|
node->prev = in_list->prev;
|
|
in_list->prev = node;
|
|
node->prev->next = node;
|
|
}
|
|
node->parent = parent;
|
|
}
|
|
|
|
|
|
function void
|
|
ast_test(){
|
|
printf("\nAST_Size = %u", (U32)sizeof(AST));
|
|
printf("\nAST_Struct_Size = %u", (U32)sizeof(Decl_Struct));
|
|
|
|
AST_Parent parent = {0};
|
|
AST decls[16] = {0};
|
|
parent.kind = 0;
|
|
for(int i = 0; i < buff_cap(decls); i++){
|
|
decls[i].kind = i+1;
|
|
}
|
|
|
|
ast_push_first(&parent, decls);
|
|
ast_push_first(&parent, decls+1);
|
|
ast_push_first(&parent, decls+2);
|
|
ast_push_before(decls, decls+3);
|
|
ast_push_before(decls, decls+4);
|
|
ast_push_after(decls, decls+5);
|
|
ast_push_after(decls+5, decls+6);
|
|
|
|
//list_print(&parent);
|
|
ast_remove(decls);
|
|
ast_remove(decls+1);
|
|
ast_remove(decls+2);
|
|
ast_remove(decls+3);
|
|
ast_remove(decls+4);
|
|
ast_remove(decls+5);
|
|
ast_remove(decls+6);
|
|
assert(parent.first == 0);
|
|
assert(parent.last == 0);
|
|
}
|
|
|
|
|