diff --git a/.gitignore b/.gitignore index b25530d..a2890b4 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ bin build Erythro test +cmake-build-debug *.s *.o \ No newline at end of file diff --git a/include/Defs.h b/include/Defs.h index 56fd0e8..4388706 100644 --- a/include/Defs.h +++ b/include/Defs.h @@ -434,7 +434,7 @@ struct ASTNode* PostfixStatement(); void ParseGlobals(); -int ParseDeclarationList(struct SymbolTableEntry** CompositeType, int ClassType, int StatementEndSymbool, int TerminateSymbol); +int ParseDeclarationList(struct SymbolTableEntry** CompositeType, int ClassType, int StatementEndSymbool, int TerminateSymbol, struct ASTNode** Tree); struct ASTNode* ParseCompound(); diff --git a/src/Parser.c b/src/Parser.c index b94033b..67bad81 100644 --- a/src/Parser.c +++ b/src/Parser.c @@ -393,7 +393,6 @@ struct ASTNode* ParseExpressionList(int terminate) { // TODO: for(int x = 0; Child = ParsePrecedenceASTNode(0); Count++; - Safe(); Tree = ConstructASTNode(OP_COMP, PointerTo(RET_VOID), Tree, NULL, Child, NULL, Count); if (CurrentFile->CurrentSymbol.type == terminate) @@ -438,6 +437,12 @@ struct ASTNode* ParseStatement(void) { Node = ParseCompound(); VerifyToken(LI_RBRAC, "}"); return Node; +/* case TY_IDENTIFIER: + if (FindAlias(CurrentIdentifier) != NULL) { + Node = ParsePrecedenceASTNode(0); + VerifyToken(LI_SEMIC, ";"); + return Node; + }*/ case TY_CHAR: case TY_LONG: case TY_INT: @@ -446,7 +451,7 @@ struct ASTNode* ParseStatement(void) { case KW_ENUM: case KW_ALIAS: printf("\t\tNew Variable: %s\n", CurrentIdentifier); - ParseDeclarationList(&Composite, SC_LOCAL, LI_SEMIC, LI_EOF); + ParseDeclarationList(&Composite, SC_LOCAL, LI_SEMIC, LI_EOF, &Node); VerifyToken(LI_SEMIC, ";"); Safe(); return NULL; @@ -497,12 +502,12 @@ struct ASTNode* ParseStatement(void) { void ParseGlobals() { struct SymbolTableEntry* Composite; - + struct ASTNode* empty; printf("Parsing global definitions\r\n"); while (CurrentFile->CurrentSymbol.type != LI_EOF) { // Read in a declaration, or list thereof - ParseDeclarationList(&Composite, SC_GLOBAL, LI_SEMIC, LI_EOF); + ParseDeclarationList(&Composite, SC_GLOBAL, LI_SEMIC, LI_EOF, &empty); // Consume semicolons if present OptionallyConsume(LI_SEMIC); diff --git a/src/Statements.c b/src/Statements.c index 04cc32d..a446a13 100644 --- a/src/Statements.c +++ b/src/Statements.c @@ -9,34 +9,9 @@ #include static void ParseEnumDeclaration(); -static struct SymbolTableEntry* ParseDeclarationSymbol(int Type, struct SymbolTableEntry* CompositeType, int Storage); +static struct SymbolTableEntry* ParseDeclarationSymbol(int Type, struct SymbolTableEntry* CompositeType, int Storage, struct ASTNode** Tree); static int ParseAliasDeclaration(struct SymbolTableEntry** CompositeType); - -/** - * - // Determine whether we're initializing immediately - if (CurrentFile->CurrentSymbol.type == LI_EQUAL) { - // TODO: Default parameters - if (Storage == SC_PARAM) - ErrorReport("Initialization of parameter not permitted.\n"); - // TODO: Enum initialization - if (Storage == SC_MEMBER) - ErrorReport("Initialization of a member not permitted.\n"); - - Tokenise(); - - if (structureType == ST_ARR) { - ParseArrayInitialization(symbol, Type, CompositeType, Storage); - } else { - // TODO: Inline initialization - ErrorReport("Initialization of a scalar not permitted.\n"); - } - } - * - */ - - /* * Handles parsing multiple statements or expressions in a row. * These are typically grouped together with the Compound tokens "{ }" @@ -285,8 +260,9 @@ static struct SymbolTableEntry* ParseArrayDeclaration(char* name, int Type, stru } // A short redirect to add a Scalar definition to the variable tables. -static struct SymbolTableEntry* ParseScalarDeclaration(char* name, int Type, struct SymbolTableEntry* CompositeType, int Storage) { +static struct SymbolTableEntry* ParseScalarDeclaration(char* name, int Type, struct SymbolTableEntry* CompositeType, int Storage, struct ASTNode** Tree) { struct SymbolTableEntry* sym = AddSymbol(name, Type, ST_VAR, Storage, 1, 0, CompositeType); + struct ASTNode* var, *expr; // Being assigned. if (CurrentFile->CurrentSymbol.type == LI_EQUAL) { @@ -300,6 +276,18 @@ static struct SymbolTableEntry* ParseScalarDeclaration(char* name, int Type, str sym->InitialValues[0] = ParseLiteral(Type); Tokenise(); } + + else if (Storage == SC_LOCAL) { + var = ConstructASTLeaf(REF_IDENT, sym->Type, sym, 0); + expr = ParsePrecedenceASTNode(0); + expr->RVal = 1; + + expr = MutateType(expr, var->ExprType, 0); + if (expr == NULL) + ErrorReport("Incompatible types in assignment: %s, %s\n", TypeNames(expr->ExprType), TypeNames(var->ExprType)); + + *Tree = ConstructASTNode(OP_ASSIGN, expr->ExprType, expr, NULL, var, NULL, 0); + } } if (Storage == SC_GLOBAL) @@ -330,10 +318,11 @@ static struct SymbolTableEntry* ParseScalarDeclaration(char* name, int Type, str * @return the type of the declaration * */ -int ParseDeclarationList(struct SymbolTableEntry** CompositeType, int ClassType, int StatementEndSymbol, int TerminateSymbol) { - +int ParseDeclarationList(struct SymbolTableEntry** CompositeType, int ClassType, int StatementEndSymbol, int TerminateSymbol, struct ASTNode** CompoundTree) { int initType, type; struct SymbolTableEntry* symbol; + struct ASTNode* Tree; + *CompoundTree = NULL; fflush(stdout); if ((initType = ParseType(CompositeType, &ClassType)) == -1) @@ -341,7 +330,7 @@ int ParseDeclarationList(struct SymbolTableEntry** CompositeType, int ClassType, while (1) { type = ParsePointerType(initType); - symbol = ParseDeclarationSymbol(type, *CompositeType, ClassType); + symbol = ParseDeclarationSymbol(type, *CompositeType, ClassType, &Tree); printf("\tReading a new element: %s of type %d, scope %s\n", CurrentIdentifier, type, ScopeNames[ClassType]); // Lists of function declarations are not valid. @@ -351,6 +340,11 @@ int ParseDeclarationList(struct SymbolTableEntry** CompositeType, int ClassType, return type; } + if (*CompoundTree == NULL) + *CompoundTree = Tree; + else + *CompoundTree = ConstructASTNode(OP_COMP, RET_NONE, *CompoundTree, NULL, Tree, NULL, 0); + // Terminate at either symbol if (CurrentFile->CurrentSymbol.type == StatementEndSymbol || CurrentFile->CurrentSymbol.type == TerminateSymbol) return type; @@ -371,13 +365,14 @@ int ParseDeclarationList(struct SymbolTableEntry** CompositeType, int ClassType, static int ParseParameterDeclarationList(struct SymbolTableEntry* FunctionDeclaration, struct SymbolTableEntry* FunctionDefinition) { int TokenType, ParamCount = 0; struct SymbolTableEntry* PrototypePointer = NULL, * Composite; + struct ASTNode* empty; if (FunctionDeclaration != NULL) PrototypePointer = FunctionDeclaration->Start; while (CurrentFile->CurrentSymbol.type != LI_RPARE) { // Doing int x, y, float z is valid, so parse a list of declarations per parameter. - TokenType = ParseDeclarationList(&Composite, SC_PARAM, LI_COM, LI_RPARE); + TokenType = ParseDeclarationList(&Composite, SC_PARAM, LI_COM, LI_RPARE, &empty); if (TokenType == -1) ErrorReport("Bad type in parameter list"); @@ -580,7 +575,7 @@ static char* copyString(char* str) { * @param Storage the storage class of the declaration * @return the symbol table entry to the new symbol */ -static struct SymbolTableEntry* ParseDeclarationSymbol(int Type, struct SymbolTableEntry* CompositeType, int Storage) { +static struct SymbolTableEntry* ParseDeclarationSymbol(int Type, struct SymbolTableEntry* CompositeType, int Storage, struct ASTNode** Tree) { struct SymbolTableEntry* symbol = NULL; malloc(2); char* variableName = copyString(CurrentIdentifier); @@ -613,7 +608,7 @@ static struct SymbolTableEntry* ParseDeclarationSymbol(int Type, struct SymbolTa symbol = ParseArrayDeclaration(variableName, Type, CompositeType, Storage); structureType = ST_ARR; } else { - symbol = ParseScalarDeclaration(variableName, Type, CompositeType, Storage); + symbol = ParseScalarDeclaration(variableName, Type, CompositeType, Storage, Tree); } return symbol; @@ -662,7 +657,7 @@ struct SymbolTableEntry* BeginCompositeDeclaration(int Type) { printf("Reading a composite declaration.. Type is %s\n", Type == DAT_STRUCT ? "struct" : "union"); while (1) { - Type = ParseDeclarationList(&Member, SC_MEMBER, LI_SEMIC,LI_RBRAC); + Type = ParseDeclarationList(&Member, SC_MEMBER, LI_SEMIC, LI_RBRAC, NULL); if (Type == -1) ErrorReport("Bad type in member list of composite\n"); diff --git a/tests/switch.er b/tests/switch.er index 7e32969..fa1a040 100644 --- a/tests/switch.er +++ b/tests/switch.er @@ -3,10 +3,8 @@ import "tests/print.em" int stuff[] = { 5, 7, 9 }; int :: main() { - int x; - int y; - for (x = 0; x < 5; x++) { - y = 0; + for (int x = 0; x < 5; x++) { + int y = 0; switch(x) { case 1: { y = stuff[0];