Skip to content

Commit b331dc0

Browse files
committed
Merge branch 'main' of github-frame-site:frame-lang/frame_transpiler
2 parents a9e76f3 + 25aa719 commit b331dc0

File tree

18 files changed

+179
-109
lines changed

18 files changed

+179
-109
lines changed

frame_runtime/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,4 @@ pub use crate::event::*;
101101
pub use crate::history::*;
102102
pub use crate::info::*;
103103
pub use crate::machine::*;
104-
pub use crate::smcat::*;
105104
pub use crate::transition::*;

frame_runtime/src/smcat.rs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -101,49 +101,46 @@ pub trait Style {
101101
}
102102

103103
/// A style implementation that relegates all formatting to CSS via the "class" style options.
104+
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
104105
pub struct CssStyle;
105106

106107
/// A simple style implementation that doesn't require CSS.
108+
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
107109
pub struct SimpleStyle;
108110

109111
impl Style for CssStyle {
110112
fn node(&self, info: &StateInfo, active: bool) -> NodeStyle {
111113
let mut classes = Vec::new();
112-
if active {
113-
classes.push("active");
114-
}
115-
if !info.children().is_empty() {
116-
classes.push("parent");
117-
}
118114
if info.is_stack_pop {
119115
classes.push("stack-pop");
116+
} else if info.children().is_empty() {
117+
classes.push("simple");
118+
} else {
119+
classes.push("parent");
120+
}
121+
if active {
122+
classes.push("active");
120123
}
121124
NodeStyle {
122125
// TODO Not sure if the "active" attribute is "semantic" or purely for rendering style.
123126
// If it's purely style, then we should not enable it since we'll set the style in CSS.
124127
// active,
125-
class: if classes.is_empty() {
126-
None
127-
} else {
128-
Some(classes.join(" "))
129-
},
128+
class: Some(classes.join(" ")),
130129
..NodeStyle::default()
131130
}
132131
}
133132
fn edge(&self, info: &TransitionInfo, active: bool) -> EdgeStyle {
134133
let mut classes = Vec::new();
135-
if active {
136-
classes.push("active");
137-
}
138134
if info.is_change_state() {
139135
classes.push("change-state");
136+
} else {
137+
classes.push("standard");
138+
}
139+
if active {
140+
classes.push("active");
140141
}
141142
EdgeStyle {
142-
class: if classes.is_empty() {
143-
None
144-
} else {
145-
Some(classes.join(" "))
146-
},
143+
class: Some(classes.join(" ")),
147144
..EdgeStyle::default()
148145
}
149146
}
@@ -175,20 +172,21 @@ impl Style for SimpleStyle {
175172
}
176173

177174
/// Generates smcat diagrams from Frame state machines.
178-
pub struct Renderer {
179-
style: Box<dyn Style>,
175+
#[derive(Clone)]
176+
pub struct Renderer<S: Style> {
177+
style: S,
180178
}
181179

182-
impl Renderer {
180+
impl<S: Style> Renderer<S> {
183181
/// Create a new renderer with the given style configuration.
184-
pub fn new(style: Box<dyn Style>) -> Self {
182+
pub fn new(style: S) -> Self {
185183
Renderer { style }
186184
}
187185

188186
/// Generate an smcat diagram illustrating the structure of a state machine, independent of any
189187
/// particular execution.
190188
pub fn render_static(&self, machine_info: &MachineInfo) -> String {
191-
self.render_common(machine_info, None, None)
189+
self.render(machine_info, None, None)
192190
}
193191

194192
/// Generate an smcat diagram from a snapshot of a running state machine. Depending on the
@@ -207,10 +205,12 @@ impl Renderer {
207205
.transition_history()
208206
.newest()
209207
.map(|t| t.info.id);
210-
self.render_common(machine_info, Some(active_state), last_transition)
208+
self.render(machine_info, Some(active_state), last_transition)
211209
}
212210

213-
pub fn render_common(
211+
/// Generate an smcat diagram, highlighing the given active state and last transition (if
212+
/// provided) according to the associated style configurating.
213+
pub fn render(
214214
&self,
215215
machine_info: &MachineInfo,
216216
active_state: Option<&'static str>,
@@ -226,11 +226,11 @@ impl Renderer {
226226
&machine_info.top_level_states(),
227227
&mut output,
228228
);
229-
output.push_str(";\n");
229+
output.push('\n');
230230

231231
// render transitions
232232
if let Some(init) = machine_info.initial_state() {
233-
output.push_str(&format!("initial => {};\n", init.name));
233+
output.push_str(&format!("initial -> {};\n", init.name));
234234
}
235235
for transition in machine_info.transitions {
236236
self.render_transition(last_transition, transition, &mut output);
@@ -259,6 +259,8 @@ impl Renderer {
259259
}
260260
if state_iter.peek().is_some() {
261261
output.push_str(",\n");
262+
} else {
263+
output.push_str(";\n");
262264
}
263265
}
264266
}

frame_runtime/src/transition.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,7 @@ where
6767
enter_event: None,
6868
}
6969
}
70-
}
7170

72-
impl<M: Machine> Transition<M>
73-
where
74-
<M::EnvironmentPtr as Deref>::Target: Environment,
75-
<M::EventPtr as Deref>::Target: Event<M>,
76-
<M::StatePtr as Deref>::Target: State<M>,
77-
{
7871
/// Get the arguments from the exit event, or an empty environment if there is no exit
7972
/// event.
8073
pub fn exit_arguments(&self) -> M::EnvironmentPtr {

frame_runtime/tests/demo.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -522,42 +522,50 @@ mod tests {
522522
}
523523

524524
use indoc::indoc;
525+
525526
const SMCAT_STATIC: &str = indoc! {r#"
526527
initial,
527528
Init,
528529
Foo,
529530
Bar;
530-
initial => Init;
531+
532+
initial -> Init;
531533
Init -> Foo : " Init:> ";
532534
Foo -> Bar : " next ";
533535
Bar -> Foo [color="grey"] : " next ";
534536
"#};
537+
535538
const SMCAT_LIVE_1: &str = indoc! {r#"
536539
initial,
537540
Init,
538541
Foo [active color="red"],
539542
Bar;
540-
initial => Init;
543+
544+
initial -> Init;
541545
Init -> Foo [color="red" width=2] : " Init:> ";
542546
Foo -> Bar : " next ";
543547
Bar -> Foo [color="grey"] : " next ";
544548
"#};
549+
545550
const SMCAT_LIVE_2: &str = indoc! {r#"
546551
initial,
547552
Init,
548553
Foo,
549554
Bar [active color="red"];
550-
initial => Init;
555+
556+
initial -> Init;
551557
Init -> Foo : " Init:> ";
552558
Foo -> Bar [color="red" width=2] : " next ";
553559
Bar -> Foo [color="grey"] : " next ";
554560
"#};
561+
555562
const SMCAT_LIVE_3: &str = indoc! {r#"
556563
initial,
557564
Init,
558565
Foo [active color="red"],
559566
Bar;
560-
initial => Init;
567+
568+
initial -> Init;
561569
Init -> Foo : " Init:> ";
562570
Foo -> Bar : " next ";
563571
Bar -> Foo [color="pink" width=2] : " next ";
@@ -566,15 +574,15 @@ mod tests {
566574
#[test]
567575
fn smcat_render_static() {
568576
use frame_runtime::smcat::*;
569-
let smcat = Renderer::new(Box::new(SimpleStyle));
577+
let smcat = Renderer::new(SimpleStyle);
570578
assert_eq!(smcat.render_static(super::info::machine()), SMCAT_STATIC);
571579
}
572580

573581
#[test]
574582
fn smcat_render_live() {
575583
use crate::demo::sync::*;
576584
use frame_runtime::smcat::*;
577-
let smcat = Renderer::new(Box::new(SimpleStyle));
585+
let smcat = Renderer::new(SimpleStyle);
578586

579587
let mut sm = Demo::new();
580588
assert_eq!(smcat.render_live(&sm), SMCAT_LIVE_1);

framec/src/frame_c/config.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,10 @@ impl Default for RustRuntime {
458458
impl Default for SmcatCode {
459459
fn default() -> Self {
460460
SmcatCode {
461-
simple_state_node_style: String::from("class=\"state simple\""),
462-
parent_state_node_style: String::from("class=\"state parent\""),
463-
change_state_edge_style: String::from("class=\"edge change-state\""),
464-
transition_edge_style: String::from("class=\"edge transition\""),
461+
simple_state_node_style: String::from("class=\"simple\""),
462+
parent_state_node_style: String::from("class=\"parent\""),
463+
change_state_edge_style: String::from("class=\"change-state\""),
464+
transition_edge_style: String::from("class=\"standard\""),
465465
}
466466
}
467467
}

framec/src/frame_c/visitors/cpp_visitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ impl AstVisitor for CppVisitor {
13651365

13661366
method_call.call_expr_list.accept(self);
13671367

1368-
self.add_code(&format!(""));
1368+
self.add_code("");
13691369
}
13701370

13711371
//* --------------------------------------------------------------------- *//
@@ -1386,7 +1386,7 @@ impl AstVisitor for CppVisitor {
13861386

13871387
method_call.call_expr_list.accept_to_string(self, output);
13881388

1389-
output.push_str(&format!(""));
1389+
output.push_str("");
13901390
}
13911391

13921392
//* --------------------------------------------------------------------- *//

framec/src/frame_c/visitors/cs_visitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@ impl AstVisitor for CsVisitor {
14341434

14351435
method_call.call_expr_list.accept(self);
14361436

1437-
self.add_code(&format!(""));
1437+
self.add_code("");
14381438
}
14391439

14401440
//* --------------------------------------------------------------------- *//
@@ -1455,7 +1455,7 @@ impl AstVisitor for CsVisitor {
14551455

14561456
method_call.call_expr_list.accept_to_string(self, output);
14571457

1458-
output.push_str(&format!(""));
1458+
output.push_str("");
14591459
}
14601460

14611461
//* --------------------------------------------------------------------- *//

framec/src/frame_c/visitors/cs_visitor_for_bob.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,7 @@ impl AstVisitor for CsVisitorForBob {
15001500

15011501
method_call.call_expr_list.accept(self);
15021502

1503-
self.add_code(&format!(""));
1503+
self.add_code("");
15041504
}
15051505

15061506
//* --------------------------------------------------------------------- *//
@@ -1521,7 +1521,7 @@ impl AstVisitor for CsVisitorForBob {
15211521

15221522
method_call.call_expr_list.accept_to_string(self, output);
15231523

1524-
output.push_str(&format!(""));
1524+
output.push_str("");
15251525
}
15261526

15271527
//* --------------------------------------------------------------------- *//

framec/src/frame_c/visitors/gdscript_3_2_visitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,7 @@ impl AstVisitor for GdScript32Visitor {
13131313

13141314
method_call.call_expr_list.accept(self);
13151315

1316-
self.add_code(&format!(""));
1316+
self.add_code("");
13171317
}
13181318

13191319
//* --------------------------------------------------------------------- *//
@@ -1334,7 +1334,7 @@ impl AstVisitor for GdScript32Visitor {
13341334

13351335
method_call.call_expr_list.accept_to_string(self, output);
13361336

1337-
output.push_str(&format!(""));
1337+
output.push_str("");
13381338
}
13391339

13401340
//* --------------------------------------------------------------------- *//

framec/src/frame_c/visitors/java_8_visitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,7 +1449,7 @@ impl AstVisitor for Java8Visitor {
14491449

14501450
method_call.call_expr_list.accept(self);
14511451

1452-
self.add_code(&format!(""));
1452+
self.add_code("");
14531453
}
14541454

14551455
//* --------------------------------------------------------------------- *//
@@ -1470,7 +1470,7 @@ impl AstVisitor for Java8Visitor {
14701470

14711471
method_call.call_expr_list.accept_to_string(self, output);
14721472

1473-
output.push_str(&format!(""));
1473+
output.push_str("");
14741474
}
14751475

14761476
//* --------------------------------------------------------------------- *//

0 commit comments

Comments
 (0)