示例#1
0
 def test_puts_function(self):
     self.assertEqual(
         self.parse("""
             int puts(const char * string) {
                 int i = 0;
                 while (string[i] != NULL) {
                     putc(string[i++]);
                 }
                 putc('\\n');
                 return i + 1;
             }
         """),
         Program([
             Function(
                 return_type=Type(base='int'),
                 name="puts",
                 params=[
                     VariableDeclaration(name="string",
                                         vtype=Type(
                                             base="pointer",
                                             const=True,
                                             reference=Type(base="char")))
                 ],
                 body=Block([
                     VariableDeclaration(name="i",
                                         vtype=Type(base="int"),
                                         value=Int32(value=0)),
                     For(condition=BinaryOperation(
                         operator="!=",
                         left=ArrayDereference(
                             array=Variable(name="string"),
                             index=Variable(name="i")),
                         right=Null()),
                         body=Block([
                             Call(name="putc",
                                  args=[
                                      ArrayDereference(
                                          array=Variable("string"),
                                          index=PostOperation(
                                              operator="++",
                                              variable=Variable(name="i")))
                                  ])
                         ])),
                     Call(name="putc", args=[Char('\n')]),
                     ReturnStatement(
                         value=BinaryOperation(operator="+",
                                               left=Variable(name="i"),
                                               right=Int32(value=1)))
                 ]))
         ]))
示例#2
0
 def test_basic_or(self):
     self.assertEqual(
         self.parse(self.function_wrap('1 || 0;')),
         self.function_wrap_node(
             BinaryOperation(operator="||",
                             left=Int32(value=1),
                             right=Int32(value=0))))
示例#3
0
 def test_basic_lte(self):
     self.assertEqual(
         self.parse(self.function_wrap('2 <= 3;')),
         self.function_wrap_node(
             BinaryOperation(operator="<=",
                             left=Int32(value=2),
                             right=Int32(value=3))))
示例#4
0
 def test_modulus(self):
     self.assertEqual(
         self.parse(self.function_wrap("1 % 2;")),
         self.function_wrap_node(
             BinaryOperation(operator="%",
                             left=Int32(value=1),
                             right=Int32(value=2))))
示例#5
0
 def test_division(self):
     self.assertEqual(
         self.parse(self.function_wrap("1 / 2;")),
         self.function_wrap_node(
             BinaryOperation(operator="/",
                             left=Int32(value=1),
                             right=Int32(value=2))))
示例#6
0
 def test_multiplication(self):
     self.assertEqual(
         self.parse(self.function_wrap("1 * 2;")),
         self.function_wrap_node(
             BinaryOperation(operator="*",
                             left=Int32(value=1),
                             right=Int32(value=2))))
示例#7
0
 def test_if_loop(self):
     self.assertEqual(
         self.parse(self.function_wrap("if ( i > 10 ) { i++; } ")),
         self.function_wrap_node(
             If(
                 condition=BinaryOperation(operator=">",
                                           left=Variable(name="i"),
                                           right=Int32(value=10)),
                 body=Block([
                     PostOperation(operator="++",
                                   variable=Variable(name="i"))
                 ]),
             )))
示例#8
0
 def test_for_loop(self):
     self.assertEqual(
         self.parse(
             self.function_wrap("for ( i = 0; i < 10; i++ ) { putc(i); }")),
         self.function_wrap_node(
             For(
                 initial=Assignment(left=Variable(name="i"),
                                    right=Int32(value=0)),
                 condition=BinaryOperation(operator="<",
                                           left=Variable(name="i"),
                                           right=Int32(value=10)),
                 increment=PostOperation(operator="++",
                                         variable=Variable(name="i")),
                 body=Block([Call(name="putc", args=[Variable(name="i")])]),
             )))
示例#9
0
 def test_braceless_while_loop(self):
     self.assertEqual(
         self.parse(
             self.function_wrap("""
             while ( i < 10 )
                 i++;
             """)),
         self.function_wrap_node(
             For(
                 condition=BinaryOperation(operator="<",
                                           left=Variable(name="i"),
                                           right=Int32(value=10)),
                 body=Block([
                     PostOperation(operator="++",
                                   variable=Variable(name="i"))
                 ]),
             )))
示例#10
0
 def test_while_loop(self):
     self.assertEqual(
         self.parse(
             self.function_wrap(
                 "while (string[i] != NULL) { putc(string[i++]); }")),
         self.function_wrap_node(
             For(condition=BinaryOperation(
                 operator="!=",
                 left=ArrayDereference(array=Variable(name="string"),
                                       index=Variable("i")),
                 right=Null()),
                 body=Block([
                     Call(name="putc",
                          args=[
                              ArrayDereference(
                                  array=Variable("string"),
                                  index=PostOperation(
                                      operator="++",
                                      variable=Variable(name="i")))
                          ])
                 ]))))
示例#11
0
 def binop(self, p):
     return BinaryOperation(operator=p[1].getstr(), left=p[0], right=p[2])