comments, refactor

floats
Ondřej Hruška 4 years ago
parent 8f076e195a
commit 547beed847
Signed by untrusted user: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 49
      asm/src/instr/cond.rs
  2. 2
      asm/src/instr/mod.rs
  3. 27
      asm/src/parse/parse_cond.rs
  4. 2
      asm/src/parse/parse_op.rs

@ -1,28 +1,75 @@
use std::fmt::{self, Display, Formatter};
use std::ops::Not;
use crate::error::Error;
/// Condition flag
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum Cond {
/// Equality
Equal,
/// Inequality
NotEqual,
/// Equal to zero
Zero,
/// Not equal zero
NotZero,
/// A < B
Lower,
/// A <= B
LowerOrEqual,
/// A > B
Greater,
/// A >= B
GreaterOrEqual,
/// A > 0
Positive,
/// A <= 0
NonPositive,
/// A < 0
Negative,
/// A >= 0
NonNegative,
/// Arithmetic operation caused an integer operand to overflow
Overflow,
/// No overflow
NotOverflow,
/// Arithmetic (or other) operation was invalid.
/// Example: division by zero
Invalid,
/// Operation was valid
Valid,
/// Arithmetic carry
Carry,
/// Carry not set
NotCarry,
}
pub fn parse_cond(text: &str) -> Result<Cond, Error> {
Ok(match text.trim_end_matches('?') {
"eq" | "=" | "==" => Cond::Equal,
"ne" | "<>" | "!=" | "≠" => Cond::NotEqual,
"z" | "0" => Cond::Zero,
"nz" | "<>0" | "!0" => Cond::NotZero,
"lt" | "<" => Cond::Lower,
"le" | "<=" | "≤" => Cond::LowerOrEqual,
"gt" | ">" => Cond::Greater,
"ge" | ">=" | "≥" => Cond::GreaterOrEqual,
"pos" | "+" | ">0" => Cond::Positive,
"neg" | "-" | "<0" => Cond::Negative,
"npos" | "!+" | "0-" | "<=0" | "≥0" => Cond::NonPositive,
"nneg" | "!-" | "0+" | ">=0" | "≤0" => Cond::NonNegative,
"c" => Cond::Carry,
"nc" | "!c" => Cond::NotCarry,
"valid" => Cond::Valid,
"inval" => Cond::Invalid,
"ov" | "^" => Cond::Overflow,
"nov" | "!ov" | "!^" => Cond::NotOverflow,
_ => {
return Err(Error::Parse(format!("Unknown cond: {}", text).into()));
}
})
}
impl Display for Cond {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(match self {
@ -43,7 +90,7 @@ impl Display for Cond {
Cond::Carry => "c",
Cond::NotCarry => "nc",
Cond::Invalid => "inval",
Cond::Valid => "ok",
Cond::Valid => "valid",
})
}
}

@ -7,7 +7,7 @@ pub use op::Op;
use crate::data::literal::RoutineName;
mod op;
mod cond;
pub mod cond;
mod flatten;
/// A higher-level instruction

@ -1,7 +1,7 @@
use sexp::Sexp;
use crate::error::Error;
use crate::instr::{Cond, Instr};
use crate::instr::{Cond, Instr, cond};
use crate::parse::parse_instr::parse_instructions;
use crate::parse::sexp_expect::{expect_list, expect_string_atom};
use crate::patches::TryRemove;
@ -14,29 +14,6 @@ pub fn parse_cond_branch(tok: Sexp) -> Result<(Cond, Vec<Instr>), Error> {
return Err(Error::Parse(format!("Condition must end with '?': {}", kw).into()));
}
Ok((parse_cond(&kw)?, parse_instructions(list)?))
Ok((cond::parse_cond(&kw)?, parse_instructions(list)?))
}
pub fn parse_cond(text: &str) -> Result<Cond, Error> {
Ok(match text.trim_end_matches('?') {
"eq" | "=" | "==" => Cond::Equal,
"ne" | "<>" | "!=" | "≠" => Cond::NotEqual,
"z" | "0" => Cond::Zero,
"nz" | "<>0" | "!0" => Cond::NotZero,
"lt" | "<" => Cond::Lower,
"le" | "<=" | "≤" => Cond::LowerOrEqual,
"gt" | ">" => Cond::Greater,
"ge" | ">=" | "≥" => Cond::GreaterOrEqual,
"pos" | "+" | ">0" => Cond::Positive,
"neg" | "-" | "<0" => Cond::Negative,
"npos" | "!+" | "0-" | "<=0" | "≥0" => Cond::NonPositive,
"nneg" | "!-" | "0+" | ">=0" | "≤0" => Cond::NonNegative,
"ov" | "^" => Cond::Overflow,
"c" => Cond::Carry,
"nc" | "!c" => Cond::NotCarry,
"nov" | "!ov" | "!^" => Cond::NotOverflow,
_ => {
return Err(Error::Parse(format!("Unknown cond: {}", text).into()));
}
})
}

@ -4,7 +4,7 @@ use crate::data::{Rd, Wr};
use crate::data::literal::{Label, RoutineName};
use crate::error::Error;
use crate::instr::{HLOp, Op};
use crate::parse::parse_cond::parse_cond;
use crate::instr::cond::parse_cond;
use crate::parse::parse_data::{parse_label, parse_rd, parse_wr};
use crate::parse::sexp_expect::expect_string_atom;

Loading…
Cancel
Save