Skip to content

Commit

Permalink
Fix flow control commands scope handling of call info stack
Browse files Browse the repository at this point in the history
  • Loading branch information
sagiegurari committed Mar 5, 2021
1 parent a72e420 commit 63b55e0
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## CHANGELOG

### v0.7.4 (2021-03-05)

* Fix flow control commands scope handling of call info stack

### v0.7.3 (2021-03-01)

* New hex_decode and hex_encode command #166 (thanks @umaYnit)
Expand Down
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion duckscript_cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "duckscript_cli"
version = "0.7.3"
version = "0.7.4"
authors = ["Sagie Gur-Ari <[email protected]>"]
description = "The duckscript command line executable."
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion duckscript_sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "duckscriptsdk"
version = "0.7.3"
version = "0.7.4"
authors = ["Sagie Gur-Ari <[email protected]>"]
description = "The duckscript SDK."
license = "Apache-2.0"
Expand Down
35 changes: 30 additions & 5 deletions duckscript_sdk/src/sdk/std/flowcontrol/forin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::sdk::std::flowcontrol::{end, function, get_line_key, ifelse, while_mod};
use crate::types::scope::get_line_context_name;
use crate::utils::state::{
get_as_string, get_core_sub_state_for_command, get_handle, get_list, get_sub_state,
};
Expand Down Expand Up @@ -27,6 +28,7 @@ struct ForInMetaInfo {
struct CallInfo {
pub(crate) iteration: usize,
pub(crate) meta_info: ForInMetaInfo,
pub(crate) line_context_name: String,
}

fn serialize_forin_meta_info(
Expand Down Expand Up @@ -73,6 +75,10 @@ fn serialize_call_info(call_info: &CallInfo, sub_state: &mut HashMap<String, Sta
"meta_info".to_string(),
StateValue::SubState(meta_info_state),
);
sub_state.insert(
"line_context_name".to_string(),
StateValue::String(call_info.line_context_name.clone()),
);
}

fn deserialize_call_info(sub_state: &mut HashMap<String, StateValue>) -> Option<CallInfo> {
Expand All @@ -93,10 +99,18 @@ fn deserialize_call_info(sub_state: &mut HashMap<String, StateValue>) -> Option<
},
None => return None,
};
let line_context_name = match sub_state.get("line_context_name") {
Some(value) => match value {
StateValue::String(line_context_name) => line_context_name.clone(),
_ => return None,
},
None => return None,
};

Some(CallInfo {
iteration,
meta_info,
line_context_name,
})
}

