Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 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. break;
  16. case CHAR:
  17. printf("CHAR");
  18. break;
  19. case FLOAT:
  20. printf("FLOAT");
  21. break;
  22. case BOOL:
  23. printf("BOOL");
  24. break;
  25. default:
  26. fprintf(stderr, "uknown type\n");
  27. exit(-1);
  28. }
  29. }
  30.  
  31. static void print_type( union type* type) {
  32. if( type != NULL ) {
  33. if( type->tag == SINGLE ) {
  34. print_native_type( type->single.type );
  35. }
  36. else {
  37. printf("[");
  38. print_type( type->seq.next );
  39. printf("]");
  40. }
  41. }
  42. }
  43.  
  44. void print_var( int n_spaces, struct var *v ) {
  45. if( v != NULL ) {
  46. print_spaces( n_spaces );
  47. print_type( v->type );
  48. printf(" %s\n", v->name );
  49. print_var( n_spaces, v->next );
  50. }
  51. }
  52.  
  53. void print_params( struct param* param ) {
  54. if( param == NULL ) return;
  55. printf("%s ", param->name );
  56. print_type( param->type );
  57. if( param->next != NULL ) {
  58. printf(", ");
  59. print_params( param->next );
  60. }
  61. }
  62.  
  63. void print_func( int n_spaces, struct func* f ) {
  64. if( f != NULL ) {
  65. print_spaces( n_spaces );
  66. printf("FUNC< ");
  67. print_type( f->type );
  68. printf(" > ");
  69. printf(" %s, ", f->name );
  70. printf("PARAMS = {");
  71. print_params( f->param );
  72. printf("}\n");
  73.  
  74. print_func( n_spaces, f->next );
  75. }
  76. }
  77.  
  78. void print_tree() {
  79. print_var( 4, GLOBAL_TREE->vars );
  80. print_func( 4, GLOBAL_TREE->funcs );
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement