def test_cppgen_if(): exp = """ if(x==y){ x=1;\n }else if(x>y){ x=2;\n }else{ x=3;\n }""".strip() + "\n" n = If(cond=BinOp(x=Var(name="x"), op="==", y=Var(name="y")),\ body=[ExprStmt(child=Assign(target=Var(name="x"), value=Raw(code="1")))],\ elifs=[(BinOp(x=Var(name="x"), op=">", y=Var(name="y")), [ExprStmt(child=Assign(target=Var(name="x"), value=Raw(code="2")))])],\ el=ExprStmt(child=Assign(target=Var(name="x"), value=Raw(code="3")))) obs = CPPGEN.visit(n) assert_equal(exp, obs)
def test_cppgen_funcdef(): exp = """template<> void hello(int a, std::string b) { int x=5; int y=6; int z=x+y; } """ n = FuncDef(type=Raw(code="void"), name=Var(name="hello"), args=[ Decl(type=Type(cpp="int"), name=Var(name="a")), Decl(type=Type(cpp="std::string"), name=Var(name="b")) ], body=[ ExprStmt(child=DeclAssign(type=Type(cpp="int"), target=Var(name="x"), value=Raw(code="5"))), ExprStmt(child=DeclAssign(type=Type(cpp="int"), target=Var(name="y"), value=Raw(code="6"))), ExprStmt(child=DeclAssign( type=Type(cpp="int"), target=Var(name="z"), value=BinOp(x=Var(name="x"), op="+", y=Var(name="y")))) ], tspecial=True) obs = CPPGEN.visit(n) assert_equal(exp, obs)
def test_cppgen_for(): exp = """ for(int i=0;i<5;i++){ a++; b++; c[i]=a+b;\n }""".strip() + "\n" n = For(adecl=DeclAssign(type=Type(cpp="int"), target=Var(name="i"), value=Raw(code="0")),\ cond=BinOp(x=Var(name="i"), op="<", y=Raw(code="5")),\ incr=RightUnaryOp(name=Var(name="i"), op="++"),\ body=[ExprStmt(child=RightUnaryOp(name=Var(name="a"), op="++")), ExprStmt(child=RightUnaryOp(name=Var(name="b"), op="++")), ExprStmt(child=Assign(target=RightUnaryOp(name=Var(name="c"), op="[i]"), value=BinOp(x=Var(name="a"), op="+", y=Var(name="b"))))]) obs = CPPGEN.visit(n) assert_equal(exp, obs)
def test_cppgen_block(): exp = """int x=5; int y=6; int z=x+y;\n""" n = Block(nodes=[ ExprStmt(child=DeclAssign( type=Type(cpp="int"), target=Var(name="x"), value=Raw(code="5"))), Block(nodes=[ ExprStmt(child=DeclAssign(type=Type(cpp="int"), target=Var(name="y"), value=Raw(code="6"))), ExprStmt(child=DeclAssign( type=Type(cpp="int"), target=Var(name="z"), value=BinOp(x=Var(name="x"), op="+", y=Var(name="y")))) ]) ]) obs = CPPGEN.visit(n) assert_equal(exp, obs)
def test_cppgen_binop(): exp = "x+y" n = BinOp(x=Var(name="x"), op="+", y=Var(name="y")) obs = CPPGEN.visit(n) assert_equal(exp, obs)