Esempio n. 1
0
 def test_function_call_with_arguments(self):
     arguments = [
         code.SimpleVariable("a"),
         code.SimpleVariable("b"),
         code.BooleanLiteral(False)
     ]
     self.unit.select("test", "dec") \
       .append(code.FunctionCall("some_func", arguments))
     self.assertEqualToSource(self.unit, "some_func(a, b, FALSE);")
Esempio n. 2
0
    def test_repeat_until_loop(self):
        self.unit.select("test", "dec") \
          .append(code.RepeatUntil(code.BooleanLiteral(False)) \
            .contains(code.Print("endless"), code.Print("loop")))
        self.assertEqualToSource(
            self.unit, """
#include <stdio.h>
do {
printf("endless");
printf("loop");
} while(!(FALSE));""")
Esempio n. 3
0
    def test_while_do_loop(self):
        self.unit.select("test", "dec") \
          .append(code.WhileDo(code.BooleanLiteral(True)) \
            .contains(code.Print("endless"),
                      code.Print("loop")))
        self.assertEqualToSource(
            self.unit, """
#include <stdio.h>
while(TRUE) {
printf("endless");
printf("loop");
}""")
Esempio n. 4
0
    def create_main_module(self, model):
        """
    Creates the top-level main and includes modules.
    """
        module = self.unit.append(Module("includes").tag("includes"))
        # add basic set of includes that apply to all generations, without causing
        # compilation problems
        module.select("def").append(code.Import("<stdint.h>"))

        for mod in model.modules.values():
            if len(mod.constants.items()) > 0:
                module.select("def").append(code.Import("constants"))
                break

        module.select("def").append(
            code.Import("foo-lib/crypto")).tag("foo-lib-start")
        module.select("def").append(code.Import("foo-lib/time"))

        module.select("def").append(code.Import("../lib/network"))

        # MAIN module
        module = self.unit.append(Module("main"))
        module.select("def").append(code.Import("includes"))
        module.select("dec").append(code.Import("main")).tag("main_h")

        for domain_module_name, domain_module in model.modules.items():
            for domain_name, domain in domain_module.domains.items():
                name = domain_name + "-" + domain_module_name
                module.select("def").append(code.Import(name))

        # init
        init = code.Function("init").tag("init") \
                   .contains(code.Comment("add framework init here"))

        # app
        app = code.Function("application_step") \
                  .contains(code.Comment("add application specific code here")) \
                  .tag("step")

        # main
        main = code.Function("main",
                             code.NamedType("int")).tag("main_function")
        main.append(code.FunctionCall("init").stick_top())
        main.append(code.Return(code.IntegerLiteral(1))).stick_bottom()

        module.select("dec").append(
            code.Comment("""init and application_step
can be implemented using application specific needs."""), init, app,
            code.Comment("""starting point
please don't change anything beyond this point."""), main)

        # construct an event_loop builder and hook it into the main function
        event_loop = code.WhileDo(code.BooleanLiteral(True))
        main.append(event_loop).tag("event_loop") \
            .append(code.Comment("your application gets its share"),
                    code.FunctionCall("application_step"))

        # allow each domain generator to extend the main section
        for mod in model.modules.values():
            for domain_name, domain in mod.domains.items():
                self.generator_for_domain(domain_name).extend(module)
Esempio n. 5
0
 def after_visit_BooleanLiteralExp(self, literal):
     self.code.append(code.BooleanLiteral(literal.value))