Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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" }
Expand Down
12 changes: 12 additions & 0 deletions ndc_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions ndc_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod compare;
pub mod hash_map;
pub mod int;
pub mod num;
27 changes: 13 additions & 14 deletions ndc_interpreter/src/num.rs → ndc_core/src/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Self, EvaluationError> {
/// Returns a `NumberConversionError` if you try to convert Inf, NaN, or Complex to an int
pub fn to_int_lossy(&self) -> Result<Self, NumberConversionError> {
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)
Expand Down Expand Up @@ -694,6 +689,10 @@ fn rational_to_complex(r: &BigRational) -> Complex<f64> {
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 {
Expand Down
4 changes: 1 addition & 3 deletions ndc_interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

6 changes: 2 additions & 4 deletions ndc_interpreter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down