def test_InteriorNode_valueTest3(self): leaf1 = LeafNode(5) leaf2 = LeafNode(10) node1 = InteriorNode(Token('+'), leaf1, leaf2) node2 = InteriorNode(Token('*'), leaf1, node1) node3 = InteriorNode(Token('-'), node2, leaf2) self.assertEqual(node3.value(), 65)
def test_InteriorNode_postfixTest2(self): leaf1 = LeafNode(72) leaf2 = LeafNode(14) node1 = InteriorNode(Token('+'), leaf1, leaf2) node2 = InteriorNode(Token('*'), leaf1, node1) node3 = InteriorNode(Token('/'), node2, leaf2) s = "72 72 14 + * 14 /" self.assertEqual(node3.postfix(), s)
def test_InteriorNode_prefixTest2(self): leaf1 = LeafNode(72) leaf2 = LeafNode(14) node1 = InteriorNode(Token('+'), leaf1, leaf2) node2 = InteriorNode(Token('*'), leaf1, node1) node3 = InteriorNode(Token('/'), node2, leaf2) s = "/ * 72 + 72 14 14" self.assertEqual(node3.prefix(), s)
def test_InteriorNode_infixTest1(self): leaf1 = LeafNode(5) leaf2 = LeafNode(10) node1 = InteriorNode(Token('+'), leaf1, leaf2) node2 = InteriorNode(Token('*'), leaf1, node1) node3 = InteriorNode(Token('-'), node2, leaf2) s = "((5 * (5 + 10)) - 10)" self.assertEqual(node3.infix(), s)
def test_InteriorNode_postfixTest1(self): leaf1 = LeafNode(5) leaf2 = LeafNode(10) node1 = InteriorNode(Token('+'), leaf1, leaf2) node2 = InteriorNode(Token('*'), leaf1, node1) node3 = InteriorNode(Token('-'), node2, leaf2) s = "5 5 10 + * 10 -" print(node3.postfix()) self.assertEqual(node3.postfix(), s)
def test_InteriorNode_infixTest2(self): leaf1 = LeafNode(72) leaf2 = LeafNode(14) node1 = InteriorNode(Token('+'), leaf1, leaf2) node2 = InteriorNode(Token('*'), leaf1, node1) node3 = InteriorNode(Token('/'), node2, leaf2) s = "((72 * (72 + 14)) / 14)" print(node3.infix()) self.assertEqual(node3.infix(), s)
def primary(self): token = self.scanner.get() if token.getType() == Token.INT: tree = LeafNode(token.getValue()) self.scanner.next() elif token.getType() == Token.L_PAR: self.scanner.next() tree = self.expression() self.accept(self.scanner.get(), Token.R_PAR, "')' expected") self.scanner.next() else: tree = LeafNode(token.getValue()) self.fatalError(token, "bad primary") return tree
def _factor(self): token = self._scanner.get() if token.getType() == Token.INT: tree = LeafNode(token.getValue()) self._scanner.next() elif token.getType() == Token.L_PAR: self._scanner.next() tree = self._expression() self._accept(self._scanner.get(), Token.R_PAR, "')' expected") self._scanner.next() else: tree = None self._fatalError(token, "bad factor") return tree
def test_InteriorNode_valueTest1(self): leaf1 = LeafNode(5) leaf2 = LeafNode(10) node = InteriorNode(Token('+'), leaf1, leaf2) self.assertEqual(node.value(), 15)
def test_InteriorNode_valueTest2(self): leaf1 = LeafNode(5) leaf2 = LeafNode(10) node1 = InteriorNode(Token('+'), leaf1, leaf2) node2 = InteriorNode(Token('*'), leaf1, node1) self.assertEqual(node2.value(), 75)