Start work on array parsing
This commit is contained in:
parent
bc1bac8a63
commit
985f02723e
|
@ -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);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
|
|
@ -488,11 +488,15 @@ void AsNewSymb(int ID) {
|
||||||
"\t.globl\t%s\n",
|
"\t.globl\t%s\n",
|
||||||
Symbols[ID].Name);
|
Symbols[ID].Name);
|
||||||
|
|
||||||
switch(TypeSize) {
|
fprintf(OutputFile, "%s:", Symbols[ID].Name);
|
||||||
case 1: fprintf(OutputFile, "%s:\t.byte\t0\n", Symbols[ID].Name); break;
|
|
||||||
case 4: fprintf(OutputFile, "%s:\t.long\t0\n", Symbols[ID].Name); break;
|
for(int i = 0; i < Symbols[ID].Length; i++) {
|
||||||
case 8: fprintf(OutputFile, "%s:\t.quad\t0\n", Symbols[ID].Name); break;
|
switch(TypeSize) {
|
||||||
default: DieDecimal("Unknown type in AsNewSymbol", TypeSize); break;
|
case 1: fprintf(OutputFile, "%s:\t.byte\t0\n", Symbols[ID].Name); break;
|
||||||
|
case 4: fprintf(OutputFile, "%s:\t.long\t0\n", Symbols[ID].Name); break;
|
||||||
|
case 8: fprintf(OutputFile, "%s:\t.quad\t0\n", Symbols[ID].Name); break;
|
||||||
|
default: DieDecimal("Unknown type in AsNewSymbol", TypeSize); break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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();
|
||||||
|
|
||||||
|
|
14
src/Parser.c
14
src/Parser.c
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,27 +26,43 @@ void BeginVariableDeclaration(int Type) {
|
||||||
int ID;
|
int ID;
|
||||||
//printf("type: %s\n", Types[Type]);
|
//printf("type: %s\n", Types[Type]);
|
||||||
|
|
||||||
while(1) {
|
if(CurrentToken.type == LI_LBRAS) {
|
||||||
//printf("Identifier: %s\n", CurrentIdentifier);
|
Tokenise(&CurrentToken);
|
||||||
printf("Adding symbol %s of type %s.\n", CurrentIdentifier, TypeNames[Type]);
|
Type = Type - 2;
|
||||||
ID = AddSymbol(CurrentIdentifier, Type, ST_VAR);
|
if(CurrentToken.type == LI_INT) {
|
||||||
AsNewSymb(ID);
|
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);
|
||||||
if(CurrentToken.type == LI_SEMIC) {
|
AsNewSymb(ID);
|
||||||
printf("\tEnd of variables to declare in this statement\n");
|
|
||||||
Tokenise(&CurrentToken);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(CurrentToken.type == LI_COM) {
|
Tokenise(&CurrentToken);
|
||||||
printf("\t\tStatement appears to continue. Integrating new variable.\n");
|
VerifyToken(LI_RBRAS, "]");
|
||||||
Tokenise(&CurrentToken);
|
} else {
|
||||||
VerifyToken(TY_IDENTIFIER, "ident");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
DieDecimal("\nExpected ; after identifier, found", CurrentToken.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);
|
||||||
|
AsNewSymb(ID);
|
||||||
|
|
||||||
|
if(CurrentToken.type == LI_SEMIC) {
|
||||||
|
printf("\tEnd of variables to declare in this statement\n");
|
||||||
|
Tokenise(&CurrentToken);
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VerifyToken(LI_SEMIC, ";");
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ASTNode* ParseFunction(int Type) {
|
struct ASTNode* ParseFunction(int Type) {
|
||||||
|
|
|
@ -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
3
tests/arrays
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
int a[51];
|
||||||
|
char b[100];
|
||||||
|
long c[5000];
|
Loading…
Reference in New Issue
Block a user