Browse Source

Progress with Rust

Joeri Exelmans 4 years ago
parent
commit
62f54c0081
2 changed files with 21 additions and 4 deletions
  1. 1 2
      src/sccd/statechart/cmd/gen_rust.py
  2. 20 2
      src/sccd/statechart/codegen/rust.py

+ 1 - 2
src/sccd/statechart/cmd/gen_rust.py

@@ -21,7 +21,6 @@ if __name__ == "__main__":
 
     assert isinstance(statechart, Statechart)
 
-    print("Loaded model.")
-    print()
+    sys.stderr.write("Loaded model.\n")
 
     compile_to_rust(statechart.tree)

+ 20 - 2
src/sccd/statechart/codegen/rust.py

@@ -24,12 +24,14 @@ def compile_to_rust(tree: StateTree):
     # 1.1 Write 'current state' types
     def write_state_type(state: State, children: str):
         def as_struct():
+            print("#[allow(non_camel_case_types)]")
             print("struct %s {" % ident_type(state))
             for child in children:
                 print("  %s: %s," % (ident_field(child), ident_type(child)))
             print("}")
 
         def as_enum():
+            print("#[allow(non_camel_case_types)]")
             print("enum %s {" % ident_type(state))
             for child in children:
                 print("  %s(%s)," % (ident_field(child), ident_type(child)))
@@ -62,19 +64,35 @@ def compile_to_rust(tree: StateTree):
         return state
 
     visit_tree(tree.root, lambda s: s.children,
-        parent_first=[],
         child_first=[write_state_type])
 
     # 1.2 Write statechart type
-    print("struct Statechart {")
+    print("pub struct Statechart {")
     print("  current_state: %s," % ident_type(tree.root))
     print("  // TODO: history values")
     print("  // TODO: timers")
     print("}")
+    print()
 
 
     # 2. Write "enter default state" functions
 
+    print("pub trait State {")
+    print("  fn enter_default();")
+    print("}")
+    print()
+
+    def write_enter_default(state: State, children: str):
+        print("impl State for %s {" % ident_type(state))
+        print("  fn enter_default() {")
+        if isinstance(state, ParallelState):
+            pass
+        print("  }")
+        print("}")
+        print()
+
+    visit_tree(tree.root, lambda s: s.children,
+        child_first=[write_enter_default])
 
     # 3. Write transition functions