Skip to content

Instantly share code, notes, and snippets.

@wareya
Created December 30, 2018 23:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save wareya/f04b689262fa92927728e95317cf8a9b to your computer and use it in GitHub Desktop.
////before
pub (crate) fn sim_BINOP(&mut self) -> StepResult
{
if self.stack_len() < 2
{
return Err(Some(format!("internal error: BINOP instruction requires 2 values on the stack but found {}", self.stack_len())));
}
let immediate = self.pull_single_from_code()?;
if let Some(right) = self.stack_pop_val()
{
if let Some(left) = self.stack_pop_val()
{
if let Some(opfunc) = get_binop_function(immediate)
{
match opfunc(&left, &right)
{
Ok(new_value) =>
{
self.stack_push_val(new_value);
}
Err(text) =>
{
if let (Some(left_fmt), Some(right_fmt)) = (format_val(&left), format_val(&right))
{
return Err(Some(format!("error: disallowed binary expression\n({})\n(value 1: {})\n(value 2: {})", text, left_fmt, right_fmt)));
}
else
{
return plainerr("internal error: error while trying to format error for disallowed binary expression");
}
}
}
}
else
{
return Err(Some(format!("internal error: unknown binary operation 0x{:02X}", immediate)));
}
}
else
{
return plainerr("internal error: not enough values on stack to run instruction BINOP (this error should be inaccessible!)");
}
}
else
{
return plainerr("internal error: not enough values on stack to run instruction BINOP (this error should be inaccessible!)");
}
Ok(())
}
////after
pub (crate) fn sim_BINOP(&mut self) -> StepResult
{
if self.stack_len() < 2
{
return Err(Some(format!("internal error: BINOP instruction requires 2 values on the stack but found {}", self.stack_len())));
}
let immediate = self.pull_single_from_code()?;
let right = self.stack_pop_val().ok_or_else(|| minierr("internal error: not enough values on stack to run instruction BINOP (this error should be inaccessible!)"))?;
let left = self.stack_pop_val().ok_or_else(|| minierr("internal error: not enough values on stack to run instruction BINOP (this error should be inaccessible!)"))?;
let opfunc = get_binop_function(immediate).ok_or_else(|| Some(format!("internal error: unknown binary operation 0x{:02X}", immediate)))?;
let new_value = opfunc(&left, &right).or_else(|text|
{
let left_fmt = format_val(&left).ok_or_else(|| minierr("internal error: failed to format left value for printing error when creating error for invalid binary expression"))?;
let right_fmt = format_val(&right).ok_or_else(|| minierr("internal error: failed to format right value for printing error when creating error for invalid binary expression"))?;
Err(Some(format!("error: disallowed binary expression\n({})\n(value 1: {})\n(value 2: {})", text, left_fmt, right_fmt)))
})?;
self.stack_push_val(new_value);
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment