Prechádzať zdrojové kódy

First steps in Rust code generation

Joeri Exelmans 4 rokov pred
rodič
commit
a3132e542f

+ 1 - 1
notes.txt

@@ -17,7 +17,7 @@ Long-term vision:
         - statechart editing plugin
 
   - code generation
-    - generating portable C code may be the most flexible option:
+    - generating portable C code (or: Rust) may be the most flexible option:
       - compile to WebAssembly
       - call from Python, Java, ...
 

+ 27 - 0
src/sccd/statechart/cmd/gen_rust.py

@@ -0,0 +1,27 @@
+import argparse
+import sys
+import termcolor
+from sccd.statechart.parser.xml import *
+
+from sccd.statechart.codegen.rust import compile_to_rust
+
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser(
+        description="Generate Rust code.")
+    parser.add_argument('path', metavar='PATH', type=str, help="Model to check.")
+    args = parser.parse_args()
+
+    src = args.path
+
+    path = os.path.dirname(src)
+    rules = [("statechart", statechart_parser_rules(Globals(), path, load_external=True))]
+
+    statechart = parse_f(src, rules)
+
+    assert isinstance(statechart, Statechart)
+
+    print("Loaded model.")
+    print()
+
+    compile_to_rust(statechart.tree)

+ 64 - 0
src/sccd/statechart/codegen/code_generation.txt

@@ -0,0 +1,64 @@
+
+===============
+Code generation
+===============
+
+
+Roadmap
+=======
+
+  Milestone 1: Initial compilation to Rust
+  
+    - entering and exiting states correctly
+    - no history
+    - no action language
+      - guards evals and action stmts are just logged to console
+      - guards always true, actions no effect
+
+    - incoming event triggers the right transition
+    
+    - no event parameters
+
+    - main operation:
+       statechart_process(:StatechartState, :Event) -> StatechartState
+          old state -> new state
+          
+    - fixed semantics (YAKINDU-like)
+
+    - goal: some subset of SCCD tests passes
+
+
+Implementation
+==============
+
+Data types
+----------
+
+- state machine's state
+  - [M1] current state(s)
+      nested structure of
+        - tagged unions (OR-states)
+        - structs (AND-states)
+  - history values
+  - timer IDs
+
+Operations
+----------
+
+  - hierarchical operations
+    - [M1] entering a state's default state
+    - entering a state's (deep/shallow) history
+    
+  - "flat" operations
+    - [M1] firing a transition
+      - [M1] exit states
+          + save history values
+          + canceling timers
+          + exit actions
+      - transition's actions
+      - [M1] enter states
+          + enter actions
+          + reading history values
+          + starting timers
+
+  - [M1] select and execute transition given an event

+ 41 - 0
src/sccd/statechart/codegen/rust.py

@@ -0,0 +1,41 @@
+from typing import *
+from sccd.statechart.static.tree import *
+from sccd.util.visit_tree import *
+
+def ident(state: State) -> str:
+    return state.opt.full_name.replace('/', '_');
+
+def compile_to_rust(tree: StateTree):
+    # 1. Write types
+
+    def write_state_type(state: State, children: str):
+        if isinstance(state, ParallelState):
+            # We use Rust structs for And-states
+            print("struct T%s {" % ident(state))
+            for child in children:
+                print("  s%s: T%s," % (child, child))
+            print("}")
+        elif isinstance(state, HistoryState):
+            print("Skipping HistoryState: ", state.opt.full_name)
+        elif isinstance(state, State):
+            # Or-state (with children) or Basic state (without children)
+            # We use Rust enums (typed unions)
+            print("enum T%s {" % ident(state))
+            for child in children:
+                print("  s%s(T%s)," % (child, child))
+            print("}")
+        print()
+        return ident(state)
+
+    visit_tree(tree.root, lambda s: s.children,
+        parent_first=[],
+        child_first=[write_state_type])
+
+
+    # 2. Write "enter default state" functions
+
+
+    # 3. Write transition functions
+
+
+    # 4. Write transition candidate generation function