|
|
@@ -7,6 +7,7 @@ from sccd.statechart.static.statechart import *
|
|
|
from sccd.statechart.static.globals import *
|
|
|
from sccd.statechart.static import priority
|
|
|
from sccd.util.indenting_writer import *
|
|
|
+from sccd.util.wasm import *
|
|
|
|
|
|
# Hardcoded limit on number of sub-rounds of combo and big step to detect never-ending superrounds.
|
|
|
# TODO: make this a model parameter, also allowing for +infinity
|
|
|
@@ -182,13 +183,15 @@ class StatechartRustGenerator(ActionLangRustGenerator):
|
|
|
# TODO: evaluate event parameters
|
|
|
if DEBUG:
|
|
|
self.w.writeln("eprintln!(\"raise out %s:%s\");" % (event.outport, event.name))
|
|
|
- self.w.writeln("(output)(OutEvent::%s(%s{}));" % (ident_event_enum_variant(event.name), ident_event_type(event.name)))
|
|
|
+ # self.w.writeln("(output)(OutEvent::%s(%s{}));" % (ident_event_enum_variant(event.name), ident_event_type(event.name)))
|
|
|
+ self.w.writeln("(output)(OutEvent::%s);" % (ident_event_enum_variant(event.name)))
|
|
|
|
|
|
def visit_RaiseInternalEvent(self, event):
|
|
|
if DEBUG:
|
|
|
self.w.writeln("eprintln!(\"raise internal %s\");" % (event.name))
|
|
|
if self.internal_queue:
|
|
|
- self.w.writeln("sched.set_timeout(%d, InEvent::%s(%s{}));" % (0, ident_event_enum_variant(event.name), ident_event_type(event.name)))
|
|
|
+ # self.w.writeln("sched.set_timeout(%d, InEvent::%s(%s{}));" % (0, ident_event_enum_variant(event.name), ident_event_type(event.name)))
|
|
|
+ self.w.writeln("sched.set_timeout(%d, InEvent::%s);" % (0, ident_event_enum_variant(event.name)))
|
|
|
else:
|
|
|
self.w.writeln("internal.raise().%s = Some(%s{});" % (ident_event_field(event.name), (ident_event_type(event.name))))
|
|
|
|
|
|
@@ -239,7 +242,7 @@ class StatechartRustGenerator(ActionLangRustGenerator):
|
|
|
self.w.writeln("impl %s {" % ident_type(state))
|
|
|
|
|
|
# Enter actions: Executes enter actions of only this state
|
|
|
- self.w.writeln(" fn enter_actions<TimerId: Copy, Sched: statechart::Scheduler<InEvent, TimerId>, OutputCallback: FnMut(OutEvent)>(timers: &mut Timers<TimerId>, data: &mut DataModel, internal: &mut InternalLifeline, sched: &mut Sched, output: &mut OutputCallback) {")
|
|
|
+ self.w.writeln(" fn enter_actions<TimerId: Default, Sched: statechart::Scheduler<InEvent, TimerId>, OutputCallback: FnMut(OutEvent)>(timers: &mut Timers<TimerId>, data: &mut DataModel, internal: &mut InternalLifeline, sched: &mut Sched, output: &mut OutputCallback) {")
|
|
|
if DEBUG:
|
|
|
self.w.writeln(" eprintln!(\"enter %s\");" % state.full_name);
|
|
|
self.w.writeln(" let scope = data;")
|
|
|
@@ -248,14 +251,15 @@ class StatechartRustGenerator(ActionLangRustGenerator):
|
|
|
a.accept(self)
|
|
|
self.w.dedent(); self.w.dedent()
|
|
|
for a in state.after_triggers:
|
|
|
- self.w.writeln(" timers[%d] = sched.set_timeout(%d, InEvent::%s(%s{}));" % (a.after_id, a.delay.opt, ident_event_enum_variant(a.enabling[0].name), ident_event_type(a.enabling[0].name)))
|
|
|
+ # self.w.writeln(" timers[%d] = sched.set_timeout(%d, InEvent::%s(%s{}));" % (a.after_id, a.delay.opt, ident_event_enum_variant(a.enabling[0].name), ident_event_type(a.enabling[0].name)))
|
|
|
+ self.w.writeln(" timers[%d] = sched.set_timeout(%d, InEvent::%s);" % (a.after_id, a.delay.opt, ident_event_enum_variant(a.enabling[0].name)))
|
|
|
self.w.writeln(" }")
|
|
|
|
|
|
# Enter actions: Executes exit actions of only this state
|
|
|
- self.w.writeln(" fn exit_actions<TimerId: Copy, Sched: statechart::Scheduler<InEvent, TimerId>, OutputCallback: FnMut(OutEvent)>(timers: &mut Timers<TimerId>, data: &mut DataModel, internal: &mut InternalLifeline, sched: &mut Sched, output: &mut OutputCallback) {")
|
|
|
+ self.w.writeln(" fn exit_actions<TimerId: Default, Sched: statechart::Scheduler<InEvent, TimerId>, OutputCallback: FnMut(OutEvent)>(timers: &mut Timers<TimerId>, data: &mut DataModel, internal: &mut InternalLifeline, sched: &mut Sched, output: &mut OutputCallback) {")
|
|
|
self.w.writeln(" let scope = data;")
|
|
|
for a in state.after_triggers:
|
|
|
- self.w.writeln(" sched.unset_timeout(timers[%d]);" % (a.after_id))
|
|
|
+ self.w.writeln(" sched.unset_timeout(&timers[%d]);" % (a.after_id))
|
|
|
self.w.indent(); self.w.indent()
|
|
|
for a in state.exit:
|
|
|
a.accept(self)
|
|
|
@@ -265,7 +269,7 @@ class StatechartRustGenerator(ActionLangRustGenerator):
|
|
|
self.w.writeln(" }")
|
|
|
|
|
|
# Enter default: Executes enter actions of entering this state and its default substates, recursively
|
|
|
- self.w.writeln(" fn enter_default<TimerId: Copy, Sched: statechart::Scheduler<InEvent, TimerId>, OutputCallback: FnMut(OutEvent)>(timers: &mut Timers<TimerId>, data: &mut DataModel, internal: &mut InternalLifeline, sched: &mut Sched, output: &mut OutputCallback) {")
|
|
|
+ self.w.writeln(" fn enter_default<TimerId: Default, Sched: statechart::Scheduler<InEvent, TimerId>, OutputCallback: FnMut(OutEvent)>(timers: &mut Timers<TimerId>, data: &mut DataModel, internal: &mut InternalLifeline, sched: &mut Sched, output: &mut OutputCallback) {")
|
|
|
self.w.writeln(" %s::enter_actions(timers, data, internal, sched, output);" % (ident_type(state)))
|
|
|
if isinstance(state.type, AndState):
|
|
|
for child in state.real_children:
|
|
|
@@ -275,7 +279,7 @@ class StatechartRustGenerator(ActionLangRustGenerator):
|
|
|
self.w.writeln(" }")
|
|
|
|
|
|
# Exit current: Executes exit actions of this state and current children, recursively
|
|
|
- self.w.writeln(" fn exit_current<TimerId: Copy, Sched: statechart::Scheduler<InEvent, TimerId>, OutputCallback: FnMut(OutEvent)>(&self, timers: &mut Timers<TimerId>, data: &mut DataModel, internal: &mut InternalLifeline, sched: &mut Sched, output: &mut OutputCallback) {")
|
|
|
+ self.w.writeln(" fn exit_current<TimerId: Default, Sched: statechart::Scheduler<InEvent, TimerId>, OutputCallback: FnMut(OutEvent)>(&self, timers: &mut Timers<TimerId>, data: &mut DataModel, internal: &mut InternalLifeline, sched: &mut Sched, output: &mut OutputCallback) {")
|
|
|
# first, children (recursion):
|
|
|
if isinstance(state.type, AndState):
|
|
|
for child in state.real_children:
|
|
|
@@ -290,7 +294,7 @@ class StatechartRustGenerator(ActionLangRustGenerator):
|
|
|
self.w.writeln(" }")
|
|
|
|
|
|
# Exit current: Executes enter actions of this state and current children, recursively
|
|
|
- self.w.writeln(" fn enter_current<TimerId: Copy, Sched: statechart::Scheduler<InEvent, TimerId>, OutputCallback: FnMut(OutEvent)>(&self, timers: &mut Timers<TimerId>, data: &mut DataModel, internal: &mut InternalLifeline, sched: &mut Sched, output: &mut OutputCallback) {")
|
|
|
+ self.w.writeln(" fn enter_current<TimerId: Default, Sched: statechart::Scheduler<InEvent, TimerId>, OutputCallback: FnMut(OutEvent)>(&self, timers: &mut Timers<TimerId>, data: &mut DataModel, internal: &mut InternalLifeline, sched: &mut Sched, output: &mut OutputCallback) {")
|
|
|
# first, parent:
|
|
|
self.w.writeln(" %s::enter_actions(timers, data, internal, sched, output);" % (ident_type(state)))
|
|
|
# then, children (recursion):
|
|
|
@@ -353,16 +357,21 @@ class StatechartRustGenerator(ActionLangRustGenerator):
|
|
|
# Until we implement event parameters, event types are just empty structs
|
|
|
|
|
|
self.w.writeln("// Input Events")
|
|
|
- for event_name in input_event_names:
|
|
|
- self.w.writeln("#[derive(Copy, Clone, Debug)]")
|
|
|
- self.w.writeln("pub struct %s {" % ident_event_type(event_name))
|
|
|
- self.w.writeln(" // TODO: input event parameters")
|
|
|
- self.w.writeln("}")
|
|
|
-
|
|
|
+ # for event_name in input_event_names:
|
|
|
+ # if WASM:
|
|
|
+ # self.w.writeln("#[wasm_bindgen]")
|
|
|
+ # self.w.writeln("#[derive(Copy, Clone, Debug)]")
|
|
|
+ # self.w.writeln("pub struct %s {" % ident_event_type(event_name))
|
|
|
+ # self.w.writeln(" // TODO: input event parameters")
|
|
|
+ # self.w.writeln("}")
|
|
|
+
|
|
|
+ if WASM:
|
|
|
+ self.w.writeln("#[wasm_bindgen]")
|
|
|
self.w.writeln("#[derive(Copy, Clone, Debug)]")
|
|
|
self.w.writeln("pub enum InEvent {")
|
|
|
for event_name in input_event_names:
|
|
|
- self.w.writeln(" %s(%s)," % (ident_event_enum_variant(event_name), ident_event_type(event_name)))
|
|
|
+ # self.w.writeln(" %s(%s)," % (ident_event_enum_variant(event_name), ident_event_type(event_name)))
|
|
|
+ self.w.writeln(" %s," % (ident_event_enum_variant(event_name)))
|
|
|
self.w.writeln("}")
|
|
|
self.w.writeln()
|
|
|
|
|
|
@@ -392,16 +401,21 @@ class StatechartRustGenerator(ActionLangRustGenerator):
|
|
|
# Output events
|
|
|
output_event_names = self.globals.out_events.names
|
|
|
self.w.writeln("// Output Events")
|
|
|
- for event_name in output_event_names:
|
|
|
- self.w.writeln("#[derive(Copy, Clone, Debug, PartialEq, Eq)]")
|
|
|
- self.w.writeln("pub struct %s {" % ident_event_type(event_name))
|
|
|
- self.w.writeln(" // TODO: output event parameters")
|
|
|
- self.w.writeln("}")
|
|
|
-
|
|
|
+ # for event_name in output_event_names:
|
|
|
+ # if WASM:
|
|
|
+ # self.w.writeln("#[wasm_bindgen]")
|
|
|
+ # self.w.writeln("#[derive(Copy, Clone, Debug, PartialEq, Eq)]")
|
|
|
+ # self.w.writeln("pub struct %s {" % ident_event_type(event_name))
|
|
|
+ # self.w.writeln(" // TODO: output event parameters")
|
|
|
+ # self.w.writeln("}")
|
|
|
+
|
|
|
+ if WASM:
|
|
|
+ self.w.writeln("#[wasm_bindgen]")
|
|
|
self.w.writeln("#[derive(Copy, Clone, Debug, PartialEq, Eq)]")
|
|
|
self.w.writeln("pub enum OutEvent {")
|
|
|
for event_name in output_event_names:
|
|
|
- self.w.writeln(" %s(%s)," % (ident_event_enum_variant(event_name), ident_event_type(event_name)))
|
|
|
+ # self.w.writeln(" %s(%s)," % (ident_event_enum_variant(event_name), ident_event_type(event_name)))
|
|
|
+ self.w.writeln(" %s," % (ident_event_enum_variant(event_name)))
|
|
|
self.w.writeln("}")
|
|
|
self.w.writeln()
|
|
|
|
|
|
@@ -463,7 +477,7 @@ class StatechartRustGenerator(ActionLangRustGenerator):
|
|
|
self.w.writeln()
|
|
|
|
|
|
# Function fair_step: a single "Take One" Maximality 'round' (= nonoverlapping arenas allowed to fire 1 transition)
|
|
|
- self.w.writeln("fn fair_step<TimerId: Copy, Sched: statechart::Scheduler<InEvent, TimerId>, OutputCallback: FnMut(OutEvent)>(sc: &mut Statechart<TimerId>, input: &mut Option<InEvent>, internal: &mut InternalLifeline, sched: &mut Sched, output: &mut OutputCallback, dirty: Arenas) -> Arenas {")
|
|
|
+ self.w.writeln("fn fair_step<TimerId: Default, Sched: statechart::Scheduler<InEvent, TimerId>, OutputCallback: FnMut(OutEvent)>(sc: &mut Statechart<TimerId>, input: &mut Option<InEvent>, internal: &mut InternalLifeline, sched: &mut Sched, output: &mut OutputCallback, dirty: Arenas) -> Arenas {")
|
|
|
self.w.writeln(" let mut fired: Arenas = ARENA_NONE;")
|
|
|
self.w.writeln(" let mut scope = &mut sc.data;")
|
|
|
self.w.writeln(" let %s = &mut sc.configuration;" % ident_var(tree.root))
|
|
|
@@ -610,7 +624,8 @@ class StatechartRustGenerator(ActionLangRustGenerator):
|
|
|
condition = []
|
|
|
for e in t.trigger.enabling:
|
|
|
if bit(e.id) & input_events:
|
|
|
- condition.append("let Some(InEvent::%s(%s{})) = &input" % (ident_event_enum_variant(e.name), ident_event_type(e.name)))
|
|
|
+ # condition.append("let Some(InEvent::%s(%s{})) = &input" % (ident_event_enum_variant(e.name), ident_event_type(e.name)))
|
|
|
+ condition.append("let Some(InEvent::%s) = &input" % (ident_event_enum_variant(e.name)))
|
|
|
elif bit(e.id) & internal_events:
|
|
|
condition.append("let Some(%s) = &internal.current().%s" % (ident_event_type(e.name), ident_event_field(e.name)))
|
|
|
else:
|
|
|
@@ -755,7 +770,7 @@ class StatechartRustGenerator(ActionLangRustGenerator):
|
|
|
|
|
|
# Write combo step and big step function
|
|
|
def write_stepping_function(name: str, title: str, maximality: Maximality, substep: str, cycle_input: bool, cycle_internal: bool):
|
|
|
- self.w.writeln("fn %s<TimerId: Copy, Sched: statechart::Scheduler<InEvent, TimerId>, OutputCallback: FnMut(OutEvent)>(sc: &mut Statechart<TimerId>, input: &mut Option<InEvent>, internal: &mut InternalLifeline, sched: &mut Sched, output: &mut OutputCallback, dirty: Arenas) -> Arenas {" % (name))
|
|
|
+ self.w.writeln("fn %s<TimerId: Default, Sched: statechart::Scheduler<InEvent, TimerId>, OutputCallback: FnMut(OutEvent)>(sc: &mut Statechart<TimerId>, input: &mut Option<InEvent>, internal: &mut InternalLifeline, sched: &mut Sched, output: &mut OutputCallback, dirty: Arenas) -> Arenas {" % (name))
|
|
|
self.w.writeln(" // %s Maximality: %s" % (title, maximality))
|
|
|
if maximality == Maximality.TAKE_ONE:
|
|
|
self.w.writeln(" %s(sc, input, internal, sched, output, dirty)" % (substep))
|
|
|
@@ -801,7 +816,7 @@ class StatechartRustGenerator(ActionLangRustGenerator):
|
|
|
self.w.writeln()
|
|
|
|
|
|
# Implement 'SC' trait
|
|
|
- self.w.writeln("impl<TimerId: Copy, Sched: statechart::Scheduler<InEvent, TimerId>, OutputCallback: FnMut(OutEvent)> statechart::SC<InEvent, TimerId, Sched, OutputCallback> for Statechart<TimerId> {")
|
|
|
+ self.w.writeln("impl<TimerId: Default, Sched: statechart::Scheduler<InEvent, TimerId>, OutputCallback: FnMut(OutEvent)> statechart::SC<InEvent, TimerId, Sched, OutputCallback> for Statechart<TimerId> {")
|
|
|
self.w.writeln(" fn init(&mut self, sched: &mut Sched, output: &mut OutputCallback) {")
|
|
|
self.w.writeln(" %s::enter_default(&mut self.timers, &mut self.data, &mut Default::default(), sched, output)" % (ident_type(tree.root)))
|
|
|
self.w.writeln(" }")
|
|
|
@@ -819,7 +834,7 @@ class StatechartRustGenerator(ActionLangRustGenerator):
|
|
|
|
|
|
if DEBUG:
|
|
|
self.w.writeln("use std::mem::size_of;")
|
|
|
- self.w.writeln("fn debug_print_sizes<TimerId: Copy>() {")
|
|
|
+ self.w.writeln("fn debug_print_sizes<TimerId: Default>() {")
|
|
|
self.w.writeln(" eprintln!(\"------------------------\");")
|
|
|
self.w.writeln(" eprintln!(\"Semantics: %s\");" % sc.semantics)
|
|
|
self.w.writeln(" eprintln!(\"------------------------\");")
|