Пример #1
0
 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)
Пример #2
0
 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)
Пример #3
0
 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)
Пример #4
0
 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)
Пример #5
0
 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)
Пример #6
0
 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)
Пример #7
0
 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
Пример #8
0
 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
Пример #9
0
 def test_InteriorNode_valueTest1(self):
     leaf1 = LeafNode(5)
     leaf2 = LeafNode(10)
     node = InteriorNode(Token('+'), leaf1, leaf2)
     self.assertEqual(node.value(), 15)
Пример #10
0
 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)