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_INTEGER_LITERAL, 1))) ])) ])
def testParsingDecrements(self): self.assertEqual(ParseToAst('阿庆混腔势。'), [ Statement(STMT_DEC_BY, (IdentifierToken('阿庆'), IntegerLiteralExpr(1))) ]) self.assertEqual(ParseToAst('阿庆混三趟。'), [ Statement(STMT_DEC_BY, (IdentifierToken('阿庆'), IntegerLiteralExpr(3))) ])
def testParsingIncrements(self): self.assertEqual(ParseToAst('阿庆扎台型。'), [ Statement(STMT_INC_BY, (IdentifierToken('阿庆'), IntegerLiteralExpr(1))) ]) self.assertEqual(ParseToAst('阿庆扎两趟。'), [ Statement(STMT_INC_BY, (IdentifierToken('阿庆'), IntegerLiteralExpr(2))) ])
def testParseConditional(self): self.assertEqual( ParseStmtFromStr('轧苗头:阿庆比五老卵?要来赛就嘎讪胡:阿庆。')[0], Statement( STMT_CONDITIONAL, ( ComparisonExpr(VariableExpr('阿庆'), Keyword('老卵'), IntegerLiteralExpr(5)), # then-branch Statement(STMT_SAY, VariableExpr('阿庆')), # else-branch None)))
def testParsingComparison(self): self.assertEquals(ParseToAst('嘎讪胡:2比5老卵。'), [ Statement( STMT_SAY, ComparisonExpr(IntegerLiteralExpr(2), Keyword('老卵'), IntegerLiteralExpr(5))) ])
def testParsingFuncDefWithParam(self): self.assertEqual( ParseToAst('【阶乘】(那啥)哪能组:组好了。'), [ Statement( STMT_FUNC_DEF, ( IdentifierToken('阶乘'), [IdentifierToken('那啥')], # Formal parameters. [] # Function body. )) ])
def testParsingFuncCallWithParam(self): self.assertEqual( ParseToAst('白相【阶乘】(五)。'), [Statement(STMT_CALL, CallExpr('阶乘', [IntegerLiteralExpr(5)]))])
def testParsingLoop(self): self.assertEqual(ParseToAst('阿庆从1到9搞七捻三:搞好了。'), [ Statement(STMT_LOOP, (IdentifierToken('阿庆'), IntegerLiteralExpr(1), IntegerLiteralExpr(9), [])) ])