From 68f651f90ac9bb42e6fcb461f72f82ee74df1fdd Mon Sep 17 00:00:00 2001
From: AidenRushbrooke <72034940+AidenRushbrooke@users.noreply.github.com>
Date: Sat, 20 Nov 2021 02:24:52 +0000
Subject: Added initial support for do while loops

---
 src/Compiler/Parser.java       | 12 ++++++++++++
 src/Compiler/Statement.java    | 16 ++++++++++++++++
 src/Compiler/TokenScanner.java |  1 +
 src/Compiler/TokenType.java    |  2 +-
 src/Compiler/Translator.java   |  9 +++++++++
 src/example.txt                | 20 ++++++++------------
 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
-- 
cgit v1.2.3