Almost works
This commit is contained in:
70
new_print.c
70
new_print.c
@@ -5,6 +5,11 @@ global S64 indent;
|
||||
|
||||
#define println(...) do{ printf("\n"); print_indent(); printf(__VA_ARGS__); }while(0)
|
||||
|
||||
|
||||
#define AST_CAST(item, T) T *item = (T *)item; assert(item->kind == AST_##Kind
|
||||
#define AST_Iter(parent, name, T) for(T *name = (T *)(parent)->first; name; name=(T *)name->next) // , assert(name->kind == AST_##type
|
||||
|
||||
|
||||
function void
|
||||
print_indent(){
|
||||
for(S64 i = 0; i < indent*2; i++)
|
||||
@@ -190,76 +195,81 @@ print_assign_expr(Expr *expr){
|
||||
}
|
||||
|
||||
function void
|
||||
decl_print(Decl *node){
|
||||
ast_print(AST *in){
|
||||
//print_notes(p, node->first_note);
|
||||
|
||||
switch(node->kind) {
|
||||
|
||||
case DECL_List: {
|
||||
for(Decl *n = node->list_decl.first; n; n=n->next){
|
||||
decl_print(n);
|
||||
switch(in->kind) {
|
||||
case AST_Program: {
|
||||
AST_Parent *node = (AST_Parent *)in;
|
||||
for(AST *n = node->first; n; n=n->next){
|
||||
ast_print(n);
|
||||
printf("\n");
|
||||
}
|
||||
} break;
|
||||
|
||||
case DECL_Const:
|
||||
case DECL_Variable:{
|
||||
case AST_Decl_Const:
|
||||
case AST_Decl_Var:{
|
||||
Decl_Var *node = (Decl_Var *)in;
|
||||
println("%s: ", node->name.s.str);
|
||||
if(node->var_decl.typespec) typespec_print(node->var_decl.typespec);
|
||||
print_assign_expr(node->var_decl.expr);
|
||||
if(node->typespec) typespec_print(node->typespec);
|
||||
print_assign_expr(node->expr);
|
||||
} break;
|
||||
|
||||
case DECL_Typedef:{
|
||||
case AST_Decl_Typedef:{
|
||||
Decl_Typedef *node = (Decl_Typedef *)in;
|
||||
println("typedef %s ", node->name.s.str);
|
||||
typespec_print(node->typedef_decl.typespec);
|
||||
typespec_print(node->typespec);
|
||||
printf(";");
|
||||
} break;
|
||||
|
||||
case DECL_Function:{
|
||||
case AST_Decl_Func:{
|
||||
Decl_Func *node = (Decl_Func *)in;
|
||||
println("");
|
||||
typespec_print(node->func_decl.ret);
|
||||
typespec_print(node->ret);
|
||||
printf(" %s", node->name.s.str);
|
||||
printf("(");
|
||||
for(Decl_Function_Arg *arg = node->func_decl.first; arg; arg=arg->next){
|
||||
AST_Iter(node, arg, Decl_Func_Arg){
|
||||
printf("%s: ", arg->name.s.str);
|
||||
typespec_print(arg->typespec);
|
||||
if(arg != node->func_decl.last)
|
||||
if((AST *)arg != node->last)
|
||||
printf(", ");
|
||||
}
|
||||
printf(");");
|
||||
} break;
|
||||
|
||||
case DECL_Struct:
|
||||
case DECL_Union :{
|
||||
const char *struct_name = node->kind==DECL_Struct ? "struct" : "union";
|
||||
case AST_Decl_Struct:
|
||||
case AST_Decl_Union :{
|
||||
Decl_Struct *node = (Decl_Struct *)in;
|
||||
const char *struct_name = node->kind==AST_Decl_Struct ? "struct" : "union";
|
||||
println("%s %s{", struct_name, node->name.s.str?(char*)node->name.s.str:"");
|
||||
indent++;
|
||||
for(Decl *n = node->struct_decl.first; n; n=n->next){
|
||||
decl_print(n);
|
||||
for(AST *n = node->first; n; n=n->next){
|
||||
ast_print(n);
|
||||
}
|
||||
indent--;
|
||||
println("};");
|
||||
} break;
|
||||
|
||||
case DECL_SubStruct:
|
||||
case DECL_SubUnion :{
|
||||
const char *struct_name = node->kind==DECL_SubStruct ? "struct" : "union";
|
||||
case AST_Decl_SubStruct:
|
||||
case AST_Decl_SubUnion :{
|
||||
Decl_Struct *node = (Decl_Struct *)in;
|
||||
const char *struct_name = node->kind==AST_Decl_Struct ? "struct" : "union";
|
||||
println("%s %s{", struct_name, node->name.s.str?(char*)node->name.s.str:"");
|
||||
indent++;
|
||||
for(Decl *n = node->struct_decl.first; n; n=n->next){
|
||||
decl_print(n);
|
||||
for(AST *n = node->first; n; n=n->next){
|
||||
ast_print(n);
|
||||
}
|
||||
indent--;
|
||||
println("};");
|
||||
} break;
|
||||
|
||||
|
||||
case DECL_Enum:{
|
||||
case AST_Decl_Enum:{
|
||||
Decl_Enum *node = (Decl_Enum *)in;
|
||||
println("enum %s : ", node->name.s.str);
|
||||
typespec_print(node->enum_decl.typespec);
|
||||
typespec_print(node->typespec);
|
||||
printf("{");
|
||||
indent++;
|
||||
for(Decl_Enum_Child *n = node->enum_decl.first; n; n=n->next){
|
||||
AST_Iter(node, n, Decl_Enum_Child){
|
||||
//print_notes(p, n->first_note);
|
||||
println("%s", n->name.s.str);
|
||||
print_assign_expr(n->expr);
|
||||
|
||||
Reference in New Issue
Block a user