113 lines
2.1 KiB
C
113 lines
2.1 KiB
C
#ifndef SYMBOL_TABLE_H_INCLUDED
|
|
#define SYMBOL_TABLE_H_INCLUDED
|
|
|
|
#include <stddef.h>
|
|
|
|
#define SYMBOL_TABLE_SIZE 100
|
|
|
|
enum Type {
|
|
TYPE_CONST,
|
|
TYPE_INT
|
|
};
|
|
|
|
typedef struct SymbolItem {
|
|
enum Type type;
|
|
char *name;
|
|
int address;
|
|
int init;
|
|
} SymbolItem;
|
|
|
|
/**
|
|
* Stores temp variables at the end of the table
|
|
*/
|
|
typedef struct SymbolTable {
|
|
struct SymbolItem table[SYMBOL_TABLE_SIZE];
|
|
int index;
|
|
int temp_index;
|
|
} SymbolTable;
|
|
|
|
/**
|
|
* Initializes the table's index to 0
|
|
*
|
|
* @param table
|
|
*/
|
|
void init_table(SymbolTable *table);
|
|
|
|
/**
|
|
* Initializes the symbol
|
|
*
|
|
* @param table
|
|
*/
|
|
void init_symbol_item(SymbolItem *symbol_item, size_t name_size);
|
|
|
|
/**
|
|
* Checks if the given symbol is in the table
|
|
*
|
|
* @param table
|
|
* @param name
|
|
* @return
|
|
*/
|
|
int has_symbol(SymbolTable* table, char *name);
|
|
|
|
/**
|
|
* Adds the given symbol to the table.
|
|
*
|
|
* @param table
|
|
* @param type
|
|
* @param name
|
|
* @return 0 on success, -1 if the symbol already exists, -2 if the table is full
|
|
*/
|
|
int add_symbol(SymbolTable* table, enum Type type, char *name);
|
|
|
|
/**
|
|
* Adds the given symbol to temporary variables space
|
|
* Temporary variables must start with _
|
|
*
|
|
* @param table
|
|
* @param type
|
|
* @param name
|
|
* @return 0 on success, -1 if the table is full
|
|
*/
|
|
const SymbolItem* add_temp_symbol(SymbolTable* table, enum Type type);
|
|
|
|
/**
|
|
* Marks the given symbol as initialized
|
|
*
|
|
* @param table
|
|
* @param name
|
|
* @return 0 on success, -1 if the symbol was not found, -2 if the symbol was already initialized
|
|
*/
|
|
int mark_symbol_initialized(SymbolTable* table, char *name);
|
|
|
|
/**
|
|
* Prints the whole table to the console
|
|
*
|
|
* @param table
|
|
*/
|
|
void print_table(SymbolTable* table);
|
|
|
|
/**
|
|
* Prints the given item to the console
|
|
*
|
|
* @param item
|
|
*/
|
|
void print_item(struct SymbolItem *item);
|
|
|
|
/**
|
|
* Converts a Type to a string
|
|
*
|
|
* @param type
|
|
* @return
|
|
*/
|
|
char* type_to_string(enum Type type);
|
|
|
|
/**
|
|
* Gets the table line for the given symbol name
|
|
*
|
|
* @param table
|
|
* @param name
|
|
* @return the item if found, NULL otherwise
|
|
*/
|
|
SymbolItem *get_symbol_item(SymbolTable* table, const char *name);
|
|
|
|
#endif /* !SYMBOL_TABLE_H_INCLUDED */
|