Erythro/src/Symbols.c

75 lines
1.7 KiB
C
Raw Normal View History

/*************/
/*GEMWIRE */
/* ERYTHRO*/
/*************/
#include <Defs.h>
#include <Data.h>
static int GlobalSymbols = 0;
/*
* 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!
2020-11-18 20:49:08 +00:00
int AddArraySymbol(char* Name, int Type, int Structure, int EndLabel, int Size) {
int Slot;
Slot = AddFunctionSymbol(Name, Type, Structure, EndLabel, Size);
2020-11-18 20:49:08 +00:00
return Slot;
}
int AddFunctionSymbol(char* Name, int Type, int Structure, int EndLabel, int Size) {
int Slot;
Slot = AddSymbol(Name, Type, Structure, Size);
Symbols[Slot].EndLabel = EndLabel;
return Slot;
}
int AddSymbol(char* Name, int Type, int Structure, int Size) {
int TableSlot;
if((TableSlot = FindSymbol(Name) != -1))
return TableSlot;
TableSlot = NewSymbol();
Symbols[TableSlot].Name = strdup(Name);
Symbols[TableSlot].Type = Type;
Symbols[TableSlot].Structure = Structure;
Symbols[TableSlot].Length = Size;
//printf("Adding new variable %s of type %s to the table at %d\n", CurrentIdentifier, Types[Type], TableSlot);
return TableSlot;
}