Skip to content

Commit 37353f2

Browse files
committed
make loop increment after the loop runs
this makes it more consistent with other programming languages
1 parent 6dd3179 commit 37353f2

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

Diff for: src/codegen/sb3.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,17 @@ where T: Write + Seek
354354
)
355355
}
356356

357+
pub fn stmts(
358+
&mut self,
359+
s: S,
360+
d: D,
361+
stmts: &[Stmt],
362+
this_id: NodeID,
363+
parent_id: Option<NodeID>,
364+
) -> io::Result<()> {
365+
self.stmts_with_next(s, d, stmts, this_id, parent_id, None)
366+
}
367+
357368
pub fn substack(&mut self, name: &str, this_id: Option<NodeID>) -> io::Result<()> {
358369
let Some(this_id) = this_id else {
359370
return Ok(());
@@ -967,18 +978,20 @@ where T: Write + Seek
967978
self.stmts(s, d, &event.body, next_id, Some(this_id))
968979
}
969980

970-
pub fn stmts(
981+
pub fn stmts_with_next(
971982
&mut self,
972983
s: S,
973984
d: D,
974985
stmts: &[Stmt],
975986
mut this_id: NodeID,
976987
mut parent_id: Option<NodeID>,
988+
last_id: Option<NodeID>,
977989
) -> io::Result<()> {
978990
for (i, stmt) in stmts.iter().enumerate() {
979991
let is_last = i == stmts.len() - 1;
980992
if is_last || stmt.is_terminator() {
981-
self.stmt(s, d, stmt, this_id, None, parent_id)?;
993+
let next_id = last_id;
994+
self.stmt(s, d, stmt, this_id, next_id, parent_id)?;
982995
if !is_last {
983996
d.report(DiagnosticKind::FollowedByUnreachableCode, stmt.span());
984997
}

Diff for: src/codegen/stmt.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ where T: Write + Seek
6969
let body_id = self.id.new_id();
7070
self.begin_inputs()?;
7171
self.input(s, d, "CONDITION", cond, cond_id)?;
72-
self.substack("SUBSTACK", Some(incr_id))?;
72+
self.substack("SUBSTACK", Some((!body.is_empty()).then_some(body_id).unwrap_or(incr_id)))?;
7373
self.end_obj()?; // inputs
7474
self.end_obj()?; // node
7575
self.expr(s, d, cond, cond_id, this_id)?;
76-
self.stmt(s, d, incr, incr_id, Some(body_id), Some(this_id))?;
77-
self.stmts(s, d, body, body_id, Some(incr_id))
76+
self.stmts_with_next(s, d, body, body_id, Some(this_id), Some(incr_id))?;
77+
self.stmt(s, d, incr, incr_id, None, Some(body_id))
7878
}
7979

8080
pub fn foreach(

0 commit comments

Comments
 (0)