Refactor symbols, enforce size argument.

Fixes string parsing. tests/strings now runs flawlessly.
This commit is contained in:
Curle 2020-11-23 19:12:13 +00:00
parent 86b1688035
commit 4f75085348
Signed by: TheCurle
GPG Key ID: 5942F13718443F79
5 changed files with 13 additions and 13 deletions

View File

@ -273,9 +273,9 @@ struct ASTNode* PrintStatement(void);
int FindSymbol(char* Symbol); int FindSymbol(char* Symbol);
int AddSymbol(char* Name, int Type, int Structure); int AddSymbol(char* Name, int Type, int Structure, int Size);
int AddFunctionSymbol(char* Name, int Type, int Structure, int EndLabel); int AddFunctionSymbol(char* Name, int Type, int Structure, int EndLabel, int Size);
int AddArraySymbol(char* Name, int Type, int Structure, int EndLabel, int Size); int AddArraySymbol(char* Name, int Type, int Structure, int EndLabel, int Size);

View File

@ -290,7 +290,7 @@ void AsJmp(int Label) {
void AsLabel(int Label) { void AsLabel(int Label) {
printf("\tCreating label %d\n", Label); printf("\tCreating label %d\n", Label);
fprintf(OutputFile, "L%d:\n", Label); fprintf(OutputFile, "\nL%d:\n", Label);
} }
int AsNewString(char* Value) { int AsNewString(char* Value) {
@ -508,7 +508,7 @@ void AsNewSymb(int ID) {
"\t.globl\t%s\n", "\t.globl\t%s\n",
Symbols[ID].Name); Symbols[ID].Name);
fprintf(OutputFile, "%s:", Symbols[ID].Name); fprintf(OutputFile, "%s:\n", Symbols[ID].Name);
for(int i = 0; i < Symbols[ID].Length; i++) { for(int i = 0; i < Symbols[ID].Length; i++) {
switch(TypeSize) { switch(TypeSize) {

View File

@ -80,8 +80,8 @@ int main(int argc, char* argv[]) {
exit(1); exit(1);
} }
AddFunctionSymbol("PrintInteger", RET_CHAR, ST_FUNC, 0); AddFunctionSymbol("PrintInteger", RET_CHAR, ST_FUNC, 0, 1);
AddFunctionSymbol("PrintString", RET_CHAR, ST_FUNC, 1); AddFunctionSymbol("PrintString", RET_CHAR, ST_FUNC, 1, 1);
//AddSymbol("forgecord", PTR_CHAR, ST_VAR); //AddSymbol("forgecord", PTR_CHAR, ST_VAR);
Tokenise(&CurrentToken); Tokenise(&CurrentToken);

View File

@ -42,7 +42,7 @@ void BeginVariableDeclaration(int Type) {
while(1) { while(1) {
//printf("Identifier: %s\n", CurrentIdentifier); //printf("Identifier: %s\n", CurrentIdentifier);
printf("Adding symbol %s of type %s.\n", CurrentIdentifier, TypeNames[Type]); printf("Adding symbol %s of type %s.\n", CurrentIdentifier, TypeNames[Type]);
ID = AddSymbol(CurrentIdentifier, Type, ST_VAR); ID = AddSymbol(CurrentIdentifier, Type, ST_VAR, TypeSizes[Type]);
AsNewSymb(ID); AsNewSymb(ID);
if(CurrentToken.type == LI_SEMIC) { if(CurrentToken.type == LI_SEMIC) {
@ -74,7 +74,7 @@ struct ASTNode* ParseFunction(int Type) {
BreakLabel = NewLabel(); BreakLabel = NewLabel();
SymbolSlot = AddFunctionSymbol(CurrentIdentifier, Type, ST_FUNC, BreakLabel); SymbolSlot = AddFunctionSymbol(CurrentIdentifier, Type, ST_FUNC, BreakLabel, 1);
CurrentFunction = SymbolSlot; CurrentFunction = SymbolSlot;
VerifyToken(LI_LPARE, "("); VerifyToken(LI_LPARE, "(");

View File

@ -45,19 +45,18 @@ static int NewSymbol(void) {
int AddArraySymbol(char* Name, int Type, int Structure, int EndLabel, int Size) { int AddArraySymbol(char* Name, int Type, int Structure, int EndLabel, int Size) {
int Slot; int Slot;
Slot = AddFunctionSymbol(Name, Type, Structure, EndLabel); Slot = AddFunctionSymbol(Name, Type, Structure, EndLabel, Size);
Symbols[Slot].Length = Size;
return Slot; return Slot;
} }
int AddFunctionSymbol(char* Name, int Type, int Structure, int EndLabel) { int AddFunctionSymbol(char* Name, int Type, int Structure, int EndLabel, int Size) {
int Slot; int Slot;
Slot = AddSymbol(Name, Type, Structure); Slot = AddSymbol(Name, Type, Structure, Size);
Symbols[Slot].EndLabel = EndLabel; Symbols[Slot].EndLabel = EndLabel;
return Slot; return Slot;
} }
int AddSymbol(char* Name, int Type, int Structure) { int AddSymbol(char* Name, int Type, int Structure, int Size) {
int TableSlot; int TableSlot;
@ -69,6 +68,7 @@ int AddSymbol(char* Name, int Type, int Structure) {
Symbols[TableSlot].Name = strdup(Name); Symbols[TableSlot].Name = strdup(Name);
Symbols[TableSlot].Type = Type; Symbols[TableSlot].Type = Type;
Symbols[TableSlot].Structure = Structure; 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); //printf("Adding new variable %s of type %s to the table at %d\n", CurrentIdentifier, Types[Type], TableSlot);
return TableSlot; return TableSlot;