Start work on array parsing

This commit is contained in:
Curle 2020-11-18 20:49:08 +00:00
parent bc1bac8a63
commit 985f02723e
Signed by: TheCurle
GPG Key ID: 5942F13718443F79
7 changed files with 83 additions and 24 deletions

View File

@ -47,6 +47,9 @@ enum TokenTypes {
LI_LBRAC, // { LI_LBRAC, // {
LI_RBRAC, // } LI_RBRAC, // }
LI_LBRAS, // [
LI_RBRAS, // ]
LI_LPARE, // ( LI_LPARE, // (
LI_RPARE, // ) LI_RPARE, // )
@ -150,6 +153,7 @@ struct SymbolTable {
int Type; // An entry in DataTypes, referring to the type of this data int Type; // An entry in DataTypes, referring to the type of this data
int Structure; // An entry in StructureType - metadata on how to process the data int Structure; // An entry in StructureType - metadata on how to process the data
int EndLabel; // The number of the label to jump to, in order to exit this function (if applicable) int EndLabel; // The number of the label to jump to, in order to exit this function (if applicable)
int Length; // The length of the symbol in units of 1 element -- the size of an array, for example.
}; };
@ -179,7 +183,8 @@ enum DataTypes {
enum StructureType { enum StructureType {
ST_VAR, // This is variable ST_VAR, // This is variable
ST_FUNC // This is a function ST_FUNC, // This is a function
ST_ARR // This is an array
// This is an enum // This is an enum
// This is a struct // This is a struct
// This is a typedef // This is a typedef
@ -269,6 +274,8 @@ int AddSymbol(char* Name, int Type, int Structure);
int AddFunctionSymbol(char* Name, int Type, int Structure, int EndLabel); int AddFunctionSymbol(char* Name, int Type, int Structure, int EndLabel);
int AddArraySymbol(char* Name, int Type, int Structure, int EndLabel, int Size);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * *

View File

@ -487,12 +487,16 @@ void AsNewSymb(int ID) {
fprintf(OutputFile, "\t.data\n" fprintf(OutputFile, "\t.data\n"
"\t.globl\t%s\n", "\t.globl\t%s\n",
Symbols[ID].Name); Symbols[ID].Name);
fprintf(OutputFile, "%s:", Symbols[ID].Name);
switch(TypeSize) { for(int i = 0; i < Symbols[ID].Length; i++) {
case 1: fprintf(OutputFile, "%s:\t.byte\t0\n", Symbols[ID].Name); break; switch(TypeSize) {
case 4: fprintf(OutputFile, "%s:\t.long\t0\n", Symbols[ID].Name); break; case 1: fprintf(OutputFile, "%s:\t.byte\t0\n", Symbols[ID].Name); break;
case 8: fprintf(OutputFile, "%s:\t.quad\t0\n", Symbols[ID].Name); break; case 4: fprintf(OutputFile, "%s:\t.long\t0\n", Symbols[ID].Name); break;
default: DieDecimal("Unknown type in AsNewSymbol", TypeSize); break; case 8: fprintf(OutputFile, "%s:\t.quad\t0\n", Symbols[ID].Name); break;
default: DieDecimal("Unknown type in AsNewSymbol", TypeSize); break;
}
} }
} }

View File

@ -289,6 +289,14 @@ int Tokenise(struct Token* Token) {
Token->type = LI_RBRAC; Token->type = LI_RBRAC;
break; break;
case '[':
Token->type = LI_LBRAS;
break;
case ']':
Token->type = LI_RBRAS;
break;
case ':': case ':':
Char = NextChar(); Char = NextChar();

View File

@ -138,6 +138,20 @@ struct ASTNode* ParsePrimary(void) {
Node = ConstructASTLeaf(REF_IDENT, Symbols[ID].Type, ID); Node = ConstructASTLeaf(REF_IDENT, Symbols[ID].Type, ID);
break; break;
case LI_LPARE:
// We're expecting a primary encased in parentheses.
// That means, we tokenise the next item
Tokenise(&CurrentToken);
// Then, we expect a valid expression
Node = ParsePrecedenceASTNode(0);
// Then, after the expression is parsed, we're returned here.
// since the expression is ENCASED in parens, we expect another.
VerifyToken(LI_RPARE, ")");
// Skip the next tokenise and return early
return Node;
default: default:
DieDecimal("Unable to parse primary type", CurrentToken.type); DieDecimal("Unable to parse primary type", CurrentToken.type);
} }

View File

@ -25,28 +25,44 @@
void BeginVariableDeclaration(int Type) { void BeginVariableDeclaration(int Type) {
int ID; int ID;
//printf("type: %s\n", Types[Type]); //printf("type: %s\n", Types[Type]);
if(CurrentToken.type == LI_LBRAS) {
Tokenise(&CurrentToken);
Type = Type - 2;
if(CurrentToken.type == LI_INT) {
printf("Adding array %s that is %d x %s.\r\n", CurrentIdentifier, CurrentToken.value, TypeNames[Type]);
ID = AddArraySymbol(CurrentIdentifier, PointerTo(Type), ST_ARR, 0, CurrentToken.value);
AsNewSymb(ID);
}
Tokenise(&CurrentToken);
VerifyToken(LI_RBRAS, "]");
} else {
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);
AsNewSymb(ID); AsNewSymb(ID);
if(CurrentToken.type == LI_SEMIC) { if(CurrentToken.type == LI_SEMIC) {
printf("\tEnd of variables to declare in this statement\n"); printf("\tEnd of variables to declare in this statement\n");
Tokenise(&CurrentToken); Tokenise(&CurrentToken);
return; return;
}
if(CurrentToken.type == LI_COM) {
printf("\t\tStatement appears to continue. Integrating new variable.\n");
Tokenise(&CurrentToken);
VerifyToken(TY_IDENTIFIER, "ident");
continue;
}
DieDecimal("\nExpected ; after identifier, found", CurrentToken.type);
} }
if(CurrentToken.type == LI_COM) {
printf("\t\tStatement appears to continue. Integrating new variable.\n");
Tokenise(&CurrentToken);
VerifyToken(TY_IDENTIFIER, "ident");
continue;
}
DieDecimal("\nExpected ; after identifier, found", CurrentToken.type);
} }
VerifyToken(LI_SEMIC, ";");
} }
struct ASTNode* ParseFunction(int Type) { struct ASTNode* ParseFunction(int Type) {

View File

@ -45,6 +45,13 @@ static int NewSymbol(void) {
// TODO: this is going weird! // TODO: this is going weird!
int AddArraySymbol(char* Name, int Type, int Structure, int EndLabel, int Size) {
int Slot;
Slot = AddFunctionSymbol(Name, Type, Structure, EndLabel);
Symbols[Slot].Length = Size;
return Slot;
}
int AddFunctionSymbol(char* Name, int Type, int Structure, int EndLabel) { int AddFunctionSymbol(char* Name, int Type, int Structure, int EndLabel) {
int Slot; int Slot;
Slot = AddSymbol(Name, Type, Structure); Slot = AddSymbol(Name, Type, Structure);

3
tests/arrays Normal file
View File

@ -0,0 +1,3 @@
int a[51];
char b[100];
long c[5000];