comments, refactor

pull/21/head
Ondřej Hruška 4 years ago
parent 8f076e195a
commit 547beed847
Signed by: 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::fmt::{self, Display, Formatter};
use std::ops::Not; use std::ops::Not;
use crate::error::Error;
/// Condition flag
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum Cond { pub enum Cond {
/// Equality
Equal, Equal,
/// Inequality
NotEqual, NotEqual,
/// Equal to zero
Zero, Zero,
/// Not equal zero
NotZero, NotZero,
/// A < B
Lower, Lower,
/// A <= B
LowerOrEqual, LowerOrEqual,
/// A > B
Greater, Greater,
/// A >= B
GreaterOrEqual, GreaterOrEqual,
/// A > 0
Positive, Positive,
/// A <= 0
NonPositive, NonPositive,
/// A < 0
Negative, Negative,
/// A >= 0
NonNegative, NonNegative,
/// Arithmetic operation caused an integer operand to overflow
Overflow, Overflow,
/// No overflow
NotOverflow, NotOverflow,
/// Arithmetic (or other) operation was invalid.
/// Example: division by zero
Invalid, Invalid,
/// Operation was valid
Valid, Valid,
/// Arithmetic carry
Carry, Carry,
/// Carry not set
NotCarry, 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 { impl Display for Cond {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(match self { f.write_str(match self {
@ -43,7 +90,7 @@ impl Display for Cond {
Cond::Carry => "c", Cond::Carry => "c",
Cond::NotCarry => "nc", Cond::NotCarry => "nc",
Cond::Invalid => "inval", Cond::Invalid => "inval",
Cond::Valid => "ok", Cond::Valid => "valid",
}) })
} }
} }

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

@ -1,7 +1,7 @@
use sexp::Sexp; use sexp::Sexp;
use crate::error::Error; 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::parse_instr::parse_instructions;
use crate::parse::sexp_expect::{expect_list, expect_string_atom}; use crate::parse::sexp_expect::{expect_list, expect_string_atom};
use crate::patches::TryRemove; 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())); 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::data::literal::{Label, RoutineName};
use crate::error::Error; use crate::error::Error;
use crate::instr::{HLOp, Op}; 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::parse_data::{parse_label, parse_rd, parse_wr};
use crate::parse::sexp_expect::expect_string_atom; use crate::parse::sexp_expect::expect_string_atom;

Loading…
Cancel
Save