Fixes to struct parsing

This commit is contained in:
Curle 2021-02-22 17:43:06 +00:00
parent 72358aed9f
commit 8263fb853b
No known key found for this signature in database
GPG Key ID: 58A5C4688ECE6E7C
5 changed files with 41 additions and 22 deletions

View File

@ -51,19 +51,19 @@ char* Suffixate(char* String, char Suffix) {
/* /*
* Starts most of the work to do with the Erythro compiler. * Starts most of the work to do with the Erythro compiler.
* It: * It:
* Opens the input and output files, * Opens the input and output files,
* Parses the global symbols of the file, including function blocks. * Parses the global symbols of the file, including function blocks.
* Generates the assembly representation of the source code * Generates the assembly representation of the source code
* Saves said assembly into the OutputFile * Saves said assembly into the OutputFile
* Returns the name of the file containing the generated assembly. * Returns the name of the file containing the generated assembly.
* Note that the Input file must have a valid extension. * Note that the Input file must have a valid extension.
* For Erythro code, this is .er * For Erythro code, this is .er
* The generated assembly will have the extension .s * The generated assembly will have the extension .s
* *
* @param InputFile: The filename of the Erythro Source code to compile * @param InputFile: The filename of the Erythro Source code to compile
* @return the filename of the generated PECOFF32+ assembly * @return the filename of the generated PECOFF32+ assembly
*/ */
char* Compile(char* InputFile) { char* Compile(char* InputFile) {
char* OutputName; char* OutputName;

View File

@ -511,13 +511,20 @@ struct ASTNode* ParseCompound() {
void ParseGlobals() { void ParseGlobals() {
struct ASTNode* Tree; struct ASTNode* Tree;
struct SymbolTableEntry* Composite;
int Type, FunctionComing; int Type, FunctionComing;
printf("Parsing global definitions\r\n"); printf("Parsing global definitions\r\n");
while(1) { while(1) {
// We loop early if there's a struct, and since a struct may be the last
// thing in a file, we need to check for eof before anything else
if(CurrentToken.type == LI_EOF)
break;
printf("New definition incoming..\r\n\n"); printf("New definition incoming..\r\n\n");
Type = ParseOptionalPointer(NULL); Type = ParseOptionalPointer(&Composite);
//TODO: converge pathways on this block? //TODO: converge pathways on this block?
if(CurrentToken.type == KW_FUNC) { if(CurrentToken.type == KW_FUNC) {
@ -525,6 +532,13 @@ void ParseGlobals() {
FunctionComing = 1; FunctionComing = 1;
} }
// Structs are parsed fully in ParseOptionalPointer
// TODO: FIX THAT!!
if(Type == DAT_STRUCT && CurrentToken.type == LI_SEMIC) {
Tokenise();
continue;
}
VerifyToken(TY_IDENTIFIER, "ident"); VerifyToken(TY_IDENTIFIER, "ident");
if(FunctionComing && CurrentToken.type == LI_LPARE) { if(FunctionComing && CurrentToken.type == LI_LPARE) {
@ -539,12 +553,9 @@ void ParseGlobals() {
} }
} else { } else {
printf("\tParsing global variable declaration\n"); printf("\tParsing global variable declaration\n");
BeginVariableDeclaration(Type, NULL, SC_GLOBAL); BeginVariableDeclaration(Type, Composite, SC_GLOBAL);
VerifyToken(LI_SEMIC, ";"); VerifyToken(LI_SEMIC, ";");
} }
if(CurrentToken.type == LI_EOF)
break;
} }
} }

View File

@ -104,12 +104,12 @@ int ParseOptionalPointer(struct SymbolTableEntry** Composite) {
// x = **y; // x = **y;
// possible. // possible.
while(1) { while(1) {
Tokenise();
printf("\t\t\tType on parsing is %d\n", CurrentToken.type); printf("\t\t\tType on parsing is %d\n", CurrentToken.type);
if(CurrentToken.type != AR_STAR) if(CurrentToken.type != AR_STAR)
break; break;
Type = PointerTo(Type); Type = PointerTo(Type);
Tokenise();
// Tokenise(); TODO: is this skipping pointers? // Tokenise(); TODO: is this skipping pointers?
} }

View File

@ -33,7 +33,7 @@ static int ReadDeclarationList(struct SymbolTableEntry* FunctionSymbol, int Stor
if(FunctionSymbol != NULL) if(FunctionSymbol != NULL)
PrototypePointer = FunctionSymbol->Start; PrototypePointer = FunctionSymbol->Start;
while(CurrentToken.type != LI_RPARE) { while(CurrentToken.type != End) {
TokenType = ParseOptionalPointer(FunctionSymbol); TokenType = ParseOptionalPointer(FunctionSymbol);
VerifyToken(TY_IDENTIFIER, "identifier"); VerifyToken(TY_IDENTIFIER, "identifier");
@ -200,6 +200,11 @@ struct ASTNode* ParseFunction(int Type) {
ParamCount = ReadDeclarationList(OldFunction, SC_GLOBAL, LI_RPARE); ParamCount = ReadDeclarationList(OldFunction, SC_GLOBAL, LI_RPARE);
VerifyToken(LI_RPARE, ")"); VerifyToken(LI_RPARE, ")");
printf("\nIdentified%sfunction %s of return type %s, end label %d\n",
(OldFunction == NULL) ? " new " : " overloaded ",
(OldFunction == NULL) ? NewFunction->Name : OldFunction->Name,
TypeNames(Type), BreakLabel);
if(NewFunction) { if(NewFunction) {
NewFunction->Elements = ParamCount; NewFunction->Elements = ParamCount;
NewFunction->Start = Params; NewFunction->Start = Params;
@ -212,9 +217,6 @@ struct ASTNode* ParseFunction(int Type) {
Tokenise(); Tokenise();
return NULL; return NULL;
} }
printf("\nIdentified%sfunction %s of return type %s, end label %d\n", (OldFunction == NULL) ? " new " : " overloaded ", OldFunction->Name, TypeNames(Type), BreakLabel);
FunctionEntry = OldFunction; FunctionEntry = OldFunction;

6
tests/struct.er Normal file
View File

@ -0,0 +1,6 @@
struct a {
int x,
int y
};
int main() { return (0); }