Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions framec/src/frame_c/default_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ codegen:
state_var_name_suffix: _state
state_enum_suffix: State
state_enum_traits: Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord
initialize_method_name: initialize
transition_method_name: transition
change_state_method_name: change_state
transition_hook_method_name: transition_hook
Expand Down
78 changes: 56 additions & 22 deletions framec/src/frame_c/visitors/rust_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct Config {
state_var_name_suffix: String,
state_enum_suffix: String,
state_enum_traits: String,
initialize_method_name: String,
transition_method_name: String,
change_state_method_name: String,
transition_hook_method_name: Option<String>,
Expand Down Expand Up @@ -215,6 +216,10 @@ impl Config {
.as_str()
.unwrap_or_default()
.to_string(),
initialize_method_name: (&code_yaml["initialize_method_name"])
.as_str()
.unwrap_or_default()
.to_string(),
transition_method_name: (&code_yaml["transition_method_name"])
.as_str()
.unwrap_or_default()
Expand Down Expand Up @@ -899,22 +904,14 @@ impl RustVisitor {
&self.config.state_context_var_name, &self.config.state_context_var_name
));
self.newline();
self.add_code(&format!(
"let mut enter_event = {}::new({}::{}, None);",
self.config.frame_event_type_name,
self.config.frame_event_message_type_name,
self.config.enter_msg
));
self.newline();
} else {
self.add_code(&format!(
"let mut enter_event = {}::new({}::{}, None);",
self.config.frame_event_type_name,
self.config.frame_event_message_type_name,
self.config.enter_msg
));
self.newline();
}
self.add_code(&format!(
"let mut enter_event = {}::new({}::{}, None);",
self.config.frame_event_type_name,
self.config.frame_event_message_type_name,
self.config.enter_msg
));
self.newline();
self.add_code(&format!(
"(self.{})(self, &mut enter_event);",
&self.config.state_var_name
Expand Down Expand Up @@ -2169,9 +2166,10 @@ impl AstVisitor for RustVisitor {
None => {}
}

// generate constructor

if self.has_states {
// generate constructor

self.newline();
self.add_code(&format!("pub fn new() -> {} {{", system_node.name));
self.indent();

Expand Down Expand Up @@ -2217,7 +2215,7 @@ impl AstVisitor for RustVisitor {
}

self.newline();
self.add_code(&format!("{} {{", system_node.name));
self.add_code(&format!("let mut machine = {} {{", system_node.name));
self.indent();
self.newline();
self.add_code(&format!(
Expand Down Expand Up @@ -2262,14 +2260,50 @@ impl AstVisitor for RustVisitor {

self.outdent();
self.newline();
self.add_code(&format!("}}"));
self.add_code("};");

// run the initialize method on the new machine
self.newline();
self.add_code(&format!(
"machine.{}();",
self.config.initialize_method_name
));
self.newline();

// return the new machine
self.add_code("machine");

self.outdent();
self.newline();
self.add_code(&format!("}}"));
self.add_code("}");
self.newline();
}

// end of generate constructor
// end of generate constructor

// generate initialize method
self.newline();
self.add_code(&format!(
"pub fn {}(&mut self) {{",
self.config.initialize_method_name
));
self.indent();
self.newline();
self.add_code(&format!(
"let mut e = {}::new({}::{}, None);",
self.config.frame_event_type_name,
self.config.frame_event_message_type_name,
self.config.enter_msg
));
self.newline();
self.add_code(&format!(
"(self.{})(self, &mut e);",
&self.config.state_var_name
));
self.outdent();
self.newline();
self.add_code("}");
self.newline();
}

self.serialize.push("".to_string());
self.serialize.push("Bag _serialize__do() {".to_string());
Expand Down
3 changes: 1 addition & 2 deletions framec_tests/src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ mod tests {
// Revisit: currently generated code doesn't send entry event to state machine's initial
// state on creation
#[test]
#[ignore]
fn intial_state_entry_call() {
let sm = Basic::new();
assert_eq!(sm.entry_log, vec!["S1"]);
assert_eq!(sm.entry_log, vec!["S0"]);
}

#[test]
Expand Down