Fix assigning local definitions.
This commit is contained in:
parent
29b797d7dc
commit
cdd8e017e2
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -8,6 +8,7 @@ bin
|
||||||
build
|
build
|
||||||
Erythro
|
Erythro
|
||||||
test
|
test
|
||||||
|
cmake-build-debug
|
||||||
|
|
||||||
*.s
|
*.s
|
||||||
*.o
|
*.o
|
|
@ -434,7 +434,7 @@ struct ASTNode* PostfixStatement();
|
||||||
|
|
||||||
void ParseGlobals();
|
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();
|
struct ASTNode* ParseCompound();
|
||||||
|
|
||||||
|
|
13
src/Parser.c
13
src/Parser.c
|
@ -393,7 +393,6 @@ struct ASTNode* ParseExpressionList(int terminate) {
|
||||||
// TODO: for(int x = 0;
|
// TODO: for(int x = 0;
|
||||||
Child = ParsePrecedenceASTNode(0);
|
Child = ParsePrecedenceASTNode(0);
|
||||||
Count++;
|
Count++;
|
||||||
Safe();
|
|
||||||
Tree = ConstructASTNode(OP_COMP, PointerTo(RET_VOID), Tree, NULL, Child, NULL, Count);
|
Tree = ConstructASTNode(OP_COMP, PointerTo(RET_VOID), Tree, NULL, Child, NULL, Count);
|
||||||
|
|
||||||
if (CurrentFile->CurrentSymbol.type == terminate)
|
if (CurrentFile->CurrentSymbol.type == terminate)
|
||||||
|
@ -438,6 +437,12 @@ struct ASTNode* ParseStatement(void) {
|
||||||
Node = ParseCompound();
|
Node = ParseCompound();
|
||||||
VerifyToken(LI_RBRAC, "}");
|
VerifyToken(LI_RBRAC, "}");
|
||||||
return Node;
|
return Node;
|
||||||
|
/* case TY_IDENTIFIER:
|
||||||
|
if (FindAlias(CurrentIdentifier) != NULL) {
|
||||||
|
Node = ParsePrecedenceASTNode(0);
|
||||||
|
VerifyToken(LI_SEMIC, ";");
|
||||||
|
return Node;
|
||||||
|
}*/
|
||||||
case TY_CHAR:
|
case TY_CHAR:
|
||||||
case TY_LONG:
|
case TY_LONG:
|
||||||
case TY_INT:
|
case TY_INT:
|
||||||
|
@ -446,7 +451,7 @@ struct ASTNode* ParseStatement(void) {
|
||||||
case KW_ENUM:
|
case KW_ENUM:
|
||||||
case KW_ALIAS:
|
case KW_ALIAS:
|
||||||
printf("\t\tNew Variable: %s\n", CurrentIdentifier);
|
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, ";");
|
VerifyToken(LI_SEMIC, ";");
|
||||||
Safe();
|
Safe();
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -497,12 +502,12 @@ struct ASTNode* ParseStatement(void) {
|
||||||
|
|
||||||
void ParseGlobals() {
|
void ParseGlobals() {
|
||||||
struct SymbolTableEntry* Composite;
|
struct SymbolTableEntry* Composite;
|
||||||
|
struct ASTNode* empty;
|
||||||
printf("Parsing global definitions\r\n");
|
printf("Parsing global definitions\r\n");
|
||||||
|
|
||||||
while (CurrentFile->CurrentSymbol.type != LI_EOF) {
|
while (CurrentFile->CurrentSymbol.type != LI_EOF) {
|
||||||
// Read in a declaration, or list thereof
|
// 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
|
// Consume semicolons if present
|
||||||
OptionallyConsume(LI_SEMIC);
|
OptionallyConsume(LI_SEMIC);
|
||||||
|
|
|
@ -9,34 +9,9 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
static void ParseEnumDeclaration();
|
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);
|
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.
|
* Handles parsing multiple statements or expressions in a row.
|
||||||
* These are typically grouped together with the Compound tokens "{ }"
|
* 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.
|
// 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 SymbolTableEntry* sym = AddSymbol(name, Type, ST_VAR, Storage, 1, 0, CompositeType);
|
||||||
|
struct ASTNode* var, *expr;
|
||||||
|
|
||||||
// Being assigned.
|
// Being assigned.
|
||||||
if (CurrentFile->CurrentSymbol.type == LI_EQUAL) {
|
if (CurrentFile->CurrentSymbol.type == LI_EQUAL) {
|
||||||
|
@ -300,6 +276,18 @@ static struct SymbolTableEntry* ParseScalarDeclaration(char* name, int Type, str
|
||||||
sym->InitialValues[0] = ParseLiteral(Type);
|
sym->InitialValues[0] = ParseLiteral(Type);
|
||||||
Tokenise();
|
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)
|
if (Storage == SC_GLOBAL)
|
||||||
|
@ -330,10 +318,11 @@ static struct SymbolTableEntry* ParseScalarDeclaration(char* name, int Type, str
|
||||||
* @return the type of the declaration
|
* @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;
|
int initType, type;
|
||||||
struct SymbolTableEntry* symbol;
|
struct SymbolTableEntry* symbol;
|
||||||
|
struct ASTNode* Tree;
|
||||||
|
*CompoundTree = NULL;
|
||||||
|
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
if ((initType = ParseType(CompositeType, &ClassType)) == -1)
|
if ((initType = ParseType(CompositeType, &ClassType)) == -1)
|
||||||
|
@ -341,7 +330,7 @@ int ParseDeclarationList(struct SymbolTableEntry** CompositeType, int ClassType,
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
type = ParsePointerType(initType);
|
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]);
|
printf("\tReading a new element: %s of type %d, scope %s\n", CurrentIdentifier, type, ScopeNames[ClassType]);
|
||||||
|
|
||||||
// Lists of function declarations are not valid.
|
// Lists of function declarations are not valid.
|
||||||
|
@ -351,6 +340,11 @@ int ParseDeclarationList(struct SymbolTableEntry** CompositeType, int ClassType,
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (*CompoundTree == NULL)
|
||||||
|
*CompoundTree = Tree;
|
||||||
|
else
|
||||||
|
*CompoundTree = ConstructASTNode(OP_COMP, RET_NONE, *CompoundTree, NULL, Tree, NULL, 0);
|
||||||
|
|
||||||
// Terminate at either symbol
|
// Terminate at either symbol
|
||||||
if (CurrentFile->CurrentSymbol.type == StatementEndSymbol || CurrentFile->CurrentSymbol.type == TerminateSymbol)
|
if (CurrentFile->CurrentSymbol.type == StatementEndSymbol || CurrentFile->CurrentSymbol.type == TerminateSymbol)
|
||||||
return type;
|
return type;
|
||||||
|
@ -371,13 +365,14 @@ int ParseDeclarationList(struct SymbolTableEntry** CompositeType, int ClassType,
|
||||||
static int ParseParameterDeclarationList(struct SymbolTableEntry* FunctionDeclaration, struct SymbolTableEntry* FunctionDefinition) {
|
static int ParseParameterDeclarationList(struct SymbolTableEntry* FunctionDeclaration, struct SymbolTableEntry* FunctionDefinition) {
|
||||||
int TokenType, ParamCount = 0;
|
int TokenType, ParamCount = 0;
|
||||||
struct SymbolTableEntry* PrototypePointer = NULL, * Composite;
|
struct SymbolTableEntry* PrototypePointer = NULL, * Composite;
|
||||||
|
struct ASTNode* empty;
|
||||||
|
|
||||||
if (FunctionDeclaration != NULL)
|
if (FunctionDeclaration != NULL)
|
||||||
PrototypePointer = FunctionDeclaration->Start;
|
PrototypePointer = FunctionDeclaration->Start;
|
||||||
|
|
||||||
while (CurrentFile->CurrentSymbol.type != LI_RPARE) {
|
while (CurrentFile->CurrentSymbol.type != LI_RPARE) {
|
||||||
// Doing int x, y, float z is valid, so parse a list of declarations per parameter.
|
// 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)
|
if (TokenType == -1)
|
||||||
ErrorReport("Bad type in parameter list");
|
ErrorReport("Bad type in parameter list");
|
||||||
|
|
||||||
|
@ -580,7 +575,7 @@ static char* copyString(char* str) {
|
||||||
* @param Storage the storage class of the declaration
|
* @param Storage the storage class of the declaration
|
||||||
* @return the symbol table entry to the new symbol
|
* @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;
|
struct SymbolTableEntry* symbol = NULL;
|
||||||
malloc(2);
|
malloc(2);
|
||||||
char* variableName = copyString(CurrentIdentifier);
|
char* variableName = copyString(CurrentIdentifier);
|
||||||
|
@ -613,7 +608,7 @@ static struct SymbolTableEntry* ParseDeclarationSymbol(int Type, struct SymbolTa
|
||||||
symbol = ParseArrayDeclaration(variableName, Type, CompositeType, Storage);
|
symbol = ParseArrayDeclaration(variableName, Type, CompositeType, Storage);
|
||||||
structureType = ST_ARR;
|
structureType = ST_ARR;
|
||||||
} else {
|
} else {
|
||||||
symbol = ParseScalarDeclaration(variableName, Type, CompositeType, Storage);
|
symbol = ParseScalarDeclaration(variableName, Type, CompositeType, Storage, Tree);
|
||||||
}
|
}
|
||||||
|
|
||||||
return symbol;
|
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");
|
printf("Reading a composite declaration.. Type is %s\n", Type == DAT_STRUCT ? "struct" : "union");
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
Type = ParseDeclarationList(&Member, SC_MEMBER, LI_SEMIC,LI_RBRAC);
|
Type = ParseDeclarationList(&Member, SC_MEMBER, LI_SEMIC, LI_RBRAC, NULL);
|
||||||
if (Type == -1)
|
if (Type == -1)
|
||||||
ErrorReport("Bad type in member list of composite\n");
|
ErrorReport("Bad type in member list of composite\n");
|
||||||
|
|
||||||
|
|
|
@ -3,10 +3,8 @@ import "tests/print.em"
|
||||||
int stuff[] = { 5, 7, 9 };
|
int stuff[] = { 5, 7, 9 };
|
||||||
|
|
||||||
int :: main() {
|
int :: main() {
|
||||||
int x;
|
for (int x = 0; x < 5; x++) {
|
||||||
int y;
|
int y = 0;
|
||||||
for (x = 0; x < 5; x++) {
|
|
||||||
y = 0;
|
|
||||||
switch(x) {
|
switch(x) {
|
||||||
case 1: {
|
case 1: {
|
||||||
y = stuff[0];
|
y = stuff[0];
|
||||||
|
|
Loading…
Reference in New Issue
Block a user