Esempio n. 1
0
 def test_02_basicexpr(self):
     """Test cnorm expression nodes"""
     e = nodes.Id('cool')
     self.assertEqual(str(e.to_c()), "cool", "Failed to convert to C")
     e = nodes.Literal('42')
     self.assertEqual(str(e.to_c()), "42", "Failed to convert to C")
     e = nodes.Binary(nodes.Raw('+'), [nodes.Id('a'), nodes.Literal('12')])
     p = e
     self.assertEqual(str(e.to_c()), "a + 12", "Failed to convert to C")
     e = nodes.Func(nodes.Id('f'), [nodes.Id('a'), nodes.Literal('12')])
     self.assertEqual(str(e.to_c()), "f(a, 12)", "Failed to convert to C")
     e = nodes.Ternary(
         '', [nodes.Id('a'),
              nodes.Literal('1'),
              nodes.Literal('2')])
     self.assertEqual(str(e.to_c()), "a ? 1 : 2", "Failed to convert to C")
     e = nodes.Unary(nodes.Raw('++'), [nodes.Id('a')])
     self.assertEqual(str(e.to_c()), "++a", "Failed to convert to C")
     e = nodes.Paren('', [p])
     self.assertEqual(str(e.to_c()), "(a + 12)", "Failed to convert to C")
     e = nodes.Post(nodes.Raw('++'), [nodes.Id('a')])
     self.assertEqual(str(e.to_c()), "a++", "Failed to convert to C")
     e = nodes.Array(nodes.Id('tab'), [p])
     self.assertEqual(str(e.to_c()), "tab[a + 12]",
                      "Failed to convert to C")
     e = nodes.Dot(nodes.Id('s'), [nodes.Id('a')])
     self.assertEqual(str(e.to_c()), "s.a", "Failed to convert to C")
     e = nodes.Arrow(nodes.Id('s'), [nodes.Id('a')])
     self.assertEqual(str(e.to_c()), "s->a", "Failed to convert to C")
Esempio n. 2
0
 def test_03_basicstmt(self):
     """Test cnorm statement nodes"""
     c = nodes.Binary(nodes.Raw('<'), [nodes.Id('a'), nodes.Literal('12')])
     thencond = nodes.ExprStmt(
         nodes.Binary(nodes.Raw('='),
                      [nodes.Id('b'), nodes.Literal('1')]))
     elsecond = nodes.ExprStmt(
         nodes.Binary(nodes.Raw('='),
                      [nodes.Id('c'), nodes.Literal('2')]))
     s = nodes.If(c, thencond, elsecond)
     self.assertEqual(
         str(s.to_c()),
         "if (a < 12)\n{tab}b = 1;\nelse\n{tab}c = 2;\n".format(tab=" " *
                                                                4),
         "Failed to convert to C")
     s = nodes.RootBlockStmt(
         [thencond,
          nodes.BlockStmt([thencond, elsecond]), elsecond])
     self.assertEqual(
         str(s.to_c()),
         "b = 1;\n{{\n{tab}b = 1;\n{tab}c = 2;\n}}\nc = 2;\n".format(
             tab=" " * 4), "Failed to convert to C")
     s = nodes.While(c, thencond)
     self.assertEqual(str(s.to_c()),
                      "while (a < 12)\n{tab}b = 1;\n".format(tab=" " * 4),
                      "Failed to convert to C")
     s = nodes.Do(c, thencond)
     self.assertEqual(
         str(s.to_c()),
         "do\n{tab}b = 1;\nwhile (a < 12);\n".format(tab=" " * 4),
         "Failed to convert to C")
     s = nodes.Return(c)
     self.assertEqual(str(s.to_c()), "return a < 12;\n",
                      "Failed to convert to C")
     s = nodes.Goto(c)
     self.assertEqual(str(s.to_c()), "goto a < 12;\n",
                      "Failed to convert to C")
     s = nodes.Case(c)
     self.assertEqual(str(s.to_c()), "case a < 12:\n",
                      "Failed to convert to C")
     s = nodes.Label("Cool")
     self.assertEqual(str(s.to_c()), "Cool:\n", "Failed to convert to C")
     s = nodes.Switch(c, thencond)
     self.assertEqual(str(s.to_c()),
                      "switch (a < 12)\n{tab}b = 1;\n".format(tab=" " * 4),
                      "Failed to convert to C")
     init = nodes.ExprStmt(
         nodes.Binary(nodes.Raw('='),
                      [nodes.Id('b'), nodes.Literal('0')]))
     cond = nodes.ExprStmt(c)
     inc = nodes.Binary(nodes.Raw('+='),
                        [nodes.Id('b'), nodes.Literal('1')])
     s = nodes.For(init, cond, inc, thencond)
     self.assertEqual(
         str(s.to_c()),
         "for (b = 0; a < 12; b += 1)\n{tab}b = 1;\n".format(tab=" " * 4),
         "Failed to convert to C")
Esempio n. 3
0
def new_range(self, ast, expr):
    begin = Node()
    begin.set(ast)
    ast.set(nodes.Range(nodes.Raw('...'), [begin, expr]))
    return True
Esempio n. 4
0
def new_raw(self, ast, data):
    ast.set(nodes.Raw(self.value(data)))
    return True
Esempio n. 5
0
def is_raw(self, op, ident):
    ident_value = self.value(ident)
    if ident_value in Idset and Idset[ident_value] == "unary":
        op.set(nodes.Raw(ident_value + " "))
        return True
    return False
Esempio n. 6
0
def new_builtoffset(self, ast, bof):
    ast.set(nodes.Raw("__builtin_offsetof(" + self.value(bof) + ")"))
    return True
Esempio n. 7
0
def new_sizeof(self, ast, i, n):
    thing = n
    if isinstance(thing, nodes.Decl):
        thing = n.ctype
    ast.set(nodes.Sizeof(nodes.Raw(self.value(i)), [thing]))
    return True
Esempio n. 8
0
def to_cast(self, ast, typename):
    expr = Node()
    expr.set(ast)
    ast.set(nodes.Cast(nodes.Raw('()'), [typename.ctype, expr]))
    return True
Esempio n. 9
0
def raw_decl(self, decl):
    decl.set(nodes.Raw(self.value(decl)))
    return True