diff --git a/.classpath b/.classpath
new file mode 100644
index 0000000..4a04201
--- /dev/null
+++ b/.classpath
@@ -0,0 +1,6 @@
+
+
+
+
+
+
diff --git a/.gitignore b/.gitignore
index 7ff3503..6952112 100644
--- a/.gitignore
+++ b/.gitignore
@@ -60,3 +60,4 @@ local.properties
# Typically, this file would be tracked if it contains build/dependency configurations:
#.project
/.gradle/
+/build/
diff --git a/.project b/.project
index 4beb8b6..85e55a8 100644
--- a/.project
+++ b/.project
@@ -1,10 +1,15 @@
- a5-triangle-tools
+ Triangle-Tools
Project a5-triangle-tools created by Buildship.
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
org.eclipse.buildship.core.gradleprojectbuilder
@@ -12,6 +17,7 @@
+ org.eclipse.jdt.core.javanature
org.eclipse.buildship.core.gradleprojectnature
diff --git a/Triangle.Compiler/src/main/java/triangle/codeGenerator/Encoder.java b/Triangle.Compiler/src/main/java/triangle/codeGenerator/Encoder.java
index 7d1fd6e..6930f5d 100644
--- a/Triangle.Compiler/src/main/java/triangle/codeGenerator/Encoder.java
+++ b/Triangle.Compiler/src/main/java/triangle/codeGenerator/Encoder.java
@@ -181,10 +181,28 @@ public final class Encoder implements ActualParameterVisitor,
var loopAddr = emitter.getNextInstrAddr();
ast.C.visit(this, frame);
emitter.patch(jumpAddr);
+
ast.E.visit(this, frame);
emitter.emit(OpCode.JUMPIF, Machine.trueRep, Register.CB, loopAddr);
return null;
}
+
+ @Override
+ public Void visitLoopWhile(LoopWhile ast, Frame frame) {
+ var loopAddr = emitter.getNextInstrAddr();
+ ast.C1.visit(this, frame); //(LOOP = C1)
+
+ ast.E.visit(this, frame); //check if the while is false first (WHILE = E)
+ var jumpIfAddr = emitter.emit(OpCode.JUMPIF, Machine.falseRep, Register.CB, 0);
+ //store this address
+
+ ast.C2.visit(this, frame); // visit the do section now (DO = C2)
+ emitter.emit(OpCode.JUMP, Machine.trueRep, Register.CB, loopAddr); //jump back to C1
+
+ emitter.patch(jumpIfAddr); //patch the jmpIfAddr to go back to the while section
+
+ return null;
+ }
// Expressions
@Override
@@ -794,10 +812,4 @@ public final class Encoder implements ActualParameterVisitor,
var baseObject = (AddressableEntity) V.visit(this, frame);
baseObject.encodeFetchAddress(emitter, frame, V);
}
-
- @Override
- public Void visitLoopWhile(LoopWhile loopWhile, Frame arg) {
- // TODO Auto-generated method stub
- return null;
- }
}
diff --git a/Triangle.Compiler/src/main/java/triangle/optimiser/ConstantFolder.java b/Triangle.Compiler/src/main/java/triangle/optimiser/ConstantFolder.java
index 8158674..dbf4141 100644
--- a/Triangle.Compiler/src/main/java/triangle/optimiser/ConstantFolder.java
+++ b/Triangle.Compiler/src/main/java/triangle/optimiser/ConstantFolder.java
@@ -496,6 +496,17 @@ public class ConstantFolder implements ActualParameterVisitor,
var d1 = ast.C1.visit(this);
var d2 = ast.E.visit(this);
var d3 = ast.C2.visit(this);
- return layoutTernary("LoopWhile.", d1, d2, d3);
+ return layoutTernary("LoopWhileCom.", d1, d2, d3);
}
diff --git a/Triangle.Compiler/src/test/java/triangle/syntacticAnalyser/TestScanner.java b/Triangle.Compiler/src/test/java/triangle/syntacticAnalyser/TestScanner.java
index 5113b97..70c2acc 100644
--- a/Triangle.Compiler/src/test/java/triangle/syntacticAnalyser/TestScanner.java
+++ b/Triangle.Compiler/src/test/java/triangle/syntacticAnalyser/TestScanner.java
@@ -53,6 +53,11 @@ public class TestScanner {
compileExpectSuccess("/while-curly.tri");
}
+ @Test
+ public void testLoopWhileDoCommand() {
+ compileExpectSuccess("/loopwhile.tri");
+ }
+
private void compileExpectSuccess(String filename) {
// build.gradle has a line sourceSets.test.resources.srcDir file("$rootDir/programs")
// which adds the programs directory to the list of places Java can easily find files
diff --git a/build/libs/Triangle-Tools.jar b/build/libs/Triangle-Tools.jar
index ee0b873..03b15f9 100644
Binary files a/build/libs/Triangle-Tools.jar and b/build/libs/Triangle-Tools.jar differ
diff --git a/loopwhile.tam b/loopwhile.tam
index b8dd1cb..0f721de 100644
Binary files a/loopwhile.tam and b/loopwhile.tam differ
diff --git a/programs/loopwhile.tri b/programs/loopwhile.tri
index 156fd05..bfaba26 100644
--- a/programs/loopwhile.tri
+++ b/programs/loopwhile.tri
@@ -6,9 +6,6 @@ let
in
begin
a := 0;
- b := 10;
- putint(b);
-
loop
begin
put('a');
@@ -16,5 +13,4 @@ begin
end
while a < 5 do
put('b');
-
end