Skip to content

Commit

Permalink
Merge pull request #14 from mrLSD/feat/improve-return-for-body-statem…
Browse files Browse the repository at this point in the history
…ents

Feat: refactor `if-end` jump logic
  • Loading branch information
mrLSD authored Nov 8, 2023
2 parents b25203d + 47e1adf commit 7e8883a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "semantic-analyzer"
version = "0.2.4"
version = "0.2.5"
authors = ["Evgeny Ukhanov <mrlsd@ya.ru>"]
description = "Semantic analyzer library for compilers written in Rust for semantic analysis of programming languages AST"
keywords = ["compiler", "semantic-analisis", "semantic-alalyzer", "compiler-design", "semantic"]
Expand Down
26 changes: 15 additions & 11 deletions src/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,12 +854,14 @@ impl State {
);
}
}
// Codegen for jump to if-end statement - return to program flow
// TODO: issue #9
if_body_state
.borrow_mut()
.context
.jump_to(label_if_end.clone());
// Codegen for jump to if-end statement - return to program flow.
// If return is set do not add jump-to-end label.
if !if_body_state.borrow().manual_return {
if_body_state
.borrow_mut()
.context
.jump_to(label_if_end.clone());
}

// Check else statements: else, else-if
if is_else {
Expand Down Expand Up @@ -901,11 +903,13 @@ impl State {
}

// Codegen for jump to if-end statement -return to program flow
// TODO: issue #9
if_body_state
.borrow_mut()
.context
.jump_to(label_if_end.clone());
// If return is set do not add jump-to-end label.
if !if_body_state.borrow().manual_return {
if_body_state
.borrow_mut()
.context
.jump_to(label_if_end.clone());
}
} else if let Some(else_if_statement) = &data.else_if_statement {
// Analyse else-if statement
// Set `label_if_end` to indicate single if-end point
Expand Down
40 changes: 5 additions & 35 deletions tests/if_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ fn else_if_statement() {
assert!(ch_ctx3.borrow().children.is_empty());

let ctx1 = ch_ctx1.borrow().context.clone().get();
assert_eq!(ctx1.len(), 6);
assert_eq!(ctx1.len(), 5);
assert_eq!(
ctx1[0],
SemanticStackContext::IfConditionExpression {
Expand Down Expand Up @@ -744,25 +744,19 @@ fn else_if_statement() {
);
assert_eq!(
ctx1[3],
SemanticStackContext::JumpTo {
label: String::from("if_end").into()
}
);
assert_eq!(
ctx1[4],
SemanticStackContext::SetLabel {
label: String::from("if_else").into()
}
);
assert_eq!(
ctx1[5],
ctx1[4],
SemanticStackContext::SetLabel {
label: String::from("if_end").into()
}
);

let ctx2 = ch_ctx2.borrow().context.clone().get();
assert_eq!(ctx2.len(), 6);
assert_eq!(ctx2.len(), 4);
assert_eq!(
ctx2[0],
SemanticStackContext::IfConditionExpression {
Expand Down Expand Up @@ -791,22 +785,10 @@ fn else_if_statement() {
);
assert_eq!(
ctx2[3],
SemanticStackContext::JumpTo {
label: String::from("if_end").into()
}
);
assert_eq!(
ctx2[4],
SemanticStackContext::SetLabel {
label: String::from("if_else.0").into()
}
);
assert_eq!(
ctx2[5],
SemanticStackContext::JumpTo {
label: String::from("if_end").into()
}
);

let ctx3 = ch_ctx3.borrow().context.clone().get();
assert_eq!(ctx3.len(), 1);
Expand Down Expand Up @@ -922,7 +904,7 @@ fn if_body_statements() {
assert_eq!(ctx.borrow().children.len(), 2);

let stm_ctx = ctx.borrow().context.clone().get();
assert_eq!(stm_ctx.len(), 8);
assert_eq!(stm_ctx.len(), 7);
assert_eq!(
stm_ctx[0],
SemanticStackContext::IfConditionExpression {
Expand Down Expand Up @@ -994,12 +976,6 @@ fn if_body_statements() {
);
assert_eq!(
stm_ctx[6],
SemanticStackContext::JumpTo {
label: String::from("if_end").into()
}
);
assert_eq!(
stm_ctx[7],
SemanticStackContext::SetLabel {
label: String::from("if_end").into()
}
Expand Down Expand Up @@ -1206,7 +1182,7 @@ fn if_loop_body_statements() {
assert_eq!(ctx.borrow().children.len(), 2);

let stm_ctx = ctx.borrow().context.clone().get();
assert_eq!(stm_ctx.len(), 10);
assert_eq!(stm_ctx.len(), 9);
assert_eq!(
stm_ctx[0],
SemanticStackContext::IfConditionExpression {
Expand Down Expand Up @@ -1290,12 +1266,6 @@ fn if_loop_body_statements() {
);
assert_eq!(
stm_ctx[8],
SemanticStackContext::JumpTo {
label: String::from("if_end").into()
}
);
assert_eq!(
stm_ctx[9],
SemanticStackContext::SetLabel {
label: String::from("if_end").into()
}
Expand Down
8 changes: 1 addition & 7 deletions tests/main_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ fn if_return_from_function() {

// Children semantic stack context for the block
let st_children_ctx = children_ctx.context.clone().get();
assert_eq!(st_children_ctx.len(), 5);
assert_eq!(st_children_ctx.len(), 4);
assert_eq!(
st_children_ctx[0],
SemanticStackContext::IfConditionExpression {
Expand Down Expand Up @@ -495,12 +495,6 @@ fn if_return_from_function() {
);
assert_eq!(
st_children_ctx[3],
SemanticStackContext::JumpTo {
label: String::from("if_end").into()
}
);
assert_eq!(
st_children_ctx[4],
SemanticStackContext::SetLabel {
label: String::from("if_end").into()
}
Expand Down

0 comments on commit 7e8883a

Please sign in to comment.