fix: support up to 128-bit integers

Floats should be next
This commit is contained in:
Roman Godmaire 2024-05-06 08:28:46 -04:00
parent cbdff2c170
commit 1ed451e696
2 changed files with 38 additions and 4 deletions

40
src/env/core.rs vendored
View file

@ -93,7 +93,15 @@ pub(super) fn core() -> HashMap<String, Node> {
let res = args
.into_iter()
.reduce(|lhs, rhs| match (lhs, rhs) {
(Node::Int(lhs), Node::Int(rhs)) => Node::Int(lhs + rhs),
(Node::Int(lhs), Node::Int(rhs)) => match lhs.checked_add(rhs) {
Some(val) => Node::Int(val),
None => Node::Int128(lhs as i128 + rhs as i128),
},
(Node::Int128(lhs), Node::Int128(rhs)) => Node::Int128(lhs + rhs),
(Node::Int(lhs), Node::Int128(rhs)) => {
Node::Int128(lhs as i128 + rhs as i128)
}
(Node::Int128(lhs), Node::Int(rhs)) => Node::Int128(lhs + rhs as i128),
_ => todo!(),
})
.unwrap_or(Node::Int(0));
@ -107,7 +115,15 @@ pub(super) fn core() -> HashMap<String, Node> {
let res = args
.into_iter()
.reduce(|lhs, rhs| match (lhs, rhs) {
(Node::Int(lhs), Node::Int(rhs)) => Node::Int(lhs - rhs),
(Node::Int(lhs), Node::Int(rhs)) => match lhs.checked_sub(rhs) {
Some(val) => Node::Int(val),
None => Node::Int128(lhs as i128 - rhs as i128),
},
(Node::Int128(lhs), Node::Int128(rhs)) => Node::Int128(lhs - rhs),
(Node::Int(lhs), Node::Int128(rhs)) => {
Node::Int128(lhs as i128 - rhs as i128)
}
(Node::Int128(lhs), Node::Int(rhs)) => Node::Int128(lhs - rhs as i128),
_ => todo!(),
})
.unwrap_or(Node::Int(0));
@ -121,7 +137,15 @@ pub(super) fn core() -> HashMap<String, Node> {
let res = args
.into_iter()
.reduce(|lhs, rhs| match (lhs, rhs) {
(Node::Int(lhs), Node::Int(rhs)) => Node::Int(lhs * rhs),
(Node::Int(lhs), Node::Int(rhs)) => match lhs.checked_mul(rhs) {
Some(val) => Node::Int(val),
None => Node::Int128(lhs as i128 * rhs as i128),
},
(Node::Int128(lhs), Node::Int128(rhs)) => Node::Int128(lhs * rhs),
(Node::Int(lhs), Node::Int128(rhs)) => {
Node::Int128(lhs as i128 * rhs as i128)
}
(Node::Int128(lhs), Node::Int(rhs)) => Node::Int128(lhs * rhs as i128),
_ => todo!(),
})
.unwrap_or(Node::Int(0));
@ -135,7 +159,15 @@ pub(super) fn core() -> HashMap<String, Node> {
let res = args
.into_iter()
.reduce(|lhs, rhs| match (lhs, rhs) {
(Node::Int(lhs), Node::Int(rhs)) => Node::Int(lhs / rhs),
(Node::Int(lhs), Node::Int(rhs)) => match lhs.checked_div(rhs) {
Some(val) => Node::Int(val),
None => Node::Int128(lhs as i128 / rhs as i128),
},
(Node::Int128(lhs), Node::Int128(rhs)) => Node::Int128(lhs / rhs),
(Node::Int(lhs), Node::Int128(rhs)) => {
Node::Int128(lhs as i128 / rhs as i128)
}
(Node::Int128(lhs), Node::Int(rhs)) => Node::Int128(lhs / rhs as i128),
_ => todo!(),
})
.unwrap_or(Node::Int(0));

View file

@ -11,6 +11,7 @@ pub enum Node {
Symbol(String),
Keyword(String),
Int(i64),
Int128(i128),
String(String),
Boolean(bool),
Nil,
@ -42,6 +43,7 @@ impl std::fmt::Display for Node {
}
Node::Int(val) => write!(f, "{}", val),
Node::Int128(val) => write!(f, "{}", val),
Node::Boolean(true) => write!(f, "true"),
Node::Boolean(false) => write!(f, "false"),
Node::Symbol(val) => write!(f, "{}", val),