Erythro/src/Symbols.c

70 lines
1.5 KiB
C
Raw Normal View History

/*************/
/*GEMWIRE */
/* ERYTHRO*/
/*************/
#include <Defs.h>
#include <Data.h>
static int GlobalSymbols = 0;
static char* Types[4] = { "none", "char", "int", "void" };
/*
* Find the position of a symbol in the symbol table.
* @Return the index into the symbol table if found,
* -1 if not found.
*/
int FindSymbol(char* Symbol) {
int Ind;
for(Ind = 0; Ind < GlobalSymbols; Ind++) {
if(*Symbol == *Symbols[Ind].Name && !strcmp(Symbol, Symbols[Ind].Name))
return Ind;
}
return -1;
}
/*
* Append a new entry to the table of global symbols.
* @Return the index to the new entry
*
* Will kill the program if we run out.
* //TODO: Dump symbols on death?
*/
static int NewSymbol(void) {
int Pos;
if((Pos = GlobalSymbols++) >= SYMBOLS)
Die("Too many symbols");
return Pos;
}
// TODO: this is going weird!
int AddFunctionSymbol(char* Name, int Type, int Structure, int EndLabel) {
int Slot;
Slot = AddSymbol(Name, Type, Structure);
Symbols[Slot].EndLabel = EndLabel;
return Slot;
}
int AddSymbol(char* Name, int Type, int Structure) {
int TableSlot;
if((TableSlot = FindSymbol(Name) != -1))
return TableSlot;
TableSlot = NewSymbol();
Symbols[TableSlot].Name = strdup(Name);
Symbols[TableSlot].Type = Type;
Symbols[TableSlot].Structure = Structure;
//printf("Adding new variable %s of type %s to the table at %d\n", CurrentIdentifier, Types[Type], TableSlot);
return TableSlot;
}