loopwhilecom. now works
This commit is contained in:
@@ -181,10 +181,28 @@ public final class Encoder implements ActualParameterVisitor<Frame, Integer>,
|
||||
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<Frame, Integer>,
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -496,6 +496,17 @@ public class ConstantFolder implements ActualParameterVisitor<Void, AbstractSynt
|
||||
}
|
||||
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
|
||||
// @Override
|
||||
@@ -596,11 +607,4 @@ public class ConstantFolder implements ActualParameterVisitor<Void, AbstractSynt
|
||||
// any unhandled situation (i.e., not foldable) is ignored
|
||||
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.EOT:
|
||||
case Token.RCURLY:
|
||||
|
||||
finish(commandPos);
|
||||
commandAST = new EmptyCommand(commandPos);
|
||||
break;
|
||||
|
||||
@@ -169,7 +169,7 @@ public class LayoutVisitor implements ActualParameterVisitor<Void, DrawingTree>,
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user