Explorar el Código

Rust: make fair_step function return a boolean indicating whether a transition was fired

Joeri Exelmans hace 5 años
padre
commit
84b5bce9db
Se han modificado 2 ficheros con 11 adiciones y 4 borrados
  1. 5 1
      src/sccd/cd/codegen/sccdlib.rs
  2. 6 3
      src/sccd/statechart/codegen/rust.py

+ 5 - 1
src/sccd/cd/codegen/sccdlib.rs

@@ -14,7 +14,11 @@ pub trait State<OutputCallback> {
 
 pub trait SC<EventType, OutputCallback> {
   fn init(&self, output: &mut OutputCallback);
-  fn fair_step(&mut self, event: Option<EventType>, output: &mut OutputCallback);
+
+  // Execute a 'fair step'. This is a step with Maximality == Take One,
+  // meaning that every orthogonal component gets to fire at most 1 transition.
+  // Returns whether at least one transition was fired.
+  fn fair_step(&mut self, event: Option<EventType>, output: &mut OutputCallback) -> bool;
 }
 
 pub struct Entry<EventType> {

+ 6 - 3
src/sccd/statechart/codegen/rust.py

@@ -231,8 +231,9 @@ def compile_statechart(sc: Statechart, globals: Globals, w: IndentingWriter):
     w.writeln("  fn init(&self, output: &mut OutputCallback) {")
     w.writeln("    %s::enter_default(output);" % ident_type(tree.root))
     w.writeln("  }")
-    w.writeln("  fn fair_step(&mut self, event: Option<Event>, output: &mut OutputCallback) {")
+    w.writeln("  fn fair_step(&mut self, event: Option<Event>, output: &mut OutputCallback) -> bool {")
     w.writeln("    println!(\"fair step\");")
+    w.writeln("    let mut fired = false;")
     w.writeln("    let %s = &mut self.current_state;" % ident_var(tree.root))
 
     w.indent()
@@ -369,6 +370,8 @@ def compile_statechart(sc: Statechart, globals: Globals, w: IndentingWriter):
                 w.writeln("// Update arena configuration")
                 w.writeln("*%s = new_%s;" % (ident_var(t.opt.arena), ident_var(t.opt.arena)))
 
+                w.writeln("fired = true;")
+
                 # This arena is done:
                 w.writeln("break '%s;" % (ident_arena_label(t.opt.arena)))
 
@@ -390,7 +393,6 @@ def compile_statechart(sc: Statechart, globals: Globals, w: IndentingWriter):
                     for child in state.children:
                         w.indent()
                         w.writeln("%s::%s(%s) => {" % (ident_type(state), ident_enum_variant(child), ident_var(child)))
-                        # w.writeln("%s::%s(_) => {" % (ident_type(state), ident_enum_variant(child)))
                         w.indent()
                         write_transitions(child)
                         w.dedent()
@@ -413,13 +415,14 @@ def compile_statechart(sc: Statechart, globals: Globals, w: IndentingWriter):
             parent()
             child()
         else:
-            raise Exception("Unsupported option %s" % sc.semantics.hierarchical_priority)
+            raise Exception("Unsupported semantics %s" % sc.semantics.hierarchical_priority)
 
     write_transitions(tree.root)
 
     w.dedent()
     w.dedent()
 
+    w.writeln("    fired")
     w.writeln("  }")
     w.writeln("}")
     w.writeln()