ソースを参照

More progress with Rust code generation

Joeri Exelmans 5 年 前
コミット
077bd51033
1 ファイル変更17 行追加2 行削除
  1. 17 2
      src/sccd/statechart/codegen/rust.py

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

@@ -3,20 +3,25 @@ from sccd.statechart.static.tree import *
 from sccd.util.visit_tree import *
 
 def ident(state: State) -> str:
-    # if state.opt.full_name == "/":
+    # if bestate.opt.full_name == "/":
         # return ""
     # else:
         return state.opt.full_name.replace('/', '_');
 
 def ident_type(state: State) -> str:
-    return "State" + ident(state)
+    if state.opt.full_name == "/":
+        return "Root" # no technical reason, just make this type 'pop out' a little
+    else:
+        return "State" + ident(state)
 
 def ident_field(state: State) -> str:
     return "s" + ident(state)
 
+
 def compile_to_rust(tree: StateTree):
     # 1. Write types
 
+    # 1.1 Write 'current state' types
     def write_state_type(state: State, children: str):
         def as_struct():
             print("struct %s {" % ident_type(state))
@@ -31,11 +36,13 @@ def compile_to_rust(tree: StateTree):
             print("}")
 
         if isinstance(state, ParallelState):
+            print("// And-state")
             as_struct()
         elif isinstance(state, HistoryState):
             print("Skipping HistoryState: ", state.opt.full_name)
         elif isinstance(state, State):
             if len(state.children) > 0:
+                print("// Or-state")
                 as_enum() # Or-state
             else:
                 # Basic state: write as empty struct
@@ -45,6 +52,7 @@ def compile_to_rust(tree: StateTree):
                 #
                 # An empty enum is also a valid type in Rust, but no instances
                 # of it can be created. Also called an "uninhabited type".
+                print("// Basic state")
                 as_struct()
 
             # The above if-else construction hints at the fact that we would have
@@ -57,6 +65,13 @@ def compile_to_rust(tree: StateTree):
         parent_first=[],
         child_first=[write_state_type])
 
+    # 1.2 Write statechart type
+    print("struct Statechart {")
+    print("  current_state: %s," % ident_type(tree.root))
+    print("  // TODO: history values")
+    print("  // TODO: timers")
+    print("}")
+
 
     # 2. Write "enter default state" functions