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.1KB

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