Almost works

This commit is contained in:
Krzosa Karol
2022-05-07 20:56:03 +02:00
parent 042127239e
commit feae74b0b9
5 changed files with 477 additions and 595 deletions

652
new_ast.c
View File

@@ -11,79 +11,6 @@ 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
//-----------------------------------------------------------------------------
@@ -232,6 +159,116 @@ struct Typespec{
};
};
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
typedef enum AST_Kind{
AST_None,
AST_Decl_Enum_Child,
AST_Decl_Var,
AST_Decl_Const,
AST_Decl_Typedef,
AST_Decl_Func_Arg,
AST_Decl_Variable,
AST_Decl_Struct, // //
AST_Decl_SubStruct,
AST_Decl_SubUnion,
AST_Decl_Union, //
AST_Decl_Enum,
AST_Decl_Func,
AST_Program,
AST_Note,
AST_Note_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_FirstStruct = AST_Decl_Struct,
AST_LastStruct = AST_Decl_Union,
}AST_Kind;
#define AST_FIELDS AST *next, *prev; AST_Parent *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_FIELDS; AST_PARENT_FIELDS; }; AST_Parent child; }
struct AST{
AST_FIELDS;
};
typedef struct AST_Parent{
AST_UNION;
AST_PARENT_FIELDS;
} AST_Parent;
typedef AST_Parent Note_List;
typedef AST_Parent Program;
typedef struct Note{
AST_PARENT_UNION;
Intern_String name;
Expr *expr;
}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 Decl_Enum_Child{
AST_UNION;
Intern_String name;
Expr *expr;
}Decl_Enum_Child;
typedef struct Decl_Enum{
AST_PARENT_UNION;
Typespec *typespec;
Intern_String name;
}Decl_Enum;
typedef struct Decl_Func{
AST_PARENT_UNION;
Intern_String name;
Typespec *ret;
AST *body;
}Decl_Func;
typedef struct Decl_Typedef{
AST_PARENT_UNION;
Typespec *typespec;
Intern_String name;
}Decl_Typedef;
typedef struct Decl_Var{
AST_PARENT_UNION;
Typespec *typespec;
Intern_String name;
Expr *expr;
}Decl_Var;
typedef struct Decl_Const{
AST_PARENT_UNION;
Typespec *typespec;
Intern_String name;
Expr *expr;
}Decl_Const;
/*
//-----------------------------------------------------------------------------
// Declarations
//-----------------------------------------------------------------------------
@@ -292,7 +329,9 @@ struct Decl{
}list_decl;
};
};
*/
/*
struct Note{
Token *pos;
Intern_String name;
@@ -317,7 +356,9 @@ struct Decl_Enum_Child{
Note *first_note;
Note *last_note;
};
*/
/*
//-----------------------------------------------------------------------------
// Statements
//-----------------------------------------------------------------------------
@@ -355,6 +396,7 @@ struct Stmt{
};
};
*/
//-----------------------------------------------------------------------------
// Expression constructors
@@ -576,215 +618,10 @@ 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){
@@ -881,6 +718,265 @@ ast_push_before(AST *in_list, AST *node){
node->parent = parent;
}
//-----------------------------------------------------------------------------
// Notes
//-----------------------------------------------------------------------------
/*
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
//-----------------------------------------------------------------------------
#define ast_parent_new(arena, T, pos) (T *)ast__parent_new(arena, sizeof(T), AST_##T, pos)
#define ast_new(arena, T, pos) (T *)ast__new(arena, sizeof(T), AST_##T, pos)
function B32
ast_kind_is_parent(AST_Kind kind){
B32 result = kind >= AST_FirstParent && kind <= AST_LastParent;
return result;
}
function B32
ast_kind_is_struct(AST_Kind kind){
B32 result = kind >= AST_FirstStruct && kind <= AST_LastStruct;
return result;
}
function AST *
ast__new(Arena *p, SizeU size, AST_Kind kind, Token *pos){
assert(!ast_kind_is_parent(kind));
AST *result = arena_push_size(p, size);
result->pos = pos;
result->kind = kind;
return result;
}
function AST_Parent *
ast__parent_new(Arena *p, SizeU size, AST_Kind kind, Token *pos){
assert(ast_kind_is_parent(kind));
AST_Parent *result = arena_push_size(p, size);
result->pos = pos;
result->kind = kind;
return result;
}
function Note *
note_push_new(Arena *p, AST_Parent *parent, Token *pos, Intern_String name, Expr *expr){
Note *result = ast_parent_new(p, Note, pos);
result->name = name;
result->expr = expr;
ast_push_last(parent, (AST *)result);
return result;
}
function Note_List *
note_list(Arena *p, Token *pos){
Note_List *result = ast_parent_new(p, Note_List, pos);
return result;
}
function Decl_Struct *
decl_struct(Arena *p, AST_Kind kind, Token *pos, Intern_String name){
assert(ast_kind_is_struct(kind));
Decl_Struct *result = ast_parent_new(p, Decl_Struct, pos);
result->name = name;
return result;
}
function Decl_Typedef *
decl_typedef(Arena *p, Token *pos, Intern_String name, Typespec *type){
Decl_Typedef *result = ast_new(p, Decl_Typedef, pos);
result->typespec = type;
result->name = name;
return result;
}
function Decl_Const *
decl_const(Arena *arena, Token *pos, Intern_String name, Expr *expr, Typespec *typespec){
Decl_Const *result = ast_new(arena, Decl_Const, pos);
result->expr = expr;
result->name = name;
result->typespec = typespec;
return result;
}
function Decl_Enum *
decl_enum(Arena *p, Token *pos, Intern_String name, Typespec *typespec){
Decl_Enum *result = ast_parent_new(p, Decl_Enum, pos);
result->typespec = typespec;
return result;
}
function Decl_Var *
decl_variable(Arena *arena, Token *pos, Intern_String name, Expr *expr, Typespec *typespec, Note_List *notes){
Decl_Var *result = ast_new(arena, Decl_Var, pos);
result->expr = expr;
result->name = name;
result->typespec = typespec;
result->notes = notes;
return result;
}
function Decl_Func *
decl_function(Arena *p, Token *pos, Intern_String name, Typespec *ret){
Decl_Func *result = ast_parent_new(p, Decl_Func, pos);
result->ret = ret;
return result;
}
function Decl_Func_Arg *
decl_func_arg(Arena *arena, Token *pos, Intern_String name, Typespec *type){
Decl_Func_Arg *result = ast_new(arena, Decl_Func_Arg, pos);
result->name = name;
result->typespec = type;
return result;
}
function Decl_Enum_Child *
decl_enum_child(Arena *p, Token *pos, Intern_String name, Expr *expr, Note_List *note){
Decl_Enum_Child *result = ast_new(p, Decl_Enum_Child, pos);
result->name = name;
result->expr = expr;
result->notes = (AST_Parent *)note;
return result;
}
function void
decl_func_push(Arena *p, Decl_Func *parent, Token *pos, Intern_String name, Typespec *type){
assert(parent->kind == AST_Decl_Func);
Decl_Func_Arg *func_arg = decl_func_arg(p, pos, name, type);
ast_push_last((AST_Parent *)parent, (AST *)func_arg);
}
function void
decl_enum_push(Arena *p, Decl_Enum *parent, Token *pos, Intern_String name, Expr *expr, Note_List *notes){
assert(parent->kind == AST_Decl_Enum);
Decl_Enum_Child *child = decl_enum_child(p, pos, name, expr, notes);
ast_push_last((AST_Parent *)parent, (AST *)child);
}
function void
decl_struct_push(Decl_Struct *parent, AST *child){
assert(ast_kind_is_struct(parent->kind));
ast_push_last((AST_Parent *)parent, child);
}
function Program *
ast_program(Arena *arena, Token *token){
Program *result = ast_parent_new(arena, Program, token);
return result;
}
function void
ast_program_push(AST_Parent *parent, AST *child){
assert(parent->kind == AST_Program);
ast_push_last((AST_Parent *)parent, 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);
}
*/
//-----------------------------------------------------------------------------
// Test stuff
//-----------------------------------------------------------------------------
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_test(){