Exemple #1
0
    def test_strings(self):
        # x # ('test' # '123') # ''
        tree = \
            StringBinary(
                StringBinary(
                    Identifier('x'),
                    TokenType.HASH,
                    Grouping(
                        StringBinary(
                            Literal('test'),
                            TokenType.HASH,
                            Literal('123')
                        )
                    )
                ),
                TokenType.HASH,
                Literal('')
            )
        interpreter = Interpreter(Environment({'x': 'hello'}))
        self.assertEqual('hellotest123', interpreter.interpret_expr(tree))

        # 'asd' # 5 + 3 # true and false # x
        tree = \
            StringBinary(
                StringBinary(
                    StringBinary(
                        Literal('asd'),
                        TokenType.HASH,
                        Binary(
                            Literal(5),
                            TokenType.PLUS,
                            Literal(3)
                        )
                    ),
                    TokenType.HASH,
                    LogicalBinary(
                        Literal(True),
                        TokenType.AND,
                        Literal(False)
                    )
                ),
                TokenType.HASH,
                Identifier('x')
            )
        interpreter = Interpreter(Environment({'x': 'hello'}))
        self.assertEqual('asd8falsehello', interpreter.interpret_expr(tree))

        # 1.2
        tree = \
            StringBinary(Literal(1), TokenType.HASH, Literal(2))
        interpreter = Interpreter()
        self.assertEqual('12', interpreter.interpret_expr(tree))
Exemple #2
0
 def test_identifiers(self):
     # 5 ^ x + test123 * (7.2 - asd)
     tree = \
         Binary(
             Binary(
                 Literal(5),
                 TokenType.POW,
                 Identifier('x')
             ),
             TokenType.PLUS,
             Binary(
                 Identifier('test123'),
                 TokenType.MUL,
                 Grouping(
                     Binary(
                         Literal(7.2),
                         TokenType.MINUS,
                         Identifier('asd')
                     )
                 )
             )
         )
     env = {'x': -1, 'test123': 7, 'asd': -3}
     interpreter = Interpreter(env)
     self.assertAlmostEqual(71.6, interpreter.interpret_expr(tree))
Exemple #3
0
    def test_booleans(self):
        # not true and false or not false
        tree = \
            LogicalBinary(
                LogicalBinary(
                    LogicalUnary(
                        TokenType.NOT,
                        Literal(True)
                    ),
                    TokenType.AND,
                    Literal(False)
                ),
                TokenType.OR,
                LogicalUnary(
                    TokenType.NOT,
                    Literal(False)
                )
            )
        interpreter = Interpreter()
        self.assertEqual(True, interpreter.interpret_expr(tree))

        # 5 <= 3 = 7 > 5 - 2
        tree = \
            Comparison(
                Comparison(
                    Literal(5),
                    TokenType.LE,
                    Literal(3)
                ),
                TokenType.EQUAL,
                Comparison(
                    Literal(7),
                    TokenType.G,
                    Binary(
                        Literal(5),
                        TokenType.MINUS,
                        Literal(2)
                    )
                )
            )
        interpreter = Interpreter()
        self.assertEqual(False, interpreter.interpret_expr(tree))
Exemple #4
0
 def test_arithmetic(self):
     # 5 * 2 + 3 * 8 ^ 2 * 2 - (10 / 5 - 2)
     tree = \
         Binary(
             Binary(
                 Binary(
                     Literal(5),
                     TokenType.MUL,
                     Literal(2)
                 ),
                 TokenType.PLUS,
                 Binary(
                     Binary(
                         Literal(3),
                         TokenType.MUL,
                         Binary(
                             Literal(8),
                             TokenType.POW,
                             Literal(2)
                         )
                     ),
                     TokenType.MUL,
                     Literal(2)
                 )
             ),
             TokenType.MINUS,
             Grouping(
                 Binary(
                     Binary(
                         Literal(10),
                         TokenType.DIV,
                         Literal(5)
                     ),
                     TokenType.MINUS,
                     Literal(2)
                 )
             )
         )
     interpreter = Interpreter()
     self.assertEqual(394, interpreter.interpret_expr(tree))