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_RBRAC, // }
LI_LBRAS, // [
LI_RBRAS, // ]
LI_LPARE, // (
LI_RPARE, // )
@ -150,6 +153,7 @@ struct SymbolTable {
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 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 {
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 a struct
// 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 AddArraySymbol(char* Name, int Type, int Structure, int EndLabel, int Size);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *

View File

@ -488,6 +488,9 @@ void AsNewSymb(int ID) {
"\t.globl\t%s\n",
Symbols[ID].Name);
fprintf(OutputFile, "%s:", Symbols[ID].Name);
for(int i = 0; i < Symbols[ID].Length; i++) {
switch(TypeSize) {
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;
@ -495,6 +498,7 @@ void AsNewSymb(int ID) {
default: DieDecimal("Unknown type in AsNewSymbol", TypeSize); break;
}
}
}
int AsCall(int Register, int FuncID) {

View File

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

View File

@ -138,6 +138,20 @@ struct ASTNode* ParsePrimary(void) {
Node = ConstructASTLeaf(REF_IDENT, Symbols[ID].Type, ID);
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:
DieDecimal("Unable to parse primary type", CurrentToken.type);
}

View File

@ -26,6 +26,19 @@ void BeginVariableDeclaration(int Type) {
int ID;
//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) {
//printf("Identifier: %s\n", CurrentIdentifier);
printf("Adding symbol %s of type %s.\n", CurrentIdentifier, TypeNames[Type]);
@ -49,6 +62,9 @@ void BeginVariableDeclaration(int Type) {
}
}
VerifyToken(LI_SEMIC, ";");
}
struct ASTNode* ParseFunction(int Type) {
struct ASTNode* Tree;
struct ASTNode* FinalStatement;

View File

@ -45,6 +45,13 @@ static int NewSymbol(void) {
// 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 Slot;
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];