Пример #1
0
 def testParseComparisonExpr(self):
     self.assertEqual(
         ParseExprFromStr('5比6大')[0],
         ComparisonExpr(LiteralExpr(Token(TK_INTEGER_LITERAL, 5)),
                        Keyword('大'),
                        LiteralExpr(Token(TK_INTEGER_LITERAL, 6))))
     self.assertEqual(
         ParseExprFromStr('老王加5比6小')[0],
         ComparisonExpr(
             ArithmeticExpr(VariableExpr(Token(TK_IDENTIFIER, '老王')),
                            Keyword('加'),
                            LiteralExpr(Token(TK_INTEGER_LITERAL, 5))),
             Keyword('小'), LiteralExpr(Token(TK_INTEGER_LITERAL, 6))))
     self.assertEqual(
         ParseExprFromStr('老王跟老刘一样一样的')[0],
         ComparisonExpr(VariableExpr(Token(TK_IDENTIFIER, '老王')),
                        Keyword('一样一样的'),
                        VariableExpr(Token(TK_IDENTIFIER, '老刘'))))
     self.assertEqual(
         ParseExprFromStr('老王加5跟6不是一样一样的')[0],
         ComparisonExpr(
             ArithmeticExpr(VariableExpr(Token(TK_IDENTIFIER, '老王')),
                            Keyword('加'),
                            LiteralExpr(Token(TK_INTEGER_LITERAL, 5))),
             Keyword('不是一样一样的'), LiteralExpr(Token(TK_INTEGER_LITERAL, 6))))
Пример #2
0
 def testParseComparisonExpr(self):
   self.assertEqual(ParseExprFromStr('5比6大')[0],
                    ComparisonExpr(
                        IntegerLiteralExpr(5),
                        Keyword('大'),
                        IntegerLiteralExpr(6)
                    ))
   self.assertEqual(ParseExprFromStr('老王加5比6小')[0],
                    ComparisonExpr(
                        ArithmeticExpr(
                            VariableExpr('老王'),
                            Keyword('加'),
                            IntegerLiteralExpr(5)),
                        Keyword('小'),
                        IntegerLiteralExpr(6)
                    ))
   self.assertEqual(ParseExprFromStr('老王跟老刘一样一样的')[0],
                    ComparisonExpr(
                        VariableExpr('老王'),
                        Keyword('一样一样的'),
                        VariableExpr('老刘')
                    ))
   self.assertEqual(ParseExprFromStr('老王加5跟6不是一样一样的')[0],
                    ComparisonExpr(
                        ArithmeticExpr(
                            VariableExpr('老王'),
                            Keyword('加'),
                            IntegerLiteralExpr(5)),
                        Keyword('不是一样一样的'),
                        IntegerLiteralExpr(6)
                    ))
Пример #3
0
 def testParseArithmeticExpr(self):
   self.assertEqual(ParseExprFromStr('5加六')[0],
                    ArithmeticExpr(
                        IntegerLiteralExpr(5),
                        Keyword('加'),
                        IntegerLiteralExpr(6)
                    ))
   self.assertEqual(ParseExprFromStr('5加六乘3')[0],
                    ArithmeticExpr(
                        IntegerLiteralExpr(5),
                        Keyword('加'),
                        ArithmeticExpr(
                            IntegerLiteralExpr(6),
                            Keyword('乘'),
                            IntegerLiteralExpr(3))))
   self.assertEqual(ParseExprFromStr('5减六减老王')[0],
                    ArithmeticExpr(
                        ArithmeticExpr(
                            IntegerLiteralExpr(5),
                            Keyword('减'),
                            IntegerLiteralExpr(6)
                        ),
                        Keyword('减'),
                        VariableExpr('老王'))
                    )
Пример #4
0
 def testParseArithmeticExpr(self):
   self.assertEqual(ParseExprFromStr(u'5加六')[0],
                    ArithmeticExpr(
                        LiteralExpr(Token(TK_INTEGER_LITERAL, 5)),
                        Keyword(u'加'),
                        LiteralExpr(Token(TK_INTEGER_LITERAL, 6))
                    ))
   self.assertEqual(ParseExprFromStr(u'5加六乘3')[0],
                    ArithmeticExpr(
                        LiteralExpr(Token(TK_INTEGER_LITERAL, 5)),
                        Keyword(u'加'),
                        ArithmeticExpr(
                            LiteralExpr(Token(TK_INTEGER_LITERAL, 6)),
                            Keyword(u'乘'),
                            LiteralExpr(Token(TK_INTEGER_LITERAL, 3)))))
   self.assertEqual(ParseExprFromStr(u'5减六减老王')[0],
                    ArithmeticExpr(
                        ArithmeticExpr(
                            LiteralExpr(Token(TK_INTEGER_LITERAL, 5)),
                            Keyword(u'减'),
                            LiteralExpr(Token(TK_INTEGER_LITERAL, 6))
                        ),
                        Keyword(u'减'),
                        VariableExpr(Token(TK_IDENTIFIER, u'老王')))
                    )
