C codegen
This commit is contained in:
74
ast.c
74
ast.c
@@ -1,4 +1,6 @@
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Decls
|
||||
//-----------------------------------------------------------------------------
|
||||
function Decl *
|
||||
decl_new(Parser *p, Decl_Kind kind, Token *pos, Intern_String name){
|
||||
Decl *result = arena_push_struct(&p->main_arena, Decl);
|
||||
@@ -9,9 +11,10 @@ decl_new(Parser *p, Decl_Kind kind, Token *pos, Intern_String name){
|
||||
}
|
||||
|
||||
function Decl *
|
||||
decl_struct(Parser *p, Decl_Kind kind, Token *pos, Intern_String name){
|
||||
decl_struct(Parser *p, Decl_Kind kind, Token *pos, Intern_String name, Decl_Struct_Kind struct_kind){
|
||||
assert(kind == DECL_Struct || kind == DECL_Union);
|
||||
Decl *result = decl_new(p, kind, pos, name);
|
||||
result->struct_decl.kind = struct_kind;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -37,6 +40,23 @@ decl_variable(Parser *p, Token *pos, Intern_String name, Typespec *typespec, Exp
|
||||
return result;
|
||||
}
|
||||
|
||||
function Decl *
|
||||
decl_function(Parser *p, Token *pos, Intern_String name, Typespec *ret){
|
||||
Decl *result = decl_new(p, DECL_Function, pos, name);
|
||||
result->function_decl.ret = ret;
|
||||
return result;
|
||||
}
|
||||
|
||||
function void
|
||||
decl_function_push(Parser *p, Decl *parent, Token *pos, Intern_String name, Typespec *type){
|
||||
assert(parent->kind == DECL_Function);
|
||||
Decl_Function_Arg *result = arena_push_struct(&p->main_arena, Decl_Function_Arg);
|
||||
result->name = name;
|
||||
result->typespec = type;
|
||||
result->pos = pos;
|
||||
SLLQueuePush(parent->function_decl.first, parent->function_decl.last, result);
|
||||
}
|
||||
|
||||
function void
|
||||
decl_enum_push(Parser *p, Decl *parent, Token *pos, Intern_String name, Expr *expr, Note *notes){
|
||||
assert(parent->kind == DECL_Enum);
|
||||
@@ -61,6 +81,15 @@ decl_list_push(Decl *parent, Decl *child){
|
||||
SLLQueuePush(parent->list.first, parent->list.last, child);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// 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(Parser *p, Note *parent, Token *pos, Intern_String name, Expr *expr){
|
||||
Note *result = arena_push_struct(&p->main_arena, Note);
|
||||
@@ -71,12 +100,38 @@ note_push_new(Parser *p, Note *parent, Token *pos, Intern_String name, Expr *exp
|
||||
return result;
|
||||
}
|
||||
|
||||
function void
|
||||
decl_pass_notes(Decl *a, Note *b){
|
||||
a->first_note = b->first;
|
||||
a->last_note = b->last;
|
||||
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);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Typespec
|
||||
//-----------------------------------------------------------------------------
|
||||
function Typespec *
|
||||
typespec_new(Parser *p, Typespec_Kind kind, Token *pos){
|
||||
Typespec *result = arena_push_struct(&p->main_arena, Typespec);
|
||||
@@ -85,13 +140,6 @@ typespec_new(Parser *p, Typespec_Kind kind, Token *pos){
|
||||
return result;
|
||||
}
|
||||
|
||||
function Typespec *
|
||||
typespec_struct(Parser *p, Token *pos, Decl *aggregate){
|
||||
Typespec *result = typespec_new(p, TS_Struct, pos);
|
||||
result->struct_spec = aggregate;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Typespec *
|
||||
typespec_name(Parser *p, Token *pos, Intern_String name){
|
||||
Typespec *result = typespec_new(p, TS_Name, pos);
|
||||
|
||||
Reference in New Issue
Block a user