Skip to content

Commit

Permalink
else statements
Browse files Browse the repository at this point in the history
  • Loading branch information
maxattack committed Aug 3, 2011
1 parent d387d15 commit 94ed7ec
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Lang/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,32 @@ public Block(List<Instruction> buffer) {
public class IfBlock : Block {
public BranchInstruction branch;
public int branchIndex;
public JumpInstruction elseJump = null;

public IfBlock(Expression condition, List<Instruction> buffer) : base(buffer) {
branchIndex = buffer.Count;
branch = new BranchInstruction(condition, branchIndex+1, -1);
buffer.Add(branch);
}

override public void End() {
public bool ElseIf(Expression condition) {
return false;
}

public bool Else() {
if (elseJump != null) { return false; }
elseJump = new JumpInstruction(-1);
buffer.Add(elseJump);
branch.indexFalse = buffer.Count;
return true;
}

override public void End() {
if (elseJump != null) {
elseJump.index = buffer.Count;
} else {
branch.indexFalse = buffer.Count;
}
}

// todo add elseif, else
Expand Down
6 changes: 6 additions & 0 deletions Lang/InstructionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ State Idle(Token t) {
if (mBlock.Count == 0) { return null; }
mBlock.Pop().End();
return Idle;
case "else":
if (mBlock.Count == 0) { return null; }
var block = mBlock.Peek() as IfBlock;
if (block == null) { return null; }
if (!block.Else()) { return null; }
return Idle;
default: break;
}
}
Expand Down
5 changes: 5 additions & 0 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ turn left
move backward 5
stop stroke
end
if 0
move forward 100
else
move backward 100
end
");
var interpreter = new Interpreter(program);
while(interpreter.ExecuteNextInstruction()) {}
Expand Down

0 comments on commit 94ed7ec

Please sign in to comment.