From 8e368b67de60442c483bd9def7036e52562ccc81 Mon Sep 17 00:00:00 2001 From: AidenRushbrooke <72034940+AidenRushbrooke@users.noreply.github.com> Date: Mon, 22 Nov 2021 16:30:45 +0000 Subject: Improved error handing and added logical statements --- src/Compiler/Parser.java | 89 +++++++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 39 deletions(-) (limited to 'src/Compiler/Parser.java') 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 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 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 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,8 +172,24 @@ 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(); } -- cgit v1.2.3