Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include "ast.h"
  2. #include <stdio.h>
  3.  
  4. void print_spaces( int n ) {
  5.     printf("\n");
  6.     for(int i = 0; i < n; ++i) {
  7.         printf(" ");
  8.     }
  9. }
  10.  
  11. static void print_native_type( enum native_types type ) {
  12.     switch( type ) {
  13.         case INT:
  14.             printf("INT");
  15.         case CHAR:
  16.             printf("CHAR");
  17.         case FLOAT:
  18.             printf("FLOAT");
  19.         case BOOL:
  20.             printf("BOOL");
  21.     }
  22. }
  23.  
  24. static void print_type( struct type* type) {
  25.     if( type != NULL ) {
  26.         if( type->tag == SINGLE ) {
  27.             print_native_type( type->single.type );
  28.         }
  29.         else {
  30.             printf("[");
  31.             print_type( type->seq.next );
  32.             printf("]");
  33.         }
  34.     }
  35. }
  36.  
  37. void print_var( int n_spaces, struct var *v ) {
  38.     if( v != NULL ) {
  39.         print_spaces( n_spaces );
  40.         print_type( v->type );
  41.         printf(" %s\n", v->name );
  42.         print_var( n_spaces, v->next );
  43.     }
  44. }
  45.  
  46. void print_params( struct param* param ) {
  47.     if( param == NULL ) return;
  48.     printf("%s ", param->name );
  49.     print_type( param->type );
  50.     if( param->next != NULL ) {
  51.         printf(", ");
  52.         print_params( param->next );
  53.     }
  54. }
  55.  
  56. void print_func( int n_spaces, struct func* f ) {
  57.     if( f != NULL ) {
  58.         print_spaces( n_spaces );
  59.         printf("FUNC< ");
  60.         print_type( f->type );
  61.         printf(" > ");
  62.         printf(" %s, ", f->name );
  63.         printf("PARAMS = {");
  64.         print_params( f->param );
  65.         printf("}\n");
  66.  
  67.         print_func( n_spaces, f->next );
  68.     }
  69. }
  70.  
  71. void print_tree() {
  72.     print_var( GLOBAL_TREE->vars );
  73.     print_func( GLOBAL_TREE->funcs );
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement