le compilateur super performant
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

symbol_table.h 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef SYMBOL_TABLE_H_INCLUDED
  2. #define SYMBOL_TABLE_H_INCLUDED
  3. #define SYMBOL_TABLE_SIZE 100
  4. enum Type {
  5. TYPE_CONST,
  6. TYPE_INT
  7. };
  8. typedef struct SymbolItem {
  9. enum Type type;
  10. char *name;
  11. int address;
  12. int init;
  13. } SymbolItem;
  14. /**
  15. * Stores temp variables at the end of the table
  16. */
  17. typedef struct SymbolTable {
  18. struct SymbolItem table[SYMBOL_TABLE_SIZE];
  19. int index;
  20. int temp_index;
  21. } SymbolTable;
  22. /**
  23. * Initializes the table's index to 0
  24. *
  25. * @param table
  26. */
  27. void init_table(SymbolTable *table);
  28. /**
  29. * Checks if the given symbol is in the table
  30. *
  31. * @param table
  32. * @param name
  33. * @return
  34. */
  35. int has_symbol(SymbolTable* table, char *name);
  36. /**
  37. * Adds the given symbol to the table.
  38. *
  39. * @param table
  40. * @param type
  41. * @param name
  42. * @return 0 on success, -1 if the symbol already exists, -2 if the table is full
  43. */
  44. int add_symbol(SymbolTable* table, enum Type type, char *name);
  45. /**
  46. * Adds the given symbol to temporary variables space
  47. * Temporary variables must start with _
  48. *
  49. * @param table
  50. * @param type
  51. * @param name
  52. * @return 0 on success, -1 if the symbol already exists, -2 if the table is full
  53. * -3 if the name doesn't start by _
  54. */
  55. int add_temp_symbol(SymbolTable* table, enum Type type, char *name);
  56. /**
  57. * Marks the given symbol as initialized
  58. *
  59. * @param table
  60. * @param name
  61. * @return 0 on success, -1 if the symbol was not found, -2 if the symbol was already initialized
  62. */
  63. int mark_symbol_initialized(SymbolTable* table, char *name);
  64. /**
  65. * Prints the whole table to the console
  66. *
  67. * @param table
  68. */
  69. void print_table(SymbolTable* table);
  70. /**
  71. * Prints the given item to the console
  72. *
  73. * @param item
  74. */
  75. void print_item(struct SymbolItem *item);
  76. /**
  77. * Converts a Type to a string
  78. *
  79. * @param type
  80. * @return
  81. */
  82. char* type_to_string(enum Type type);
  83. /**
  84. * Gets the table line for the given symbol name
  85. *
  86. * @param table
  87. * @param name
  88. * @return the item if found, NULL otherwise
  89. */
  90. SymbolItem *get_symbol_item(SymbolTable* table, char *name);
  91. #endif /* !SYMBOL_TABLE_H_INCLUDED */