diff --git a/XFTGenerator.py b/XFTGenerator.py index cf1090e..11f9e43 100644 --- a/XFTGenerator.py +++ b/XFTGenerator.py @@ -191,6 +191,55 @@ def main(): variables["EVENTS_EMITS_DEC"] = "\n\n".join(events_emits_dec) + ############# + # Variables # + ############# + + vars_init = [] + vars_struct = [] + vars_setters_def = [] + vars_setters_dec = [] + + setter_def = "" + setter_def += "void {filename}_set{Name}({filename}* me, {type} v) {\n" + setter_def += " me->{name} = v;\n" + setter_def += "}" + setter_def = setter_def.replace("{filename}", variables["filename"]) + + setter_dec = "void {filename}_set{Name}({filename}* me, {type} v);" + setter_dec = setter_dec.replace("{filename}", variables["filename"]) + + print("Variables:") + print("(leave name empty to end)") + while True: + name = input("Name: ").strip() + if not name: break + + type_ = input("Type: ").strip() + default = input("Default value: ").strip() + print() + + vars_struct.append(f"{type_} {name};") + + if default: + vars_init.append(f"me->{name} = {default};") + + vars_setters_def.append( + setter_def.replace("{type}", type_) + .replace("{name}", name) + .replace("{Name}", ucfirst(name)) + ) + + vars_setters_dec.append( + setter_dec.replace("{type}", type_) + .replace("{Name}", ucfirst(name)) + ) + + variables["VARS_INIT"] = "\n".join(vars_init) + variables["VARS_STRUCT"] = "\n".join(vars_struct) + variables["VARS_SETTERS_DEF"] = "\n\n".join(vars_setters_def) + variables["VARS_SETTERS_DEC"] = "\n\n".join(vars_setters_dec) + ################## # Fill templates # ################## diff --git a/templates/file.c b/templates/file.c index abe1b07..558bd4e 100644 --- a/templates/file.c +++ b/templates/file.c @@ -9,6 +9,7 @@ void ${filename}_init(${filename}* me){ me->state = ST${fn}_INIT; + ${VARS_INIT} } void ${filename}_startBehaviour(${filename}* me){ @@ -52,4 +53,10 @@ bool ${filename}_processEvent(Event* ev) { * EMITTERS * ************/ -${EVENTS_EMITS_DEF} \ No newline at end of file +${EVENTS_EMITS_DEF} + +/*********** + * SETTERS * + ***********/ + +${VARS_SETTERS_DEF} \ No newline at end of file diff --git a/templates/file.h b/templates/file.h index 2d6326a..a908169 100644 --- a/templates/file.h +++ b/templates/file.h @@ -19,6 +19,7 @@ typedef enum { typedef struct { ${filename}_STATES state; + ${VARS_STRUCT} } ${filename}; /** @@ -46,4 +47,10 @@ bool ${filename}_processEvent(Event* ev); ${EVENTS_EMITS_DEC} +/*********** + * SETTERS * + ***********/ + +${VARS_SETTERS_DEC} + #endif