From 547beed847d525b46ea7e03950b032bce48a06d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Fri, 25 Sep 2020 23:02:54 +0200 Subject: [PATCH] comments, refactor --- Cargo.lock | 94 ++++++++++++++++++------------------- asm/src/instr/cond.rs | 49 ++++++++++++++++++- asm/src/instr/mod.rs | 2 +- asm/src/parse/parse_cond.rs | 27 +---------- asm/src/parse/parse_op.rs | 2 +- 5 files changed, 99 insertions(+), 75 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 73c2b3b..89a1407 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,9 +10,9 @@ checksum = "6b602bfe940d21c130f3895acd65221e8a61270debe89d628b9cb4e3ccb8569b" name = "asm" version = "0.1.0" dependencies = [ - "anyhow", - "sexp", - "thiserror", + "anyhow", + "sexp", + "thiserror", ] [[package]] @@ -21,9 +21,9 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ - "hermit-abi", - "libc", - "winapi", + "hermit-abi", + "libc", + "winapi", ] [[package]] @@ -44,9 +44,9 @@ version = "0.4.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "942f72db697d8767c22d46a598e01f2d3b475501ea43d0db4f16d90259182d0b" dependencies = [ - "num-integer", - "num-traits", - "time", + "num-integer", + "num-traits", + "time", ] [[package]] @@ -55,21 +55,21 @@ version = "1.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f4ffc801dacf156c5854b9df4f425a626539c3a6ef7893cc0c5084a23f0b6c59" dependencies = [ - "atty", - "lazy_static", - "winapi", + "atty", + "lazy_static", + "winapi", ] [[package]] name = "crsn" version = "0.1.0" dependencies = [ - "anyhow", - "asm", - "log", - "runtime", - "simple_logger", - "thiserror", + "anyhow", + "asm", + "log", + "runtime", + "simple_logger", + "thiserror", ] [[package]] @@ -78,7 +78,7 @@ version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c30f6d0bc6b00693347368a67d41b58f2fb851215ff1da49e90fe2c5c667151" dependencies = [ - "libc", + "libc", ] [[package]] @@ -99,7 +99,7 @@ version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" dependencies = [ - "cfg-if", + "cfg-if", ] [[package]] @@ -108,8 +108,8 @@ version = "0.1.43" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b" dependencies = [ - "autocfg", - "num-traits", + "autocfg", + "num-traits", ] [[package]] @@ -118,7 +118,7 @@ version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611" dependencies = [ - "autocfg", + "autocfg", ] [[package]] @@ -127,7 +127,7 @@ version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "36e28516df94f3dd551a587da5357459d9b36d945a7c37c3557928c1c2ff2a2c" dependencies = [ - "unicode-xid", + "unicode-xid", ] [[package]] @@ -136,18 +136,18 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" dependencies = [ - "proc-macro2", + "proc-macro2", ] [[package]] name = "runtime" version = "0.1.0" dependencies = [ - "anyhow", - "asm", - "log", - "num-traits", - "thiserror", + "anyhow", + "asm", + "log", + "num-traits", + "thiserror", ] [[package]] @@ -162,11 +162,11 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13a53ed2efd04911c8280f2da7bf9abd350c931b86bc7f9f2386fbafbf525ff9" dependencies = [ - "atty", - "chrono", - "colored", - "log", - "winapi", + "atty", + "chrono", + "colored", + "log", + "winapi", ] [[package]] @@ -175,9 +175,9 @@ version = "1.0.41" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6690e3e9f692504b941dc6c3b188fd28df054f7fb8469ab40680df52fdcc842b" dependencies = [ - "proc-macro2", - "quote", - "unicode-xid", + "proc-macro2", + "quote", + "unicode-xid", ] [[package]] @@ -186,7 +186,7 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dfdd070ccd8ccb78f4ad66bf1982dc37f620ef696c6b5028fe2ed83dd3d0d08" dependencies = [ - "thiserror-impl", + "thiserror-impl", ] [[package]] @@ -195,9 +195,9 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd80fc12f73063ac132ac92aceea36734f04a1d93c1240c6944e23a3b8841793" dependencies = [ - "proc-macro2", - "quote", - "syn", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -206,9 +206,9 @@ version = "0.1.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" dependencies = [ - "libc", - "wasi", - "winapi", + "libc", + "wasi", + "winapi", ] [[package]] @@ -229,8 +229,8 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", ] [[package]] diff --git a/asm/src/instr/cond.rs b/asm/src/instr/cond.rs index 161ce0e..5e4cae9 100644 --- a/asm/src/instr/cond.rs +++ b/asm/src/instr/cond.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 { + 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", }) } } diff --git a/asm/src/instr/mod.rs b/asm/src/instr/mod.rs index 6c5d581..66c9db5 100644 --- a/asm/src/instr/mod.rs +++ b/asm/src/instr/mod.rs @@ -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 diff --git a/asm/src/parse/parse_cond.rs b/asm/src/parse/parse_cond.rs index 93fc262..b505308 100644 --- a/asm/src/parse/parse_cond.rs +++ b/asm/src/parse/parse_cond.rs @@ -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), 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 { - 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())); - } - }) -} diff --git a/asm/src/parse/parse_op.rs b/asm/src/parse/parse_op.rs index 4015a89..e7e7d7d 100644 --- a/asm/src/parse/parse_op.rs +++ b/asm/src/parse/parse_op.rs @@ -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;