summaryrefslogtreecommitdiffstats
path: root/src/Compiler/Parser.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/Compiler/Parser.java')
-rw-r--r--src/Compiler/Parser.java52
1 files changed, 40 insertions, 12 deletions
diff --git a/src/Compiler/Parser.java b/src/Compiler/Parser.java
index d0556e4..eba3513 100644
--- a/src/Compiler/Parser.java
+++ b/src/Compiler/Parser.java
@@ -3,7 +3,6 @@ package Compiler;
import java.util.ArrayList;
import java.util.List;
-import Compiler.Statement.BlockStatement;
public class Parser {
private final List<Token> tokens;
@@ -44,11 +43,17 @@ public class Parser {
//Clean up and reduce code mess
private Statement declaration(){
if (matchAndAdvance(TokenType.INT)){
+ if(matchAndAdvance(TokenType.DIMENSION)){
+ return arrayDeclaration("int");
+ }
matchOrError(TokenType.DEFINE, ":: Required for variable definition");
matchOrError(TokenType.IDENTIFIER,"Expected variable name.");
Token varName = getPreviousToken();
return new Statement.VariableDeclaration(varName,"int");
} else if (matchAndAdvance(TokenType.REAL)){
+ if(matchAndAdvance(TokenType.DIMENSION)){
+ return arrayDeclaration("real");
+ }
matchOrError(TokenType.DEFINE, ":: Required for variable definition");
matchOrError(TokenType.IDENTIFIER,"Expected variable name.");
Token varName = getPreviousToken();
@@ -79,7 +84,23 @@ public class Parser {
return null;
}
- private BlockStatement blockStatement(){
+ private Statement arrayDeclaration(String type){
+ matchOrError(TokenType.LEFT_PAREN,"Expected ')'");
+ List<Expression> dimensions = new ArrayList<>();
+ Expression dimension = expression();
+ dimensions.add(dimension);
+ while(matchAndAdvance(TokenType.COMMA)){
+ dimension = expression();
+ dimensions.add(dimension);
+ }
+ matchOrError(TokenType.RIGHT_PAREN, "Expected ')'");
+ matchOrError(TokenType.DEFINE, ":: Required for variable definition");
+ matchOrError(TokenType.IDENTIFIER,"Expected variable name.");
+ Token varName = getPreviousToken();
+ return new Statement.ArrayDeclaration(varName, type, dimensions);
+ }
+
+ private Statement blockStatement(){
List<Statement> statements = new ArrayList<>();
while(!matchAndAdvance(TokenType.END)&&!checkToken(TokenType.ELSE)){
if(checkEOF()){
@@ -107,8 +128,8 @@ public class Parser {
private Statement ifStatement(){
Expression condition = expression();
if(matchOrError(TokenType.THEN, "then expected after if statement")){
- Statement.BlockStatement ifBlock = blockStatement();
- Statement.BlockStatement elseBlock=null;
+ Statement ifBlock = blockStatement();
+ Statement elseBlock=null;
if(matchAndAdvance(TokenType.ELSE)){
elseBlock=blockStatement();
@@ -133,7 +154,7 @@ public class Parser {
if(matchAndAdvance(TokenType.COMMA)){
step = expression();
}
- Statement.BlockStatement codeBlock = blockStatement();
+ Statement codeBlock = blockStatement();
matchOrError(TokenType.DO, "Do statements end with do");
return new Statement.DoStatement(variable, start, stop, step,codeBlock);
@@ -143,7 +164,7 @@ public class Parser {
matchOrError(TokenType.LEFT_PAREN, " missing '(' for do statement condition");
Expression condition = expression();
matchOrError(TokenType.RIGHT_PAREN, " missing ')' for do condition");
- Statement.BlockStatement codeBlock = blockStatement();
+ Statement codeBlock = blockStatement();
matchOrError(TokenType.DO, "Do while statements end with do");
return new Statement.DoWhileStatement(condition,codeBlock);
}
@@ -157,11 +178,7 @@ public class Parser {
Expression variable = expression();
if (matchAndAdvance(TokenType.EQUALS)){
Expression assignedvalue = expression();
-
- if (variable instanceof Expression.Variable){
- return new Expression.AssignmentExpression(((Expression.Variable)variable).name,assignedvalue);
- }
- throw error(getCurrentToken(),"Left of assignment must be a variable");
+ return new Expression.AssignmentExpression(variable,assignedvalue);
}
return variable;
}
@@ -240,7 +257,18 @@ public class Parser {
}
if (matchAndAdvance(TokenType.IDENTIFIER)) {
-
+ Token name= getPreviousToken();
+ if(matchAndAdvance(TokenType.LEFT_PAREN)){
+ List<Expression> positions = new ArrayList<>();
+ Expression position = expression();
+ positions.add(position);
+ while(matchAndAdvance(TokenType.COMMA)){
+ position = expression();
+ positions.add(position);
+ }
+ matchOrError(TokenType.RIGHT_PAREN,"Expected ')'");
+ return new Expression.ArrayVariable(name, positions);
+ }
return new Expression.Variable(getPreviousToken());
}