loopwhilecom. now works

main
simonkellet 1 year ago
parent a3f6b13947
commit c505b9cb2c
  1. 6
      .classpath
  2. 1
      .gitignore
  3. 8
      .project
  4. 24
      Triangle.Compiler/src/main/java/triangle/codeGenerator/Encoder.java
  5. 18
      Triangle.Compiler/src/main/java/triangle/optimiser/ConstantFolder.java
  6. 1
      Triangle.Compiler/src/main/java/triangle/syntacticAnalyzer/Parser.java
  7. 2
      Triangle.Compiler/src/main/java/triangle/treeDrawer/LayoutVisitor.java
  8. 5
      Triangle.Compiler/src/test/java/triangle/syntacticAnalyser/TestScanner.java
  9. BIN
      build/libs/Triangle-Tools.jar
  10. BIN
      loopwhile.tam
  11. 4
      programs/loopwhile.tri

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>

1
.gitignore vendored

@ -60,3 +60,4 @@ local.properties
# Typically, this file would be tracked if it contains build/dependency configurations: # Typically, this file would be tracked if it contains build/dependency configurations:
#.project #.project
/.gradle/ /.gradle/
/build/

@ -1,10 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<projectDescription> <projectDescription>
<name>a5-triangle-tools</name> <name>Triangle-Tools</name>
<comment>Project a5-triangle-tools created by Buildship.</comment> <comment>Project a5-triangle-tools created by Buildship.</comment>
<projects> <projects>
</projects> </projects>
<buildSpec> <buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand> <buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name> <name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments> <arguments>
@ -12,6 +17,7 @@
</buildCommand> </buildCommand>
</buildSpec> </buildSpec>
<natures> <natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature> <nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures> </natures>
</projectDescription> </projectDescription>

@ -181,11 +181,29 @@ public final class Encoder implements ActualParameterVisitor<Frame, Integer>,
var loopAddr = emitter.getNextInstrAddr(); var loopAddr = emitter.getNextInstrAddr();
ast.C.visit(this, frame); ast.C.visit(this, frame);
emitter.patch(jumpAddr); emitter.patch(jumpAddr);
ast.E.visit(this, frame); ast.E.visit(this, frame);
emitter.emit(OpCode.JUMPIF, Machine.trueRep, Register.CB, loopAddr); emitter.emit(OpCode.JUMPIF, Machine.trueRep, Register.CB, loopAddr);
return null; 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 // Expressions
@Override @Override
public Integer visitArrayExpression(ArrayExpression ast, Frame frame) { public Integer visitArrayExpression(ArrayExpression ast, Frame frame) {
@ -794,10 +812,4 @@ public final class Encoder implements ActualParameterVisitor<Frame, Integer>,
var baseObject = (AddressableEntity) V.visit(this, frame); var baseObject = (AddressableEntity) V.visit(this, frame);
baseObject.encodeFetchAddress(emitter, frame, V); baseObject.encodeFetchAddress(emitter, frame, V);
} }
@Override
public Void visitLoopWhile(LoopWhile loopWhile, Frame arg) {
// TODO Auto-generated method stub
return null;
}
} }

@ -497,6 +497,17 @@ public class ConstantFolder implements ActualParameterVisitor<Void, AbstractSynt
return null; return null;
} }
@Override
public AbstractSyntaxTree visitLoopWhile(LoopWhile ast, Void arg) {
ast.C1.visit(this);
AbstractSyntaxTree replacement = ast.E.visit(this);
if (replacement != null) {
ast.E = (Expression) replacement;
}
ast.C2.visit(this);
return null;
}
// TODO uncomment if you've implemented the repeat command // TODO uncomment if you've implemented the repeat command
// @Override // @Override
// public AbstractSyntaxTree visitRepeatCommand(RepeatCommand ast, Void arg) { // public AbstractSyntaxTree visitRepeatCommand(RepeatCommand ast, Void arg) {
@ -596,11 +607,4 @@ public class ConstantFolder implements ActualParameterVisitor<Void, AbstractSynt
// any unhandled situation (i.e., not foldable) is ignored // any unhandled situation (i.e., not foldable) is ignored
return null; return null;
} }
@Override
public AbstractSyntaxTree visitLoopWhile(LoopWhile loopWhile, Void arg) {
// TODO Auto-generated method stub
return null;
}
} }

@ -386,7 +386,6 @@ public class Parser {
case Token.IN: case Token.IN:
case Token.EOT: case Token.EOT:
case Token.RCURLY: case Token.RCURLY:
finish(commandPos); finish(commandPos);
commandAST = new EmptyCommand(commandPos); commandAST = new EmptyCommand(commandPos);
break; break;

@ -169,7 +169,7 @@ public class LayoutVisitor implements ActualParameterVisitor<Void, DrawingTree>,
var d1 = ast.C1.visit(this); var d1 = ast.C1.visit(this);
var d2 = ast.E.visit(this); var d2 = ast.E.visit(this);
var d3 = ast.C2.visit(this); var d3 = ast.C2.visit(this);
return layoutTernary("LoopWhile.", d1, d2, d3); return layoutTernary("LoopWhileCom.", d1, d2, d3);
} }

@ -53,6 +53,11 @@ public class TestScanner {
compileExpectSuccess("/while-curly.tri"); compileExpectSuccess("/while-curly.tri");
} }
@Test
public void testLoopWhileDoCommand() {
compileExpectSuccess("/loopwhile.tri");
}
private void compileExpectSuccess(String filename) { private void compileExpectSuccess(String filename) {
// build.gradle has a line sourceSets.test.resources.srcDir file("$rootDir/programs") // 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 // which adds the programs directory to the list of places Java can easily find files

Binary file not shown.

Binary file not shown.

@ -6,9 +6,6 @@ let
in in
begin begin
a := 0; a := 0;
b := 10;
putint(b);
loop loop
begin begin
put('a'); put('a');
@ -16,5 +13,4 @@ begin
end end
while a < 5 do while a < 5 do
put('b'); put('b');
end end

Loading…
Cancel
Save