def setup(self, unit): super(Demo, self).setup(unit) code.Import("../lib/application").insert_after(unit.find("main_h")) unit.find("init").append(code.Import("../lib/init.c")).stick_top() unit.find("step").append(code.Import("../lib/application_step.c")) unit.find("event_loop").append( code.FunctionCall("report_metrics")).stick_bottom()
def setup(self, unit): unit.select("includes", "def").append(code.Import("moose/avr")) unit.select("includes", "def").append(code.Import("moose/bool")) unit.select("includes", "def").append(code.Import("moose/serial")) unit.select("includes", "def").append(code.Import("moose/clock")) unit.select("includes", "def").append(code.Import("moose/xbee")) unit.find("event_loop").append(code.FunctionCall("xbee_receive"))
def construct(self, code_module, module): """ Constructs a code_module from a module. """ self.add_import_nodes(code_module) code_module.select("dec").append(code.Import(code_module.data)) code_module.select("def").append(code.Import("includes")) # add extensions to node_t definition node_type = self.generator.unit.find("node_type_def") node_type.append(code.Comment("extended properties for " + module.name)) # add initializations to init_node function init = self.generator.unit.find("init_node") for ext in module.domains["nodes"].extensions: for prop in ext.extension.properties: if isinstance(prop.type, model.ManyType) and \ isinstance(prop.type.subtype, model.TupleType): code.Import("tuples").insert_before(node_type) node_type.append( code.Property(prop.name, self._translate(prop.type))) init.append( code.Assign(code.ObjectProperty("node", prop.name), self.translator.translate(prop.value))) # TODO: this should go in the emission of a functioncal :-/ # and lookup the function in the externals (no time now) if isinstance(prop.value, model.FunctionCallExp) and \ prop.value.name == "now": self.generator.unit.select("node_t", "def").append( code.Import("foo-lib/time")) # create all functions for function in module.functions: code_module.select("dec").append(self._translate(function))
def prepare(self): """ Prepares the definition of the node type. """ # this is a generator for the Nodes domain, let's get it ;-) self.domain = SemanticNodes() # we need a translator self.translator = Translator() node_type = code.StructuredType("nodes").tag("node_type_def") node_type.append(code.Comment("domain properties"), code.Property("id", code.ByteType()), code.Property("address", code.LongType())) # the node type module = self.generator.unit.append(structure.Module("node_t")) module.select("def").append(code.Import("moose/bool")) module.select("def").append(code.Comment("THE node type"), node_type).tag("node_t-start") # provide init_node to initialise module-specific properties module.select("dec").append( code.Import("node_t"), code.Function("init_node", params=[ code.Parameter("node", type=code.ObjectType("node")) ]).tag("init_node") # filled when creating node_t ) # TODO: remove this redundant header file module = self.generator.unit.append(structure.Module("nodes")) module.select("def").append(code.Import("includes")) # add more imports to includes anchor = self.generator.unit.select("includes").select("def").find( "foo-lib-start") code.Import("nodes").insert_before(anchor).tag("requires-tuples") code.Import("node_t").insert_before(anchor) code.Import("foo-lib/nodes").insert_before(anchor) code.Import("foo-lib/payload").insert_before(anchor) # initialiase nodes and add handling of receive packets self.generator.unit.find("init").append( code.FunctionCall("nodes_init"), code.FunctionCall("mesh_on_receive", [code.SimpleVariable("payload_parser_parse")]))
def add_import_nodes(self, module): """ Make sure that nodes functionality is imported (once) """ if not module.find("import_nodes") is None: return module.select("def").append(code.Import("nodes")).tag("import_nodes")
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)