-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen_c.py
More file actions
23 lines (22 loc) · 1.15 KB
/
Copy pathcodegen_c.py
File metadata and controls
23 lines (22 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import os
def create_c_files(systems, output_folder):
os.makedirs(output_folder, exist_ok=True)
for obj in systems:
name = obj['name']
c_file_path = os.path.join(output_folder, f"{name.lower()}.c")
with open(c_file_path, 'w') as c_file:
c_file.write("#include <string.h>\n\n")
c_file.write(f"typedef enum {{\n")
for state in obj["states"]:
c_file.write(f" {state},\n")
c_file.write(f"}} {obj}_State;\n\n")
c_file.write(f"{obj}_State current_state_{obj} = {obj['states'][0]};\n\n")
c_file.write(f"void handle_event_{obj}(const char* event) {{\n")
c_file.write(f" switch (current_state_{obj}) {{\n")
for state in obj["states"]:
c_file.write(f" case {state}:\n")
for src, event_name, dest in obj["transitions"]:
if src == state:
c_file.write(f" if (strcmp(event, \"{event_name}\") == 0) current_state_{obj} = {dest};\n")
c_file.write(" break;\n")
c_file.write(" }\n}\n")