Fixing bug where parse tree was only growing to the left

This commit is contained in:
Chris Stewart 2016-01-28 16:49:23 -06:00
parent caaf98c43e
commit 171053bf94
2 changed files with 19 additions and 13 deletions

View file

@ -166,8 +166,12 @@ trait ControlOperationsInterpreter {
val lastOpElseIndex = findLastOpElse(t) val lastOpElseIndex = findLastOpElse(t)
if (lastOpEndIfIndex.isDefined && !t.contains(OP_IF)) { if (lastOpEndIfIndex.isDefined && !t.contains(OP_IF)) {
val opIfExpression = t.slice(0, lastOpEndIfIndex.get)
val restOfScript = t.slice(lastOpEndIfIndex.get, script.size) val opElseIndex : Option[Int] = findFirstOpElse(t)
val opIfExpression = if (opElseIndex.isDefined) t.slice(0,opElseIndex.get) else t.slice(0, lastOpEndIfIndex.get)
val restOfScript = if (opElseIndex.isDefined) t.slice(opElseIndex.get, script.size) else t.slice(lastOpEndIfIndex.get, script.size)
logger.debug("OP_IF Expression: " + opIfExpression)
logger.debug("rest of script: " + restOfScript)
Node(OP_IF, loop(opIfExpression), loop(restOfScript)) Node(OP_IF, loop(opIfExpression), loop(restOfScript))
} else if (lastOpElseIndex.isDefined) { } else if (lastOpElseIndex.isDefined) {
val opIfExpression = t.slice(0,lastOpElseIndex.get) val opIfExpression = t.slice(0,lastOpElseIndex.get)

View file

@ -115,45 +115,47 @@ class ControlOperationsInterpreterTest extends FlatSpec with MustMatchers with C
parseBinaryTree(script0).toSeq must be (script0) parseBinaryTree(script0).toSeq must be (script0)
val script1 = List(OP_IF,OP_0,OP_ELSE,OP_1,OP_ENDIF) val script1 = List(OP_IF,OP_0,OP_ELSE,OP_1,OP_ENDIF)
val bTree1 = parseBinaryTree(script1)
parseBinaryTree(script1).toSeq must be (script1) bTree1.toSeq must be (script1)
val script2 = List(OP_IF,OP_ELSE, OP_ELSE,OP_ENDIF) val script2 = List(OP_IF,OP_ELSE, OP_ELSE,OP_ENDIF)
parseBinaryTree(script2).toSeq must be (script2) parseBinaryTree(script2).toSeq must be (script2)
val script3 = List(OP_IF, OP_1, OP_ELSE, OP_0, OP_ENDIF) val script3 = List(OP_IF, OP_1, OP_ELSE, OP_0, OP_ENDIF)
parseBinaryTree(script3).toSeq must be (script3) val bTree3 = parseBinaryTree(script3)
bTree3.toSeq must be (script3)
val script = List(OP_IF, OP_IF, OP_0, OP_ELSE, OP_1, OP_ENDIF, OP_ELSE, OP_IF, OP_2, OP_ELSE, OP_3, OP_ENDIF, OP_ENDIF) val script = List(OP_IF, OP_IF, OP_0, OP_ELSE, OP_1, OP_ENDIF, OP_ELSE, OP_IF, OP_2, OP_ELSE, OP_3, OP_ENDIF, OP_ENDIF)
parseBinaryTree(script).toSeq must be (script) parseBinaryTree(script).toSeq must be (script)
} }
/* it must "parse a script into a binary tree and have the OP_IF expression on the left branch and the OP_ELSE expression on the right branch"in { it must "parse a script into a binary tree and have the OP_IF expression on the left branch and the OP_ELSE expression on the right branch"in {
val script = List(OP_IF,OP_0,OP_ELSE,OP_1,OP_ENDIF) val script = List(OP_IF,OP_0,OP_ELSE,OP_1,OP_ENDIF)
val bTree = parseBinaryTree(script) val bTree = parseBinaryTree(script)
bTree.value.get must be (OP_IF) bTree.value.get must be (OP_IF)
bTree.left.isDefined must be (true) bTree.left.isDefined must be (true)
bTree.left.get must be (OP_0) bTree.left.get.value must be (Some(OP_0))
bTree.right.isDefined must be (true) bTree.right.isDefined must be (true)
bTree.right.get must be (OP_ELSE) bTree.right.get.value must be (Some(OP_ELSE))
bTree.right.get.left.isDefined must be (true) bTree.right.get.left.isDefined must be (true)
bTree.right.get.left.get must be (OP_1) bTree.right.get.left.get.value must be (Some(OP_1))
bTree.right.get.right.get must be (OP_ENDIF) bTree.right.get.right.isDefined must be (true)
}*/ bTree.right.get.right.get.value must be (Some(OP_ENDIF))
}
it must "evaluate an OP_IF block correctly if the stack top is true" in { /* it must "evaluate an OP_IF block correctly if the stack top is true" in {
val stack = List(OP_1) val stack = List(OP_1)
val script = List(OP_IF, OP_1, OP_ELSE, OP_0, OP_ENDIF) val script = List(OP_IF, OP_1, OP_ELSE, OP_0, OP_ENDIF)
val (newStack,newScript) = opIf(stack,script) val (newStack,newScript) = opIf(stack,script)
newStack must be (List()) newStack must be (List())
newScript must be (List(OP_1, OP_ENDIF)) newScript must be (List(OP_1, OP_ENDIF))
} }*/
/*it must "evaluate a weird case using multiple OP_ELSEs" in { /*it must "evaluate a weird case using multiple OP_ELSEs" in {
val stack = List(ScriptNumberImpl(1)) val stack = List(ScriptNumberImpl(1))