C codegen

This commit is contained in:
Krzosa Karol
2022-05-03 20:08:13 +02:00
parent 8c04044ea2
commit 557dde1936
11 changed files with 573 additions and 281 deletions

64
ast.h
View File

@@ -1,7 +1,9 @@
typedef struct Decl_Enum_Child Decl_Enum_Child;
typedef struct Decl_Function_Arg Decl_Function_Arg;
typedef struct Typespec Typespec;
typedef struct Decl Decl;
typedef struct Note Note;
typedef struct Stmt Stmt;
//-----------------------------------------------------------------------------
// Type specifier
@@ -12,7 +14,6 @@ typedef enum Typespec_Kind{
TS_Pointer,
TS_Array,
TS_Function,
TS_Struct,
}Typespec_Kind;
struct Typespec{
@@ -21,7 +22,6 @@ struct Typespec{
Token *pos;
union{
Intern_String name;
Decl *struct_spec;
struct{
Typespec *first;
Typespec *last;
@@ -58,9 +58,22 @@ typedef enum Decl_Kind{
DECL_Enum,
DECL_Variable,
DECL_Typedef,
DECL_Function,
DECL_List,
}Decl_Kind;
typedef enum Decl_Struct_Kind{
STRUCT_Base ,
STRUCT_Nested,
}Decl_Struct_Kind;
struct Decl_Function_Arg{
Decl_Function_Arg *next;
Intern_String name;
Typespec *typespec;
Token *pos;
};
struct Decl_Enum_Child{
Decl_Enum_Child *next;
Intern_String name;
@@ -73,9 +86,10 @@ struct Decl_Enum_Child{
struct Decl{
Decl_Kind kind;
Decl *next;
Intern_String name;
Token *pos;
Decl *next;
Note *first_note;
Note *last_note;
@@ -87,6 +101,7 @@ struct Decl{
Typespec *typespec;
}enum_decl;
struct{
Decl_Struct_Kind kind;
Decl *first;
Decl *last;
} struct_decl;
@@ -97,6 +112,11 @@ struct Decl{
struct{
Typespec *type;
}typedef_decl;
struct{
Decl_Function_Arg *first;
Decl_Function_Arg *last ;
Typespec *ret;
}function_decl;
struct{
Decl *first;
Decl *last;
@@ -104,3 +124,41 @@ struct Decl{
};
};
//-----------------------------------------------------------------------------
// Statements
//-----------------------------------------------------------------------------
typedef enum Stmt_Kind{
STMT_None,
STMT_Decl,
STMT_Expr,
STMT_Return,
STMT_If,
STMT_For,
}Stmt_Kind;
typedef struct Stmt_List Stmt_List;
struct Stmt_List{
Stmt *first;
Stmt *last;
};
typedef struct Stmt_If Stmt_If;
struct Stmt_If{
Stmt_If *next;
Expr *expr;
union{
struct{Stmt *first; Stmt *last;};
Stmt_List *list;
};
};
struct Stmt{
Stmt_Kind kind;
Stmt *next;
union{
Decl *decl;
Expr *expr;
Stmt_If *if_stmt;
Stmt_List *list;
};
};