Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 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->tag == SINGLE ) {
  26.         print_native_type( type->single.type );
  27.     }
  28.     else {
  29.         printf("[");
  30.         print_type( type->seq.next );
  31.         printf("]");
  32.     }
  33. }
  34.  
  35. void print_var( int n_spaces, struct var *v ) {
  36.     print_spaces( n_spaces );
  37.     print_type( v->type );
  38.     printf(" %s\n", v->name );
  39.     if( v->next != NULL ) {
  40.         print_var( n_spaces, v->next );
  41.     }
  42. }
  43.  
  44. void print_params( struct param* param ) {
  45.     print_type( param->type );
  46.     if( param->next != NULL ) {
  47.         printf(", ");
  48.         print_params( param->next );
  49.     }
  50. }
  51.  
  52. void print_func( int n_spaces, struct func* f ) {
  53.     print_spaces( n_spaces );
  54.     printf("FUNC< ");
  55.     print_type( f->type );
  56.     printf(" > ");
  57.     printf(" %s, ", f->name );
  58.     printf("PARAMS = {");
  59.     print_params( f->param );
  60.     printf("}\n");
  61.  
  62.     if( f->next != NULL ) {
  63.         print_func( n_spaces, f->next );
  64.     }
  65.  
  66. }
  67.  
  68. void print_tree() {
  69.     print_var( GLOBAL_TREE->vars );
  70.     print_func( GLOBAL_TREE->funcs );
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement