From 4f750853485ab10ec3c081bb5584981023a28871 Mon Sep 17 00:00:00 2001 From: Curle Date: Mon, 23 Nov 2020 19:12:13 +0000 Subject: [PATCH] Refactor symbols, enforce size argument. Fixes string parsing. tests/strings now runs flawlessly. --- include/Defs.h | 4 ++-- src/Assembler.c | 4 ++-- src/Main.c | 4 ++-- src/Statements.c | 4 ++-- src/Symbols.c | 10 +++++----- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/Defs.h b/include/Defs.h index 748b072..8f96c6f 100644 --- a/include/Defs.h +++ b/include/Defs.h @@ -273,9 +273,9 @@ struct ASTNode* PrintStatement(void); 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); diff --git a/src/Assembler.c b/src/Assembler.c index 08acaf3..c94a504 100644 --- a/src/Assembler.c +++ b/src/Assembler.c @@ -290,7 +290,7 @@ void AsJmp(int Label) { void AsLabel(int Label) { printf("\tCreating label %d\n", Label); - fprintf(OutputFile, "L%d:\n", Label); + fprintf(OutputFile, "\nL%d:\n", Label); } int AsNewString(char* Value) { @@ -508,7 +508,7 @@ void AsNewSymb(int ID) { "\t.globl\t%s\n", 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++) { switch(TypeSize) { diff --git a/src/Main.c b/src/Main.c index ada9294..effd54e 100644 --- a/src/Main.c +++ b/src/Main.c @@ -80,8 +80,8 @@ int main(int argc, char* argv[]) { exit(1); } - AddFunctionSymbol("PrintInteger", RET_CHAR, ST_FUNC, 0); - AddFunctionSymbol("PrintString", RET_CHAR, ST_FUNC, 1); + AddFunctionSymbol("PrintInteger", RET_CHAR, ST_FUNC, 0, 1); + AddFunctionSymbol("PrintString", RET_CHAR, ST_FUNC, 1, 1); //AddSymbol("forgecord", PTR_CHAR, ST_VAR); Tokenise(&CurrentToken); diff --git a/src/Statements.c b/src/Statements.c index c53e832..c229d88 100644 --- a/src/Statements.c +++ b/src/Statements.c @@ -42,7 +42,7 @@ void BeginVariableDeclaration(int Type) { while(1) { //printf("Identifier: %s\n", CurrentIdentifier); 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); if(CurrentToken.type == LI_SEMIC) { @@ -74,7 +74,7 @@ struct ASTNode* ParseFunction(int Type) { BreakLabel = NewLabel(); - SymbolSlot = AddFunctionSymbol(CurrentIdentifier, Type, ST_FUNC, BreakLabel); + SymbolSlot = AddFunctionSymbol(CurrentIdentifier, Type, ST_FUNC, BreakLabel, 1); CurrentFunction = SymbolSlot; VerifyToken(LI_LPARE, "("); diff --git a/src/Symbols.c b/src/Symbols.c index 7bffa14..267e02d 100644 --- a/src/Symbols.c +++ b/src/Symbols.c @@ -45,19 +45,18 @@ static int NewSymbol(void) { int AddArraySymbol(char* Name, int Type, int Structure, int EndLabel, int Size) { int Slot; - Slot = AddFunctionSymbol(Name, Type, Structure, EndLabel); - Symbols[Slot].Length = Size; + Slot = AddFunctionSymbol(Name, Type, Structure, EndLabel, Size); 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; - Slot = AddSymbol(Name, Type, Structure); + Slot = AddSymbol(Name, Type, Structure, Size); Symbols[Slot].EndLabel = EndLabel; return Slot; } -int AddSymbol(char* Name, int Type, int Structure) { +int AddSymbol(char* Name, int Type, int Structure, int Size) { int TableSlot; @@ -69,6 +68,7 @@ int AddSymbol(char* Name, int Type, int Structure) { 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;