Created
July 31, 2015 22:23
-
-
Save aristotaloss/b9e7f8acd76b3d4756dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nl.bartpelle.adder.ast; | |
import nl.bartpelle.adder.AdderCtx; | |
import nl.bartpelle.adder.Instruction; | |
import nl.bartpelle.adder.Label; | |
import nl.bartpelle.adder.enums.Type; | |
import java.util.List; | |
/** | |
* Created by Bart Pelle on 8/21/2014. | |
*/ | |
public class IfNode implements AstNode { | |
public AstNode condition; | |
public AstNode trueArm; | |
public AstNode falseArm; | |
public IfNode(AstNode condition, AstNode trueArm, AstNode falseArm) { | |
this.condition = condition; | |
this.trueArm = trueArm; | |
this.falseArm = falseArm; | |
} | |
@Override | |
public void dasm() { | |
condition.dasm(); | |
System.out.println("iftrue"); | |
trueArm.dasm(); | |
if (falseArm != null) { | |
System.out.println("else"); | |
falseArm.dasm(); | |
} | |
} | |
@Override public Type type() { | |
return Type.VOID; | |
} | |
@Override public void encode(List<Instruction> out, AdderCtx ctx) { | |
Label ifFalse = Label.unique(); | |
Label endOfFalse = Label.unique(); | |
if (condition.type() != Type.BOOL) | |
throw new RuntimeException("condition cannot be casted to boolean (type: " + condition.type() + ", condition: " + condition + ")"); | |
//encodeBranching(condition, ifFalse, endOfFalse, out, ctx); | |
condition.encode(out, ctx); | |
/* Jump if zero to false */ | |
Instruction jz = new Instruction(Instruction.Opcode.JZ, 0); | |
out.add(jz); | |
ifFalse.promises.add((pos) -> jz.i = ctx.cpoolInt(pos)); | |
trueArm.encode(out, ctx); | |
Instruction j = new Instruction(Instruction.Opcode.JMP, 0); | |
out.add(j); | |
endOfFalse.promises.add((pos) -> j.i = ctx.cpoolInt(pos)); | |
out.add(ifFalse); | |
if (falseArm != null) | |
falseArm.encode(out, ctx); | |
out.add(endOfFalse); | |
} | |
private void encodeBranching(AstNode expr, Label ifFalse, Label endOfFalse, List<Instruction> out, AdderCtx ctx) { | |
System.out.println("Shit we have " + expr.getClass().getSimpleName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment