diff options
Diffstat (limited to 'src/Compiler')
-rw-r--r-- | src/Compiler/Parser.java | 12 | ||||
-rw-r--r-- | src/Compiler/Statement.java | 16 | ||||
-rw-r--r-- | src/Compiler/TokenScanner.java | 1 | ||||
-rw-r--r-- | src/Compiler/TokenType.java | 2 | ||||
-rw-r--r-- | src/Compiler/Translator.java | 9 |
5 files changed, 39 insertions, 1 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": |