Expand Down Expand Up @@ -203,6 +217,7 @@ fn pop_call_info_for_line(
state: &mut HashMap<String, StateValue>,
recursive: bool,
) -> Option<CallInfo> {
let line_context_name = get_line_context_name(state);
let forin_state = get_core_sub_state_for_command(state, FORIN_STATE_KEY.to_string());
let call_info_stack = get_list(CALL_STACK_STATE_KEY.to_string(), forin_state);

Expand All @@ -211,7 +226,9 @@ fn pop_call_info_for_line(
StateValue::SubState(mut call_info_state) => {
match deserialize_call_info(&mut call_info_state) {
Some(call_info) => {
if call_info.meta_info.start == line || call_info.meta_info.end == line {
if (call_info.meta_info.start == line || call_info.meta_info.end == line)
&& call_info.line_context_name == line_context_name
{
Some(call_info)
} else if recursive {
pop_call_info_for_line(line, state, recursive)
Expand Down Expand Up @@ -320,10 +337,15 @@ impl Command for ForInCommand {
);

match forin_meta_info_result {
Ok(forin_meta_info) => CallInfo {
iteration: 0,
meta_info: forin_meta_info,
},
Ok(forin_meta_info) => {
let line_context_name = get_line_context_name(state);

CallInfo {
iteration: 0,
meta_info: forin_meta_info,
line_context_name,
}
}
Err(error) => return CommandResult::Crash(error.to_string()),
}
}
Expand All @@ -335,10 +357,13 @@ impl Command for ForInCommand {
let handle = &arguments[2];
match get_next_iteration(iteration, handle.to_string(), state) {
Some(next_value) => {
let line_context_name = get_line_context_name(state);

store_call_info(
&CallInfo {
iteration: iteration + 1,
meta_info: forin_meta_info,
line_context_name,
},
state,
);
Expand Down
28 changes: 27 additions & 1 deletion duckscript_sdk/src/sdk/std/flowcontrol/ifelse/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::sdk::std::flowcontrol::{end, forin, function, get_line_key, while_mod};
use crate::types::scope::get_line_context_name;
use crate::utils::state::{get_core_sub_state_for_command, get_list, get_sub_state};
use crate::utils::{condition, instruction_query, pckg};
use duckscript::types::command::{Command, CommandResult, Commands, GoToValue};
Expand Down Expand Up @@ -28,6 +29,7 @@ struct CallInfo {
pub(crate) passed: bool,
pub(crate) else_line_index: usize,
pub(crate) meta_info: IfElseMetaInfo,
pub(crate) line_context_name: String,
}

fn serialize_ifelse_meta_info(
Expand Down Expand Up @@ -105,6 +107,10 @@ fn serialize_call_info(call_info: &CallInfo, sub_state: &mut HashMap<String, Sta
"meta_info".to_string(),
StateValue::SubState(meta_info_state),
);
sub_state.insert(
"line_context_name".to_string(),
StateValue::String(call_info.line_context_name.clone()),
);
}

fn deserialize_call_info(sub_state: &mut HashMap<String, StateValue>) -> Option<CallInfo> {
Expand Down Expand Up @@ -139,12 +145,20 @@ fn deserialize_call_info(sub_state: &mut HashMap<String, StateValue>) -> Option<
},
None => return None,
};
let line_context_name = match sub_state.get("line_context_name") {
Some(value) => match value {
StateValue::String(line_context_name) => line_context_name.clone(),
_ => return None,
},
None => return None,
};

Some(CallInfo {
current,
passed,
else_line_index,
meta_info,
line_context_name,
})
}

Expand Down Expand Up @@ -263,6 +277,7 @@ fn pop_call_info_for_line(
line: usize,
state: &mut HashMap<String, StateValue>,
) -> Option<CallInfo> {
let line_context_name = get_line_context_name(state);
let if_state = get_core_sub_state_for_command(state, IFELSE_STATE_KEY.to_string());
let call_info_stack = get_list(CALL_STACK_STATE_KEY.to_string(), if_state);

Expand All @@ -271,7 +286,9 @@ fn pop_call_info_for_line(
StateValue::SubState(mut call_info_state) => {
match deserialize_call_info(&mut call_info_state) {
Some(call_info) => {
if call_info.current == line {
if call_info.current == line
&& call_info.line_context_name == line_context_name
{
Some(call_info)
} else {
pop_call_info_for_line(line, state)
Expand Down Expand Up @@ -365,11 +382,14 @@ impl Command for IfCommand {
if_else_info.else_lines[0]
};

let line_context_name = get_line_context_name(state);

let call_info = CallInfo {
current: next_line,
passed,
else_line_index: 0,
meta_info: if_else_info.clone(),
line_context_name,
};

store_call_info(&call_info, state);
Expand All @@ -381,11 +401,14 @@ impl Command for IfCommand {
} else {
let next_line = if_else_info.else_lines[0];

let line_context_name = get_line_context_name(state);

let call_info = CallInfo {
current: next_line,
passed: false,
else_line_index: 0,
meta_info: if_else_info.clone(),
line_context_name,
};

store_call_info(&call_info, state);
Expand Down Expand Up @@ -448,6 +471,7 @@ impl Command for ElseIfCommand {
CommandResult::GoTo(None, GoToValue::Line(next_line))
} else {
let if_else_info = call_info.meta_info.clone();
let line_context_name = get_line_context_name(state);
match condition::eval_condition(arguments, instructions, state, variables, commands) {
Ok(passed) => {
if passed {
Expand All @@ -462,6 +486,7 @@ impl Command for ElseIfCommand {
passed,
else_line_index: call_info.else_line_index,
meta_info: if_else_info,
line_context_name
};

store_call_info(&else_call_info, state);
Expand All @@ -476,6 +501,7 @@ impl Command for ElseIfCommand {
passed: false,
else_line_index: next_index,
meta_info: if_else_info,
line_context_name
};

store_call_info(&call_info, state);
Expand Down
26 changes: 24 additions & 2 deletions duckscript_sdk/src/sdk/std/flowcontrol/while_mod/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::sdk::std::flowcontrol::{end, forin, function, get_line_key, ifelse};
use crate::types::scope::get_line_context_name;
use crate::utils::state::{get_core_sub_state_for_command, get_list, get_sub_state};
use crate::utils::{condition, instruction_query, pckg};
use duckscript::types::command::{Command, CommandResult, Commands, GoToValue};
Expand All @@ -24,6 +25,7 @@ struct WhileMetaInfo {
#[derive(Debug)]
struct CallInfo {
pub(crate) meta_info: WhileMetaInfo,
pub(crate) line_context_name: String,
}

fn serialize_while_meta_info(
Expand Down Expand Up @@ -65,6 +67,10 @@ fn serialize_call_info(call_info: &CallInfo, sub_state: &mut HashMap<String, Sta
"meta_info".to_string(),
StateValue::SubState(meta_info_state),
);
sub_state.insert(
"line_context_name".to_string(),
StateValue::String(call_info.line_context_name.clone()),
);
}

fn deserialize_call_info(sub_state: &mut HashMap<String, StateValue>) -> Option<CallInfo> {
Expand All @@ -78,8 +84,18 @@ fn deserialize_call_info(sub_state: &mut HashMap<String, StateValue>) -> Option<
},
None => return None,
};
let line_context_name = match sub_state.get("line_context_name") {
Some(value) => match value {
StateValue::String(line_context_name) => line_context_name.clone(),
_ => return None,
},
None => return None,
};

Some(CallInfo { meta_info })
Some(CallInfo {
meta_info,
line_context_name,
})
}

fn create_while_meta_info_for_line(
Expand Down Expand Up @@ -184,6 +200,7 @@ fn pop_call_info_for_line(
line: usize,
state: &mut HashMap<String, StateValue>,
) -> Option<CallInfo> {
let line_context_name = get_line_context_name(state);
let while_state = get_core_sub_state_for_command(state, WHILE_STATE_KEY.to_string());
let call_info_stack = get_list(CALL_STACK_STATE_KEY.to_string(), while_state);

Expand All @@ -192,7 +209,9 @@ fn pop_call_info_for_line(
StateValue::SubState(mut call_info_state) => {
match deserialize_call_info(&mut call_info_state) {
Some(call_info) => {
if call_info.meta_info.end == line {
if call_info.meta_info.end == line
&& call_info.line_context_name == line_context_name
{
Some(call_info)
} else {
pop_call_info_for_line(line, state)
Expand Down Expand Up @@ -280,8 +299,11 @@ impl Command for WhileCommand {
) {
Ok(passed) => {
if passed {
let line_context_name = get_line_context_name(state);

let call_info = CallInfo {
meta_info: while_info.clone(),
line_context_name,
};

store_call_info(&call_info, state);
Expand Down

0 comments on commit 63b55e0

Please sign in to comment.