summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAidenRushbrooke <72034940+AidenRushbrooke@users.noreply.github.com>2021-11-20 02:24:52 +0000
committerAidenRushbrooke <72034940+AidenRushbrooke@users.noreply.github.com>2021-11-20 02:24:52 +0000
commit68f651f90ac9bb42e6fcb461f72f82ee74df1fdd (patch)
treea94d69ae43cdc92395f1dbac3160bb4d45baa570 /src
parent286e177e603d57d445393a0f4899bf7a17a4c31d (diff)
downloadesotericFORTRAN-68f651f90ac9bb42e6fcb461f72f82ee74df1fdd.tar.gz
esotericFORTRAN-68f651f90ac9bb42e6fcb461f72f82ee74df1fdd.zip
Added initial support for do while loops
Diffstat (limited to 'src')
-rw-r--r--src/Compiler/Parser.java12
-rw-r--r--src/Compiler/Statement.java16
-rw-r--r--src/Compiler/TokenScanner.java1
-rw-r--r--src/Compiler/TokenType.java2
-rw-r--r--src/Compiler/Translator.java9
-rw-r--r--src/example.txt20
6 files changed, 47 insertions, 13 deletions
diff --git a/src/Compiler/Parser.java b/src/Compiler/Parser.java
index 5288c67..8bb8951 100644
--- a/src/Compiler/Parser.java
+++ b/src/Compiler/Parser.java
@@ -122,6 +122,9 @@ public class Parser {
}
private Statement doStatement(){
+ if(matchAndAdvance(TokenType.WHILE)){
+ return whileStatement();
+ }
Expression variable =expression();
matchOrError(TokenType.EQUALS, "'=' missing");
Expression start = expression();
@@ -137,6 +140,15 @@ public class Parser {
}
+ private Statement whileStatement(){
+ matchOrError(TokenType.LEFT_PAREN, " missing '(");
+ Expression condition = expression();
+ matchOrError(TokenType.RIGHT_PAREN, " missing ')");
+ Statement.BlockStatement codeBlock = blockStatement();
+ matchOrError(TokenType.DO, "Do while statements end with do");
+ return new Statement.DoWhileStatement(condition,codeBlock);
+ }
+
private Statement expressionStatement(){
Expression expression = assignment();
return new Statement.ExpressionStatement(expression);
diff --git a/src/Compiler/Statement.java b/src/Compiler/Statement.java
index 10ed878..a3c0960 100644
--- a/src/Compiler/Statement.java
+++ b/src/Compiler/Statement.java
@@ -71,6 +71,22 @@ abstract class Statement {
}
}
+ static class DoWhileStatement extends Statement{
+ DoWhileStatement(Expression condition,BlockStatement codeBlock){
+ this.condition=condition;
+ this.codeBlock=codeBlock;
+
+ }
+
+ final Expression condition;
+ final BlockStatement codeBlock;
+
+ @Override
+ public String getStatmentType() {
+ return "dowhileStmt";
+ }
+ }
+
static class VariableDeclaration extends Statement{
VariableDeclaration(Token name,String type){
diff --git a/src/Compiler/TokenScanner.java b/src/Compiler/TokenScanner.java
index 07994d0..49500c5 100644
--- a/src/Compiler/TokenScanner.java
+++ b/src/Compiler/TokenScanner.java
@@ -202,5 +202,6 @@ public class TokenScanner {
keywords.put("end", TokenType.END);
keywords.put("else", TokenType.ELSE);
keywords.put("do", TokenType.DO);
+ keywords.put("while", TokenType.WHILE);
}
}
diff --git a/src/Compiler/TokenType.java b/src/Compiler/TokenType.java
index 403cfe6..82776f9 100644
--- a/src/Compiler/TokenType.java
+++ b/src/Compiler/TokenType.java
@@ -12,7 +12,7 @@ public enum TokenType {
NUMBER,IDENTIFIER,STRING,
- INT,REAL,PRINT,ENDPRINT,IF,THEN,END,ELSE,LEN,DO,
+ INT,REAL,PRINT,ENDPRINT,IF,THEN,END,ELSE,LEN,DO,WHILE,
EOF
}
diff --git a/src/Compiler/Translator.java b/src/Compiler/Translator.java
index 4c5da89..c5c5bc7 100644
--- a/src/Compiler/Translator.java
+++ b/src/Compiler/Translator.java
@@ -56,6 +56,9 @@ public class Translator{
case "doStmt":
evalDoStatement((DoStatement)statement);
break;
+ case "dowhileStmt":
+ evalDoWhileStatement((DoWhileStatement)statement);
+ break;
}
}
private void evalExpressionStatement(ExpressionStatement stmt){
@@ -137,6 +140,12 @@ public class Translator{
CCode.add("}");
}
+ private void evalDoWhileStatement(DoWhileStatement dowhilestatement){
+ CCode.add("while("+evaluateExpression(dowhilestatement.condition)+"){");
+ evaluateStatement(dowhilestatement.codeBlock);
+ CCode.add("}");
+ }
+
private String evaluateExpression(Expression expression){
switch(expression.getExpressionType()){
case "binary":
diff --git a/src/example.txt b/src/example.txt
index eb453b2..bbc8973 100644
--- a/src/example.txt
+++ b/src/example.txt
@@ -1,13 +1,9 @@
-int ::temp
-int::a
-int::b
-int::i
-a=1
-b=1
-i=0
-do i=0,10
-print*,a," " endprint
-temp =a+b
-a=b
-b=temp
+int::nfact
+int::n
+nfact=1
+n=1
+do while(n<10)
+nfact=nfact*n
+n=n+1
+print*,n," ", nfact endprint
end do \ No newline at end of file