Exemplo n.º 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)))
                 ]))
         ]))
Exemplo n.º 2
0
 def test_main_with_multiple_parameters(self):
     self.assertEqual(
         self.parse(
             "int main(int argc, char **argv, char **env) { return 0; }"),
         Program(units=[
             Function(return_type=Type(base='int'),
                      name="main",
                      params=[
                          VariableDeclaration(name="argc",
                                              vtype=Type(base="int")),
                          VariableDeclaration(
                              name="argv",
                              vtype=Type(base="pointer",
                                         reference=Type(base="pointer",
                                                        reference=Type(
                                                            base="char")))),
                          VariableDeclaration(
                              name="env",
                              vtype=Type(base="pointer",
                                         reference=Type(base="pointer",
                                                        reference=Type(
                                                            base="char")))),
                      ],
                      body=Block([ReturnStatement(value=Int32(value=0))]))
         ]))
Exemplo n.º 3
0
 def function_wrap_node(self, node):
     return Program([
         Function(return_type=Type(base="int"),
                  name="main",
                  params=[],
                  body=Block([node]))
     ])
Exemplo n.º 4
0
 def test_main_with_no_parameters(self):
     self.assertEqual(
         self.parse("int main(void) { return 0; }"),
         Program(units=[
             Function(return_type=Type(base='int'),
                      name="main",
                      params=[],
                      body=Block([ReturnStatement(value=Int32(value=0))]))
         ]))
Exemplo n.º 5
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"))
                 ]),
             )))
Exemplo n.º 6
0
 def test_auxiliary_function(self):
     self.assertEqual(
         self.parse("""
             int foo(void) {
                 return 12;
             }
             int main(void) {
                 return foo();
             }
         """),
         Program([
             Function(return_type=Type(base='int'),
                      name="foo",
                      params=[],
                      body=Block(
                          [ReturnStatement(value=Int32(value=12))], )),
             Function(return_type=Type(base='int'),
                      name="main",
                      params=[],
                      body=Block([
                          ReturnStatement(value=Call(name="foo",
                                                     args=[]), ),
                      ])),
         ]))
Exemplo n.º 7
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")])]),
             )))
Exemplo n.º 8
0
 def test_function_arguments(self):
     self.assertEqual(
         self.parse("int puts(const char* string) { return 0; }"),
         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([ReturnStatement(value=Int32(value=0))]))
         ]))
Exemplo n.º 9
0
 def test_main_function(self):
     self.assertEqual(
         self.parse("""
             int main(void) {
                 return puts("Hello, world!");
             }
         """),
         Program([
             Function(return_type=Type(base='int'),
                      name="main",
                      params=[],
                      body=Block([
                          ReturnStatement(value=Call(
                              name="puts", args=[String("Hello, world!")]))
                      ]))
         ]))
Exemplo n.º 10
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"))
                 ]),
             )))
Exemplo n.º 11
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")))
                          ])
                 ]))))
Exemplo n.º 12
0
 def for_loop_single_line(self, p):
     return For(initial=p[2],
                condition=p[4],
                increment=p[6],
                body=Block(statements=[p[8]]))
Exemplo n.º 13
0
 def while_loop_single_line(self, p):
     return For(condition=p[2], body=Block(statements=[p[4]]))
Exemplo n.º 14
0
 def block_statement_list(self, p):
     return Block(statements=p[1].get_items())