Allow single statements in bodies of if, else, while and for
This commit is contained in:
parent
334b02cd76
commit
734bc049e7
|
@ -69,6 +69,9 @@ char* TokenNames[] = {
|
||||||
"Void Type",
|
"Void Type",
|
||||||
|
|
||||||
"Function keyword",
|
"Function keyword",
|
||||||
|
"Break keyword",
|
||||||
|
"Continue keyword",
|
||||||
|
|
||||||
"Print Keyword",
|
"Print Keyword",
|
||||||
"If keyword",
|
"If keyword",
|
||||||
"Else keyword",
|
"Else keyword",
|
||||||
|
@ -77,7 +80,11 @@ char* TokenNames[] = {
|
||||||
|
|
||||||
"Return keyword",
|
"Return keyword",
|
||||||
|
|
||||||
"Struct keyword"
|
"Struct keyword",
|
||||||
|
"Union keyword",
|
||||||
|
"Enum keyword",
|
||||||
|
"Alias keyword",
|
||||||
|
"Import keyword"
|
||||||
};
|
};
|
||||||
|
|
||||||
char* ScopeNames[] = {
|
char* ScopeNames[] = {
|
||||||
|
|
|
@ -462,6 +462,8 @@ struct ASTNode* GetExpressionList() {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Handles parsing an individual statement.
|
* Handles parsing an individual statement.
|
||||||
|
* This has the ability to parse Compounds in the case it starts with a left brace.
|
||||||
|
* This solves the dangling else problem for the parser.
|
||||||
*
|
*
|
||||||
* It serves as a wrapper around:
|
* It serves as a wrapper around:
|
||||||
* * If Statement
|
* * If Statement
|
||||||
|
@ -470,13 +472,18 @@ struct ASTNode* GetExpressionList() {
|
||||||
* * Return Statement
|
* * Return Statement
|
||||||
* * Numeric literals and variables
|
* * Numeric literals and variables
|
||||||
* * Binary Expressions
|
* * Binary Expressions
|
||||||
|
*
|
||||||
* @return the AST Node representing this single statement
|
* @return the AST Node representing this single statement
|
||||||
*/
|
*/
|
||||||
struct ASTNode* ParseStatement(void) {
|
struct ASTNode* ParseStatement(void) {
|
||||||
int Type;
|
int Type;
|
||||||
|
struct ASTNode* Node;
|
||||||
|
|
||||||
printf("\t\tBranch leads to here, type %s/%d\r\n", TokenNames[CurrentFile->CurrentSymbol.type], CurrentFile->CurrentSymbol.type);
|
printf("\t\tBranch leads to here, type %s/%d\r\n", TokenNames[CurrentFile->CurrentSymbol.type], CurrentFile->CurrentSymbol.type);
|
||||||
switch (CurrentFile->CurrentSymbol.type) {
|
switch (CurrentFile->CurrentSymbol.type) {
|
||||||
|
case LI_LBRAC:
|
||||||
|
Node = ParseCompound();
|
||||||
|
return Node;
|
||||||
case TY_CHAR:
|
case TY_CHAR:
|
||||||
case TY_LONG:
|
case TY_LONG:
|
||||||
case TY_INT:
|
case TY_INT:
|
||||||
|
|
|
@ -384,11 +384,11 @@ struct ASTNode* IfStatement() {
|
||||||
|
|
||||||
VerifyToken(LI_RPARE, ")");
|
VerifyToken(LI_RPARE, ")");
|
||||||
|
|
||||||
True = ParseCompound();
|
True = ParseStatement();
|
||||||
|
|
||||||
if (CurrentFile->CurrentSymbol.type == KW_ELSE) {
|
if (CurrentFile->CurrentSymbol.type == KW_ELSE) {
|
||||||
Tokenise();
|
Tokenise();
|
||||||
False = ParseCompound();
|
False = ParseStatement();
|
||||||
}
|
}
|
||||||
|
|
||||||
return ConstructASTNode(OP_IF, RET_NONE, Condition, True, False, NULL, 0);
|
return ConstructASTNode(OP_IF, RET_NONE, Condition, True, False, NULL, 0);
|
||||||
|
@ -428,14 +428,13 @@ struct ASTNode* WhileStatement() {
|
||||||
|
|
||||||
Condition = ParsePrecedenceASTNode(0);
|
Condition = ParsePrecedenceASTNode(0);
|
||||||
|
|
||||||
|
|
||||||
if (Condition->Operation < OP_EQUAL || Condition->Operation > OP_GREATE)
|
if (Condition->Operation < OP_EQUAL || Condition->Operation > OP_GREATE)
|
||||||
Condition = ConstructASTBranch(OP_BOOLCONV, Condition->ExprType, Condition, NULL, 0);
|
Condition = ConstructASTBranch(OP_BOOLCONV, Condition->ExprType, Condition, NULL, 0);
|
||||||
|
|
||||||
VerifyToken(LI_RPARE, ")");
|
VerifyToken(LI_RPARE, ")");
|
||||||
|
|
||||||
CurrentFile->CurrentLoopDepth++;
|
CurrentFile->CurrentLoopDepth++;
|
||||||
Body = ParseCompound();
|
Body = ParseStatement();
|
||||||
CurrentFile->CurrentLoopDepth--;
|
CurrentFile->CurrentLoopDepth--;
|
||||||
|
|
||||||
return ConstructASTNode(OP_LOOP, RET_NONE, Condition, NULL, Body, NULL, 0);
|
return ConstructASTNode(OP_LOOP, RET_NONE, Condition, NULL, Body, NULL, 0);
|
||||||
|
@ -493,7 +492,7 @@ struct ASTNode* ForStatement() {
|
||||||
VerifyToken(LI_RPARE, ")");
|
VerifyToken(LI_RPARE, ")");
|
||||||
|
|
||||||
CurrentFile->CurrentLoopDepth++;
|
CurrentFile->CurrentLoopDepth++;
|
||||||
Body = ParseCompound();
|
Body = ParseStatement();
|
||||||
CurrentFile->CurrentLoopDepth--;
|
CurrentFile->CurrentLoopDepth--;
|
||||||
|
|
||||||
// We need to be able to skip over the body and the postop, so we group them together.
|
// We need to be able to skip over the body and the postop, so we group them together.
|
||||||
|
@ -572,6 +571,7 @@ struct ASTNode* BreakStatement() {
|
||||||
Die("Unable to break without a loop");
|
Die("Unable to break without a loop");
|
||||||
|
|
||||||
Tokenise();
|
Tokenise();
|
||||||
|
Tokenise();
|
||||||
|
|
||||||
return ConstructASTLeaf(OP_BREAK, 0, NULL, 0);
|
return ConstructASTLeaf(OP_BREAK, 0, NULL, 0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,9 +6,8 @@ int :: main() {
|
||||||
|
|
||||||
while (x < 15) {
|
while (x < 15) {
|
||||||
printf("%d\n", x);
|
printf("%d\n", x);
|
||||||
if (x =? 12) {
|
if (x =? 12)
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
if (x =? 10) {
|
if (x =? 10) {
|
||||||
x = x + 2;
|
x = x + 2;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user