summaryrefslogtreecommitdiffstats
path: root/src/Compiler/Parser.java
diff options
context:
space:
mode:
authorjwansek <eddie.atten.ea29@gmail.com>2021-11-22 18:46:42 +0000
committerjwansek <eddie.atten.ea29@gmail.com>2021-11-22 18:46:42 +0000
commitde5022e778c12a8b91b905473f2f74bf7172eac3 (patch)
tree4f16f1cb0b6b75c0e8975bb77f432071684e57ed /src/Compiler/Parser.java
parent6557b7b080abab676cc15774bb4b4428e776cd03 (diff)
parent424ac34886895756525fbf5ddd704976e2e7d7dc (diff)
downloadesotericFORTRAN-de5022e778c12a8b91b905473f2f74bf7172eac3.tar.gz
esotericFORTRAN-de5022e778c12a8b91b905473f2f74bf7172eac3.zip
Merge branch 'main' of https://github.com/AlfieEagleton/EsotericProject into argparse
Diffstat (limited to 'src/Compiler/Parser.java')
-rw-r--r--src/Compiler/Parser.java89
1 files changed, 50 insertions, 39 deletions
diff --git a/src/Compiler/Parser.java b/src/Compiler/Parser.java
index 8bb8951..d0556e4 100644
--- a/src/Compiler/Parser.java
+++ b/src/Compiler/Parser.java
@@ -17,7 +17,7 @@ public class Parser {
List<Statement> statements = new ArrayList<>();
try{
while (!checkEOF()){
- statements.add(declaration());
+ statements.add(statement());
}
}catch (Error e){
return null;
@@ -26,6 +26,20 @@ public class Parser {
}
+ private Statement statement(){
+ if(checkToken(TokenType.INT)||checkToken(TokenType.REAL)||checkToken(TokenType.STRING)){
+ return declaration();
+ }
+ else if (matchAndAdvance(TokenType.PRINT)){
+ return printStatement();
+ }else if (matchAndAdvance(TokenType.IF)){
+ return ifStatement();
+ }else if (matchAndAdvance(TokenType.DO)){
+ return doStatement();
+ }
+ return expressionStatement();
+ }
+
//Clean up and reduce code mess
private Statement declaration(){
@@ -47,13 +61,13 @@ public class Parser {
matchOrError(TokenType.EQUALS, "Length of string must be defined");
Expression length = expression();
if(!(length instanceof Expression.Literal)){
- throw error("String length must be a number");
+ throw error(getCurrentToken(),"String length must be a number");
}
if(!((Expression.Literal)length).type.equals("int")){
- throw error("String length must be a integer");
+ throw error(getCurrentToken(),"String length must be a integer");
}
if((int)((Expression.Literal)length).value.value<1){
- throw error("String length must be greater then 0");
+ throw error(getCurrentToken(),"String length must be greater then 0");
}
matchOrError(TokenType.RIGHT_PAREN, "Length of string must be defined");
@@ -62,30 +76,16 @@ public class Parser {
Token varName = getPreviousToken();
return new Statement.StringDeclaration(varName,length);
}
-
- return statement();
- }
-
- private Statement statement(){
- if (matchAndAdvance(TokenType.PRINT)){
- return printStatement();
- }else if (matchAndAdvance(TokenType.IF)){
- Statement statement = ifStatement();
- return statement;
- }else if (matchAndAdvance(TokenType.DO)){
- Statement statement = doStatement();
- return statement;
- }
- return expressionStatement();
+ return null;
}
private BlockStatement blockStatement(){
List<Statement> statements = new ArrayList<>();
while(!matchAndAdvance(TokenType.END)&&!checkToken(TokenType.ELSE)){
if(checkEOF()){
- throw error("end missing from block");
+ throw error(getCurrentToken(),"end missing from block");
}
- statements.add(declaration());
+ statements.add(statement());
}
return new Statement.BlockStatement(statements);
}
@@ -93,11 +93,10 @@ public class Parser {
private Statement printStatement(){
matchOrError(TokenType.STAR, "Syntax, print *, item1, item2...");
List<Expression> exprList = new ArrayList<>();
- while(!matchAndAdvance(TokenType.ENDPRINT)){
+ while(matchAndAdvance(TokenType.COMMA)){
if(checkEOF()){
- throw error("Missing close parentheses");
+ throw error(getCurrentToken(),"reached end of file");
}
- matchOrError(TokenType.COMMA, "Print items must be seperated by ,");
Expression expr = expression();
exprList.add(expr);
}
@@ -141,9 +140,9 @@ public class Parser {
}
private Statement whileStatement(){
- matchOrError(TokenType.LEFT_PAREN, " missing '(");
+ matchOrError(TokenType.LEFT_PAREN, " missing '(' for do statement condition");
Expression condition = expression();
- matchOrError(TokenType.RIGHT_PAREN, " missing ')");
+ matchOrError(TokenType.RIGHT_PAREN, " missing ')' for do condition");
Statement.BlockStatement codeBlock = blockStatement();
matchOrError(TokenType.DO, "Do while statements end with do");
return new Statement.DoWhileStatement(condition,codeBlock);
@@ -162,7 +161,7 @@ public class Parser {
if (variable instanceof Expression.Variable){
return new Expression.AssignmentExpression(((Expression.Variable)variable).name,assignedvalue);
}
- throw error("Incorrect assignment operation");
+ throw error(getCurrentToken(),"Left of assignment must be a variable");
}
return variable;
}
@@ -173,9 +172,25 @@ public class Parser {
}
private Expression equality(){
- Expression createdExpression = comparison();
+ Expression createdExpression = logical();
while (matchAndAdvance(TokenType.EQUALITY)){
Token op = getPreviousToken();
+ Expression right = logical();
+ createdExpression = new Expression.Binary(createdExpression, op, right);
+ }
+ return createdExpression;
+ }
+
+ private Expression logical(){
+ if(matchAndAdvance(TokenType.NOT)){
+ Token op = getPreviousToken();
+ Expression right = comparison();
+ Expression createdExpression = new Expression.Singular(op, right);
+ return createdExpression;
+ }
+ Expression createdExpression = comparison();
+ while (matchAndAdvance(TokenType.AND)||matchAndAdvance(TokenType.OR)){
+ Token op = getPreviousToken();
Expression right = comparison();
createdExpression = new Expression.Binary(createdExpression, op, right);
}
@@ -184,7 +199,7 @@ public class Parser {
private Expression comparison(){
Expression createdExpression = term();
- while (matchAndAdvance(TokenType.GREATER)||matchAndAdvance(TokenType.LESS)){
+ while (matchAndAdvance(TokenType.GREATER)||matchAndAdvance(TokenType.LESS)||matchAndAdvance(TokenType.GREATER_EQUAL)||matchAndAdvance(TokenType.LESS_EQUAL)){
Token op = getPreviousToken();
Expression right = term();
createdExpression = new Expression.Binary(createdExpression, op, right);
@@ -231,14 +246,10 @@ public class Parser {
if (matchAndAdvance(TokenType.LEFT_PAREN)){
Expression expr = expression();
- if (matchAndAdvance(TokenType.RIGHT_PAREN)){
- return new Expression.BracketedExpression(expr);
- }
- else{
- throw error("Expected ')");
- }
+ matchOrError(TokenType.RIGHT_PAREN,"Expected ')'");
+ return new Expression.BracketedExpression(expr);
}
- throw error("Expected Expression");
+ throw error(getCurrentToken(),"Unknown syntax error");
}
private void advanceToken(){
@@ -259,7 +270,7 @@ public class Parser {
if (matchAndAdvance(type)){
return true;
}
- throw error(errorMessage);
+ throw error(getCurrentToken(),errorMessage);
}
private boolean checkToken(TokenType type){
@@ -279,8 +290,8 @@ public class Parser {
return tokens.get(currentToken - 1);
}
- private Error error(String message){
- Language.displayError(message);
+ private Error error(Token token, String message){
+ Language.displayError(token ,message);
return new Error();
}