diff --git a/Cargo.lock b/Cargo.lock index 39da850..f24b433 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1261,19 +1261,29 @@ dependencies = [ ] [[package]] -name = "ndc_interpreter" +name = "ndc_core" version = "0.2.1" dependencies = [ "ahash", + "ndc_parser", + "num", + "ordered-float", + "ryu", + "thiserror", +] + +[[package]] +name = "ndc_interpreter" +version = "0.2.1" +dependencies = [ "anyhow", "derive_builder", "derive_more", "itertools 0.14.0", + "ndc_core", "ndc_lexer", "ndc_parser", "num", - "ordered-float", - "ryu", "self_cell", "thiserror", ] diff --git a/Cargo.toml b/Cargo.toml index d22d3ec..c0b4146 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] resolver = "3" -members = ["ndc_macros", "ndc_bin", "ndc_interpreter", "ndc_lsp", "ndc_lexer", "ndc_parser", "ndc_stdlib", "benches", "tests"] +members = ["ndc_macros", "ndc_bin", "ndc_core", "ndc_interpreter", "ndc_lsp", "ndc_lexer", "ndc_parser", "ndc_stdlib", "benches", "tests"] [workspace.package] edition = "2024" @@ -17,6 +17,7 @@ derive_builder = { version = "0.20.2" } derive_more = { version = "2.1.1", features = ["deref", "deref_mut", "from", "display", "constructor"] } factorial = "0.4.0" itertools = "0.14.0" +ndc_core = { path = "ndc_core" } ndc_lexer = { path = "ndc_lexer" } ndc_interpreter = { path = "ndc_interpreter" } ndc_parser = { path = "ndc_parser" } diff --git a/ndc_core/Cargo.toml b/ndc_core/Cargo.toml new file mode 100644 index 0000000..0de4971 --- /dev/null +++ b/ndc_core/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "ndc_core" +edition.workspace = true +version.workspace = true + +[dependencies] +ahash.workspace = true +ndc_parser.workspace = true +num.workspace = true +ordered-float.workspace = true +ryu.workspace = true +thiserror.workspace = true diff --git a/ndc_interpreter/src/compare.rs b/ndc_core/src/compare.rs similarity index 100% rename from ndc_interpreter/src/compare.rs rename to ndc_core/src/compare.rs diff --git a/ndc_interpreter/src/hash_map.rs b/ndc_core/src/hash_map.rs similarity index 100% rename from ndc_interpreter/src/hash_map.rs rename to ndc_core/src/hash_map.rs diff --git a/ndc_interpreter/src/int.rs b/ndc_core/src/int.rs similarity index 100% rename from ndc_interpreter/src/int.rs rename to ndc_core/src/int.rs diff --git a/ndc_core/src/lib.rs b/ndc_core/src/lib.rs new file mode 100644 index 0000000..e99a7fd --- /dev/null +++ b/ndc_core/src/lib.rs @@ -0,0 +1,4 @@ +pub mod compare; +pub mod hash_map; +pub mod int; +pub mod num; diff --git a/ndc_interpreter/src/num.rs b/ndc_core/src/num.rs similarity index 96% rename from ndc_interpreter/src/num.rs rename to ndc_core/src/num.rs index 798110c..355881f 100644 --- a/ndc_interpreter/src/num.rs +++ b/ndc_core/src/num.rs @@ -4,11 +4,8 @@ use std::hash::{Hash, Hasher}; use std::num::TryFromIntError; use std::ops::{Add, Div, Mul, Neg, Not, Rem, Sub}; -use crate::evaluate::EvaluationError; -use crate::function::StaticType; use crate::int::Int; -use ndc_lexer::Span; -use ndc_parser::BinaryOperator; +use ndc_parser::{BinaryOperator, StaticType}; use num::bigint::TryFromBigIntError; use num::complex::{Complex64, ComplexFloat}; use num::{BigInt, BigRational, Complex, FromPrimitive, Signed, ToPrimitive, Zero}; @@ -474,26 +471,24 @@ impl Number { } /// # Errors - /// Returns an `EvaluationError` (for now) if you try to convert Inf or NaN to an int - pub fn to_int_lossy(&self) -> Result { + /// Returns a `NumberConversionError` if you try to convert Inf, NaN, or Complex to an int + pub fn to_int_lossy(&self) -> Result { let n = match self { Self::Int(i) => Self::Int(i.clone()), Self::Float(f) => { if let Some(bi) = BigInt::from_f64(*f) { Self::Int(Int::BigInt(bi).simplified()) } else { - return Err(EvaluationError::type_error( - format!("cannot convert {f} to int"), - Span::new(0, 0), // TODO: fix span creation (move out of this impl) - )); + return Err(NumberConversionError(format!( + "cannot convert {f} to int" + ))); } } Self::Rational(r) => Self::Int(Int::BigInt(r.to_integer()).simplified()), Self::Complex(c) => { - return Err(EvaluationError::type_error( - format!("cannot convert complex number {c} to int"), - Span::new(0, 0), // TODO: fix span creation (move out of this impl) - )); + return Err(NumberConversionError(format!( + "cannot convert complex number {c} to int" + ))); } }; Ok(n) @@ -694,6 +689,10 @@ fn rational_to_complex(r: &BigRational) -> Complex { Complex::from(r.to_f64().unwrap_or(f64::NAN)) } +#[derive(thiserror::Error, Debug)] +#[error("{0}")] +pub struct NumberConversionError(String); + #[derive(Debug, Clone, Copy, Eq, PartialEq)] #[deprecated = "use static type instead?"] pub enum NumberType { diff --git a/ndc_interpreter/Cargo.toml b/ndc_interpreter/Cargo.toml index 2810e5e..1420330 100644 --- a/ndc_interpreter/Cargo.toml +++ b/ndc_interpreter/Cargo.toml @@ -5,16 +5,14 @@ edition.workspace = true version.workspace = true [dependencies] -ahash.workspace = true anyhow.workspace = true derive_more.workspace = true derive_builder.workspace = true itertools.workspace = true +ndc_core.workspace = true ndc_lexer.workspace = true ndc_parser.workspace = true num.workspace = true -ordered-float.workspace = true -ryu.workspace = true self_cell.workspace = true thiserror.workspace = true diff --git a/ndc_interpreter/src/lib.rs b/ndc_interpreter/src/lib.rs index 4328ab2..c13018a 100644 --- a/ndc_interpreter/src/lib.rs +++ b/ndc_interpreter/src/lib.rs @@ -1,12 +1,10 @@ -pub mod compare; -pub mod hash_map; +pub use ndc_core::{compare, hash_map, int, num}; + pub mod environment; pub mod evaluate; pub mod function; pub mod heap; -pub mod int; pub mod iterator; -pub mod num; pub mod semantic; pub mod sequence; pub mod value;