summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAidenRushbrooke <72034940+AidenRushbrooke@users.noreply.github.com>2021-12-07 23:07:23 +0000
committerAidenRushbrooke <72034940+AidenRushbrooke@users.noreply.github.com>2021-12-07 23:07:23 +0000
commit5c4d41c63af3ef064090336a14efe91931051e7e (patch)
treeccb7cf199040b2c7c7a57ace1692dc0046afa1e4
parent6279da9b8ede22a77161e9504d2482aba33ec2e8 (diff)
downloadesotericFORTRAN-5c4d41c63af3ef064090336a14efe91931051e7e.tar.gz
esotericFORTRAN-5c4d41c63af3ef064090336a14efe91931051e7e.zip
Fixed known issues
-rw-r--r--src/Compiler/Expression.java4
-rw-r--r--src/Compiler/Language.java7
-rw-r--r--src/Compiler/Parser.java29
-rw-r--r--src/Compiler/Translator.java10
-rw-r--r--src/examples/example.ft4
5 files changed, 28 insertions, 26 deletions
diff --git a/src/Compiler/Expression.java b/src/Compiler/Expression.java
index 056019a..c71874b 100644
--- a/src/Compiler/Expression.java
+++ b/src/Compiler/Expression.java
@@ -140,11 +140,11 @@ abstract class Expression {
* Class for representing a function call
*/
static class FunctionCall extends Expression{
- FunctionCall(Token name, List<Token> arguments){
+ FunctionCall(Token name, List<Expression> arguments){
this.arguments=arguments;
this.name=name;
}
- final List<Token> arguments;
+ final List<Expression> arguments;
final Token name;
@Override
diff --git a/src/Compiler/Language.java b/src/Compiler/Language.java
index 5013cfd..549ea29 100644
--- a/src/Compiler/Language.java
+++ b/src/Compiler/Language.java
@@ -134,6 +134,13 @@ public class Language {
System.out.println("An error was encountered on line: "+line);
System.out.println(message);
}
+
+
+ static void displayError(String message){
+ hadError=true;
+ System.out.println("An error was encountered");
+ System.out.println(message);
+ }
/**
* Method for displaying error based on a specific token
* @param token the token the parser detected the error on
diff --git a/src/Compiler/Parser.java b/src/Compiler/Parser.java
index 7d46063..c7b0165 100644
--- a/src/Compiler/Parser.java
+++ b/src/Compiler/Parser.java
@@ -229,14 +229,12 @@ public class Parser {
//Extract the size of the array
List<Expression> dimensions = new ArrayList<>();
- Expression dimension = expression();
- dimensions.add(dimension);
//Extract the size of each dimension of the array
- while(matchAndAdvance(TokenType.COMMA)){
- dimension = expression();
+ do{
+ Expression dimension = expression();
dimensions.add(dimension);
- }
+ } while(matchAndAdvance(TokenType.COMMA));
matchOrError(TokenType.RIGHT_PAREN, "Expected ')'");
matchOrError(TokenType.DEFINE, ":: Required for variable definition");
@@ -519,28 +517,21 @@ public class Parser {
if(definedVars.containsKey(name.text)){
if(definedVars.get(name.text).equals("array")){
List<Expression> positions = new ArrayList<>();
- Expression position = expression();
- positions.add(position);
//Parse array positions for each dimension
- while(matchAndAdvance(TokenType.COMMA)){
- position = expression();
+ do{
+ Expression position = expression();
positions.add(position);
- }
+ }while (matchAndAdvance(TokenType.COMMA));
matchOrError(TokenType.RIGHT_PAREN,"Expected ')'");
return new Expression.ArrayVariable(name, positions);
}
}
//If not previously declared, assume function call
- List<Token> arguments = new ArrayList<>();
+ List<Expression> arguments = new ArrayList<>();
//Parse function call arguments
do{
- matchOrError(TokenType.IDENTIFIER, "Expected argument");
- Token argumentValue = getPreviousToken();
- if(definedVars.containsKey(argumentValue.text)){
- arguments.add(argumentValue);
- }else{
- throw error(argumentValue, "Argument undefined");
- }
+ Expression argument = expression();
+ arguments.add(argument);
}while(matchAndAdvance(TokenType.COMMA));
matchOrError(TokenType.RIGHT_PAREN, "Expected ')");
return new Expression.FunctionCall(name, arguments);
@@ -638,6 +629,4 @@ public class Parser {
Language.displayError(token ,message);
return new Error();
}
-
-
}
diff --git a/src/Compiler/Translator.java b/src/Compiler/Translator.java
index 6f5f050..5b7d52e 100644
--- a/src/Compiler/Translator.java
+++ b/src/Compiler/Translator.java
@@ -236,6 +236,10 @@ public class Translator{
//Write each dimension of the array
for(Expression expr:arraydec.dimensions){
+ if(expr.getExpressionType()!="literal"){
+ Language.displayError(arraydec.name, "Array sizes must be defined using constants");
+ throw new Error();
+ }
arrayString+="[";
arrayString+=evaluateExpression(expr);
arrayString+="]";
@@ -293,6 +297,8 @@ public class Translator{
types+="%f";
} else if (exprType.equals("string")){
types+="%s";
+ } else{
+ Language.displayError("Unknown expression type in print statement");
}
//Add the actual expression value
values+=evaluateExpression(expr);
@@ -527,11 +533,11 @@ public class Translator{
boolean first=true;
//Write each argument of the function call
- for(Token arg:expr.arguments){
+ for(Expression arg:expr.arguments){
if(!first){
functioncall+=",";
}
- functioncall+=arg.text;
+ functioncall+=evaluateExpression(arg);
first=false;
}
functioncall+=")";
diff --git a/src/examples/example.ft b/src/examples/example.ft
index 1d41963..571cace 100644
--- a/src/examples/example.ft
+++ b/src/examples/example.ft
@@ -1,8 +1,8 @@
program example
real::a
real::root
-a=25
-root=sqrt(a)
+a=30
+root=sqrt(25+5)
print*,"The square root of ",a," is ",root
end program example