Пример #5
0
 def testParseTermExpr(self):
     self.assertEqual(
         ParseExprFromStr('老王乘五')[0],
         ArithmeticExpr(VariableExpr('老王'), Keyword('乘'),
                        IntegerLiteralExpr(5)))
     self.assertEqual(
         ParseExprFromStr('五除以老王')[0],
         ArithmeticExpr(IntegerLiteralExpr(5), Keyword('除以'),
                        VariableExpr('老王')))
     self.assertEqual(
         ParseExprFromStr('五除以老王乘老刘')[0],
         ArithmeticExpr(
             ArithmeticExpr(IntegerLiteralExpr(5), Keyword('除以'),
                            VariableExpr('老王')), Keyword('乘'),
             VariableExpr('老刘')))
Пример #6
0
 def testParseTermExpr(self):
     self.assertEqual(
         ParseExprFromStr('老王乘五')[0],
         ArithmeticExpr(VariableExpr(Token(TK_IDENTIFIER, '老王')),
                        Keyword('乘'),
                        LiteralExpr(Token(TK_INTEGER_LITERAL, 5))))
     self.assertEqual(
         ParseExprFromStr('五除以老王')[0],
         ArithmeticExpr(LiteralExpr(Token(TK_INTEGER_LITERAL, 5)),
                        Keyword('除以'),
                        VariableExpr(Token(TK_IDENTIFIER, '老王'))))
     self.assertEqual(
         ParseExprFromStr('五除以老王乘老刘')[0],
         ArithmeticExpr(
             ArithmeticExpr(LiteralExpr(Token(TK_INTEGER_LITERAL, 5)),
                            Keyword('除以'),
                            VariableExpr(Token(TK_IDENTIFIER, '老王'))),
             Keyword('乘'), VariableExpr(Token(TK_IDENTIFIER, '老刘'))))
Пример #7
0
 def testParseConcatExpr(self):
     self.assertEqual(
         ParseExprFromStr('老王加油、2、“哈”')[0],
         ConcatExpr([
             ArithmeticExpr(VariableExpr('老王'), Keyword('加'),
                            VariableExpr('油')),
             IntegerLiteralExpr(2),
             StringLiteralExpr('哈')
         ]))
Пример #8
0
 def testParseConcatExpr(self):
     self.assertEqual(
         ParseExprFromStr('老王加油、2、“哈”')[0],
         ConcatExpr([
             ArithmeticExpr(VariableExpr(Token(TK_IDENTIFIER, '老王')),
                            Keyword('加'),
                            VariableExpr(Token(TK_IDENTIFIER, '油'))),
             LiteralExpr(Token(TK_INTEGER_LITERAL, 2)),
             LiteralExpr(Token(TK_STRING_LITERAL, '哈'))
         ]))
Пример #9
0
 def testParsingLoop(self):
   self.assertEqual(
       ParseToAst('老王从1到9磨叽:磨叽完了。'),
       [Statement(
           STMT_LOOP,
           (VariableExpr('老王'),
            NumberLiteralExpr(1),
            NumberLiteralExpr(9),
            NumberLiteralExpr(1),
            []))])
   self.assertEqual(
       ParseToAst('老王从二到十一步七蹿磨叽:磨叽完了。'),
       [Statement(
           STMT_LOOP,
           (VariableExpr('老王'),
            NumberLiteralExpr(2),
            NumberLiteralExpr(10),
            NumberLiteralExpr(7),
            []))])
   self.assertEqual(
       ParseToAst('老王从二到十一步七减一蹿磨叽:磨叽完了。'),
       [Statement(
           STMT_LOOP,
           (VariableExpr('老王'),
            NumberLiteralExpr(2),
            NumberLiteralExpr(10),
            ArithmeticExpr(
               NumberLiteralExpr(7),
               Keyword('减'),
               NumberLiteralExpr(1)),
            []))])
   self.assertEqual(
       ParseToAst('老王从一而终磨叽:磨叽完了。'),
       [Statement(
           STMT_INFINITE_LOOP,
           (VariableExpr('老王'),
            []))])
   self.assertEqual(
       ParseToAst('老张在苹果总部磨叽:磨叽完了。'),
       [Statement(
           STMT_INFINITE_LOOP,
           (VariableExpr('老张'),
            []))])