def testParsingLoop(self): self.assertEqual(ParseToAst('老王从1到9磨叽:磨叽完了。'), [ Statement(STMT_LOOP, (Token(TK_IDENTIFIER, '老王'), LiteralExpr(Token(TK_INTEGER_LITERAL, 1)), LiteralExpr(Token(TK_INTEGER_LITERAL, 9)), [])) ])
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))))
def testParseInteger(self): self.assertEqual( ParseExprFromStr('5')[0], LiteralExpr(Token(TK_INTEGER_LITERAL, 5))) self.assertEqual( ParseExprFromStr('九')[0], LiteralExpr(Token(TK_INTEGER_LITERAL, 9)))
def testParsingFuncCallWithParam(self): self.assertEqual(ParseToAst('整【阶乘】(五)。'), [ Statement( STMT_CALL, CallExpr(Token(TK_IDENTIFIER, '阶乘'), [LiteralExpr(Token(TK_INTEGER_LITERAL, 5))])) ])
def testParseConcatExpr(self): self.assertEqual( ParseExprFromStr('老王、2')[0], ConcatExpr([ VariableExpr(Token(TK_IDENTIFIER, '老王')), LiteralExpr(Token(TK_INTEGER_LITERAL, 2)) ]))
def testParsingFuncDef(self): self.assertEqual( ParseToAst('写九九表咋整:整完了。'), [ Statement( STMT_FUNC_DEF, ( Token(TK_IDENTIFIER, '写九九表'), [], # Formal parameters. [] # Function body. )) ]) self.assertEqual( ParseToAst('写九九表咋整:唠唠:1。整完了。'), [ Statement( STMT_FUNC_DEF, ( Token(TK_IDENTIFIER, '写九九表'), [], # Formal parameters. # Function body. [ Statement( STMT_SAY, LiteralExpr(Token(TK_INTEGER_LITERAL, 1))) ])) ])
def testParsingComparison(self): self.assertEquals(ParseToAst('唠唠:2比5大。'), [ Statement( STMT_SAY, ComparisonExpr(LiteralExpr(Token(TK_INTEGER_LITERAL, 2)), Keyword('大'), LiteralExpr(Token(TK_INTEGER_LITERAL, 5)))) ])
def testParsingFuncDefWithParam(self): self.assertEqual( ParseToAst(u'【阶乘】(几)咋整:整完了。'), [Statement(STMT_FUNC_DEF, (Token(TK_IDENTIFIER, u'阶乘'), [Token(TK_IDENTIFIER, u'几')], # Formal parameters. [] # Function body. ))])
def testParsingDecrements(self): self.assertEqual(ParseToAst(u'老王退退。'), [ Statement(STMT_DEC_BY, (Token(TK_IDENTIFIER, u'老王'), LiteralExpr(Token(TK_INTEGER_LITERAL, 1)))) ]) self.assertEqual(ParseToAst(u'老王退三步。'), [ Statement(STMT_DEC_BY, (Token(TK_IDENTIFIER, u'老王'), LiteralExpr(Token(TK_INTEGER_LITERAL, 3)))) ])
def testParsingIncrements(self): self.assertEqual(ParseToAst(u'老王走走。'), [ Statement(STMT_INC_BY, (Token(TK_IDENTIFIER, u'老王'), LiteralExpr(Token(TK_INTEGER_LITERAL, 1)))) ]) self.assertEqual(ParseToAst(u'老王走两步。'), [ Statement(STMT_INC_BY, (Token(TK_IDENTIFIER, u'老王'), LiteralExpr(Token(TK_INTEGER_LITERAL, 2)))) ])
def testParseParens(self): # Wide parens. self.assertEqual( ParseExprFromStr('(老王)')[0], ParenExpr(VariableExpr(Token(TK_IDENTIFIER, '老王')))) # Narrow parens. self.assertEqual( ParseExprFromStr('(老王)')[0], ParenExpr(VariableExpr(Token(TK_IDENTIFIER, '老王'))))
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, '哈')) ]))
def testTokenizingDecrements(self): self.assertEqual(list(Tokenize('老王退退')), [ Token(TK_IDENTIFIER, '老王'), Keyword('退退'), ]) self.assertEqual(list(Tokenize('老王退三步')), [ Token(TK_IDENTIFIER, '老王'), Keyword('退'), Token(TK_INTEGER_LITERAL, 3), Keyword('步'), ])
def testTokenizingIncrements(self): self.assertEqual(list(Tokenize('老王走走')), [ Token(TK_IDENTIFIER, '老王'), Keyword('走走'), ]) self.assertEqual(list(Tokenize('老王走两步')), [ Token(TK_IDENTIFIER, '老王'), Keyword('走'), Token(TK_INTEGER_LITERAL, 2), Keyword('步'), ])
def testTokenizeLoop(self): self.assertEqual(list(Tokenize('老王从1到9磨叽:磨叽完了。')), [ Token(TK_IDENTIFIER, '老王'), Keyword('从'), Token(TK_INTEGER_LITERAL, 1), Keyword('到'), Token(TK_INTEGER_LITERAL, 9), Keyword('磨叽:'), Keyword('磨叽完了'), Keyword('。'), ])
def testTokenizeLoop(self): self.assertEqual(Tokenize('老王从1到9磨叽:磨叽完了。'), [ IdentifierToken('老王'), Keyword('从'), Token(TK_NUMBER_LITERAL, 1, None), Keyword('到'), Token(TK_NUMBER_LITERAL, 9, None), Keyword('磨叽:'), Keyword('磨叽完了'), Keyword('。'), ])
def testTokenizeArithmetic(self): self.assertEqual(Tokenize('250加13减二乘五除以九'), [ Token(TK_NUMBER_LITERAL, 250, None), Keyword('加'), Token(TK_NUMBER_LITERAL, 13, None), Keyword('减'), Token(TK_NUMBER_LITERAL, 2, None), Keyword('乘'), Token(TK_NUMBER_LITERAL, 5, None), Keyword('除以'), Token(TK_NUMBER_LITERAL, 9, None), ])
def testTokenizeArithmetic(self): self.assertEqual(list(Tokenize('250加13减二乘五除以九')), [ Token(TK_INTEGER_LITERAL, 250), Keyword('加'), Token(TK_INTEGER_LITERAL, 13), Keyword('减'), Token(TK_INTEGER_LITERAL, 2), Keyword('乘'), Token(TK_INTEGER_LITERAL, 5), Keyword('除以'), Token(TK_INTEGER_LITERAL, 9), ])
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'老王'))) )
def testTokenize(self): self.assertEqual( Tokenize('# 123456\n老张'), [IdentifierToken('老张')]) self.assertEqual( Tokenize('老张'), [IdentifierToken('老张')]) self.assertEqual( TryParseNumber('老张'), (None, '老张')) self.assertEqual( list(TokenizeStrContainingNoKeyword('老张', None)), [IdentifierToken('老张')]) self.assertEqual( Tokenize('老张是活雷锋'), [IdentifierToken('老张'), Keyword('是活雷锋')]) self.assertEqual( Tokenize('老张是 活雷\n锋 。 '), [IdentifierToken('老张'), Keyword('是活雷锋'), Keyword('。'), ]) self.assertEqual( Tokenize('老张是活雷锋。\n老王是活雷锋。\n'), [IdentifierToken('老张'), Keyword('是活雷锋'), Keyword('。'), IdentifierToken('老王'), Keyword('是活雷锋'), Keyword('。'), ]) self.assertEqual( Tokenize('老张装250。\n老王装老张。\n'), [IdentifierToken('老张'), Keyword('装'), Token(TK_NUMBER_LITERAL, 250, None), Keyword('。'), IdentifierToken('老王'), Keyword('装'), IdentifierToken('老张'), Keyword('。')]) self.assertEqual( Tokenize('唠唠:“你好”。'), [Keyword('唠唠'), Keyword(':'), Keyword('“'), Token(TK_STRING_LITERAL, '你好', None), Keyword('”'), Keyword('。')])
def testParseConditional(self): self.assertEqual( ParseStmtFromStr('寻思:老王比五大?要行咧就唠唠:老王。')[0], Statement( STMT_CONDITIONAL, ( ComparisonExpr(VariableExpr(Token(TK_IDENTIFIER, '老王')), Keyword('大'), LiteralExpr(Token(TK_INTEGER_LITERAL, 5))), # then-branch Statement(STMT_SAY, VariableExpr(Token( TK_IDENTIFIER, '老王'))), # else-branch None)))
def testParsingFuncDef(self): self.assertEqual( ParseToAst('写九九表咋整:整完了。'), [ Statement( STMT_FUNC_DEF, ( IdentifierToken('写九九表'), [], # Formal parameters. [] # Function body. )) ]) self.assertEqual( ParseToAst('写九九表咋整:唠唠:1。整完了。'), [ Statement( STMT_FUNC_DEF, ( IdentifierToken('写九九表'), [], # Formal parameters. # Function body. [ Statement( STMT_SAY, LiteralExpr(Token(TK_NUMBER_LITERAL, 1, None))) ])) ])
def testTokenizingFuncDef(self): self.assertEqual(list(Tokenize('写九九表咋整:整完了。')), [ Token(TK_IDENTIFIER, '写九九表'), Keyword('咋整:'), Keyword('整完了'), Keyword('。'), ])
def testTokenizeCompound(self): self.assertEqual(list(Tokenize('开整:\n 唠唠:老王。\n整完了。')), [ Keyword('开整:'), Keyword('唠唠'), Keyword(':'), Token(TK_IDENTIFIER, '老王'), Keyword('。'), Keyword('整完了'), Keyword('。'), ])
def testTokenizingIncrements(self): self.assertEqual(Tokenize('老王走走'), [ IdentifierToken('老王'), Keyword('走走'), ]) self.assertEqual(Tokenize('老王走两步'), [ IdentifierToken('老王'), Keyword('走'), Token(TK_NUMBER_LITERAL, 2, None), Keyword('步'), ])
def testTokenizingDecrements(self): self.assertEqual(Tokenize('老王稍稍'), [ IdentifierToken('老王'), Keyword('稍稍'), ]) self.assertEqual(Tokenize('老王稍三步'), [ IdentifierToken('老王'), Keyword('稍'), Token(TK_NUMBER_LITERAL, 3, None), Keyword('步'), ])
def testTokenizingDecrements(self): self.assertEqual(list(Tokenize('老王稍稍')), [ IdentifierToken('老王'), Keyword('稍稍'), ]) self.assertEqual(list(Tokenize('老王稍三步')), [ IdentifierToken('老王'), Keyword('稍'), Token(TK_INTEGER_LITERAL, 3), Keyword('步'), ])
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, '老刘'))))
def testParseCallExpr(self): self.assertEqual( ParseExprFromStr('整老王')[0], CallExpr(Token(TK_IDENTIFIER, '老王'), [])) self.assertEqual( ParseExprFromStr('整老王(5)')[0], CallExpr(Token(TK_IDENTIFIER, '老王'), [LiteralExpr(Token(TK_INTEGER_LITERAL, 5))])) self.assertEqual( ParseExprFromStr('整老王(6)')[0], CallExpr(Token(TK_IDENTIFIER, '老王'), [LiteralExpr(Token(TK_INTEGER_LITERAL, 6))])) self.assertEqual( ParseExprFromStr('整老王(老刘,6)')[0], CallExpr(Token(TK_IDENTIFIER, '老王'), [ VariableExpr(Token(TK_IDENTIFIER, '老刘')), LiteralExpr(Token(TK_INTEGER_LITERAL, 6)) ])) self.assertEqual( ParseExprFromStr('整老王(“你”,老刘,6)')[0], CallExpr(Token(TK_IDENTIFIER, '老王'), [ LiteralExpr(Token(TK_STRING_LITERAL, '你')), VariableExpr(Token(TK_IDENTIFIER, '老刘')), LiteralExpr(Token(TK_INTEGER_LITERAL, 6)) ])) self.assertEqual( ParseExprFromStr('整老王(“你”,老刘,6)')[0], CallExpr(Token(TK_IDENTIFIER, '老王'), [ LiteralExpr(Token(TK_STRING_LITERAL, '你')), VariableExpr(Token(TK_IDENTIFIER, '老刘')), LiteralExpr(Token(TK_INTEGER_LITERAL, 6)) ]))
def testParseIdentifier(self): self.assertEqual( ParseExprFromStr('老王')[0], VariableExpr(Token(TK_IDENTIFIER, '老王')))