Skip to content
Snippets Groups Projects
Commit e656f1ab authored by stratact's avatar stratact Committed by Michael Aaron Murphy
Browse files

Fix assignments errors when there are too many value or keys. Also store the...

Fix assignments errors when there are too many value or keys. Also store the exit status of `let` and `export`
parent 83ed9cbd
No related branches found
No related tags found
1 merge request!802Fix assignments errors for let and store exit status properly
......@@ -6,6 +6,8 @@ pub(crate) enum AssignmentError<'a> {
InvalidOperator(&'a str),
InvalidValue(Primitive, Primitive),
TypeError(TypeError<'a>),
ExtraValues(&'a str, &'a str),
ExtraKeys(&'a str, &'a str),
}
impl<'a> Display for AssignmentError<'a> {
......@@ -16,6 +18,10 @@ impl<'a> Display for AssignmentError<'a> {
write!(f, "expected {}, but received {}", expected, actual)
}
AssignmentError::TypeError(ref type_err) => write!(f, "{}", type_err),
AssignmentError::ExtraValues(ref prevkey, ref prevval) => write!(f, "extra values were supplied, and thus ignored. \
Previous assignment: '{}' = '{}'", prevkey, prevval),
AssignmentError::ExtraKeys(ref prevkey, ref prevval) => write!(f, "extra keys were supplied, and thus ignored. \
Previous assignment: '{}' = '{}'", prevkey, prevval),
}
}
}
......@@ -50,27 +56,26 @@ impl<'a> Iterator for AssignmentActions<'a> {
type Item = Result<Action<'a>, AssignmentError<'a>>;
fn next(&mut self) -> Option<Result<Action<'a>, AssignmentError<'a>>> {
if let Some(key) = self.keys.next() {
match key {
Ok(key) => match self.values.next() {
Some(value) => {
let next_key = self.keys.next();
let next_value = self.values.next();
match (next_key, next_value) {
(Some(key), Some(value)) => {
match key {
Ok(key) => {
self.prevkey = key.name;
self.prevval = value;
Some(Action::new(key, self.operator, value))
}
None => None,
},
Err(why) => Some(Err(AssignmentError::TypeError(why))),
Err(why) => Some(Err(AssignmentError::TypeError(why))),
}
}
(None, Some(_)) => {
Some(Err(AssignmentError::ExtraValues(self.prevkey, self.prevval)))
}
} else {
if let Some(_) = self.values.next() {
eprintln!(
"ion: extra values were supplied, and thus ignored. Previous assignment: '{}' \
= '{}'",
self.prevkey, self.prevval
);
(Some(_), None) => {
Some(Err(AssignmentError::ExtraKeys(self.prevkey, self.prevval)))
}
None
_ => None,
}
}
}
......
......@@ -85,9 +85,11 @@ impl FlowLogic for Shell {
// Execute a Let Statement
Statement::Let(action) => {
self.previous_status = self.local(action);
self.variables.set_var("?", &self.previous_status.to_string());
}
Statement::Export(action) => {
self.previous_status = self.export(action);
self.variables.set_var("?", &self.previous_status.to_string());
}
// Collect the statements for the while loop, and if the loop is complete,
// execute the while loop with the provided expression.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment