From 790878f2094fd09aea168fcaa5a256daf1f0686e Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 25 Aug 2020 09:27:47 +0800 Subject: [PATCH 1/3] Add missing operators for f32. --- src/fn_call.rs | 8 +++--- src/packages/arithmetic.rs | 51 ++++++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/fn_call.rs b/src/fn_call.rs index 494fe1a0..5186aa0c 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -1146,7 +1146,7 @@ pub fn run_builtin_binary_op( "*" => return Ok(Some((x * y).into())), "/" => return Ok(Some((x / y).into())), "%" => return Ok(Some((x % y).into())), - "~" => return pow_i_i_u(x, y).map(Into::into).map(Some), + "~" => return Ok(Some(x.pow(y as u32).into())), ">>" => return shr_u(x, y).map(Into::into).map(Some), "<<" => return shl_u(x, y).map(Into::into).map(Some), _ => (), @@ -1223,7 +1223,7 @@ pub fn run_builtin_binary_op( "*" => return Ok(Some((x * y).into())), "/" => return Ok(Some((x / y).into())), "%" => return Ok(Some((x % y).into())), - "~" => return pow_f_f(x, y).map(Into::into).map(Some), + "~" => return Ok(Some(x.powf(y).into())), "==" => return Ok(Some((x == y).into())), "!=" => return Ok(Some((x != y).into())), ">" => return Ok(Some((x > y).into())), @@ -1274,7 +1274,7 @@ pub fn run_builtin_op_assignment( "*=" => return Ok(Some(*x *= y)), "/=" => return Ok(Some(*x /= y)), "%=" => return Ok(Some(*x %= y)), - "~=" => return Ok(Some(*x = pow_i_i_u(*x, y)?)), + "~=" => return Ok(Some(*x = (*x).pow(y as u32))), ">>=" => return Ok(Some(*x = shr_u(*x, y)?)), "<<=" => return Ok(Some(*x = shl_u(*x, y)?)), _ => (), @@ -1317,7 +1317,7 @@ pub fn run_builtin_op_assignment( "*=" => return Ok(Some(*x *= y)), "/=" => return Ok(Some(*x /= y)), "%=" => return Ok(Some(*x %= y)), - "~=" => return Ok(Some(*x = pow_f_f(*x, y)?)), + "~=" => return Ok(Some(*x = (*x).powf(y))), _ => (), } } diff --git a/src/packages/arithmetic.rs b/src/packages/arithmetic.rs index f40f1d11..5c95a059 100644 --- a/src/packages/arithmetic.rs +++ b/src/packages/arithmetic.rs @@ -4,9 +4,6 @@ use crate::parser::INT; use crate::{result::EvalAltResult, token::Position}; -#[cfg(not(feature = "no_float"))] -use crate::parser::FLOAT; - use num_traits::{ identities::Zero, CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl, CheckedShr, CheckedSub, @@ -238,18 +235,28 @@ pub fn pow_i_i(x: INT, y: INT) -> FuncReturn { } } } -// Unchecked integer power - may panic on overflow or if the power index is too high (> u32::MAX) -pub fn pow_i_i_u(x: INT, y: INT) -> FuncReturn { - Ok(x.pow(y as u32)) -} // Floating-point power - always well-defined #[cfg(not(feature = "no_float"))] -pub fn pow_f_f(x: FLOAT, y: FLOAT) -> FuncReturn { +pub fn pow_f_f_32(x: f32, y: f32) -> FuncReturn { Ok(x.powf(y)) } // Checked power #[cfg(not(feature = "no_float"))] -pub fn pow_f_i(x: FLOAT, y: INT) -> FuncReturn { +pub fn pow_f_i_32(x: f32, y: INT) -> FuncReturn { + // Raise to power that is larger than an i32 + if y > (i32::MAX as INT) { + return EvalAltResult::ErrorArithmetic( + format!("Number raised to too large an index: {} ~ {}", x, y), + Position::none(), + ) + .into(); + } + + Ok(x.powi(y as i32)) +} +// Checked power +#[cfg(not(feature = "no_float"))] +pub fn pow_f_i_64(x: f64, y: INT) -> FuncReturn { // Raise to power that is larger than an i32 if y > (i32::MAX as INT) { return EvalAltResult::ErrorArithmetic( @@ -263,7 +270,12 @@ pub fn pow_f_i(x: FLOAT, y: INT) -> FuncReturn { } // Unchecked power - may be incorrect if the power index is too high (> i32::MAX) #[cfg(not(feature = "no_float"))] -pub fn pow_f_i_u(x: FLOAT, y: INT) -> FuncReturn { +pub fn pow_f_i_u_32(x: f32, y: INT) -> FuncReturn { + Ok(x.powi(y as i32)) +} +// Unchecked power - may be incorrect if the power index is too high (> i32::MAX) +#[cfg(not(feature = "no_float"))] +pub fn pow_f_i_u_64(x: f64, y: INT) -> FuncReturn { Ok(x.powi(y as i32)) } @@ -352,6 +364,7 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { reg_op!(lib, "-", sub_u, f32); reg_op!(lib, "*", mul_u, f32); reg_op!(lib, "/", div_u, f32); + reg_op!(lib, "%", modulo_u, f32); reg_sign!(lib, "sign", f32, f32); reg_sign!(lib, "sign", f64, f64); } @@ -370,15 +383,17 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { #[cfg(not(feature = "no_float"))] { - // Checked power - if cfg!(not(feature = "unchecked")) { - lib.set_fn_2("~", pow_f_i); - } else { - lib.set_fn_2("~", pow_f_i_u); - } + // Power + lib.set_fn_2("~", pow_f_f_32); - // Floating-point modulo and power - reg_op!(lib, "%", modulo_u, f32); + // Checked float raised to integer power + if cfg!(not(feature = "unchecked")) { + lib.set_fn_2("~", pow_f_i_32); + lib.set_fn_2("~", pow_f_i_64); + } else { + lib.set_fn_2("~", pow_f_i_u_32); + lib.set_fn_2("~", pow_f_i_u_64); + } // Floating-point unary reg_unary!(lib, "-", neg_u, f32, f64); From 402c85ca65ae00a01f90989a765d0734ae47edfe Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 25 Aug 2020 09:41:52 +0800 Subject: [PATCH 2/3] Fix no_std build. --- src/fn_call.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/fn_call.rs b/src/fn_call.rs index 5186aa0c..f22b89bc 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -52,6 +52,10 @@ use crate::stdlib::{ #[cfg(not(feature = "no_function"))] use crate::stdlib::{collections::HashSet, string::String}; +#[cfg(feature = "no_std")] +#[cfg(not(feature = "no_float"))] +use num_traits::float::Float; + /// Extract the property name from a getter function name. #[inline(always)] fn extract_prop_from_getter(_fn_name: &str) -> Option<&str> { @@ -1147,8 +1151,8 @@ pub fn run_builtin_binary_op( "/" => return Ok(Some((x / y).into())), "%" => return Ok(Some((x % y).into())), "~" => return Ok(Some(x.pow(y as u32).into())), - ">>" => return shr_u(x, y).map(Into::into).map(Some), - "<<" => return shl_u(x, y).map(Into::into).map(Some), + ">>" => return Ok(Some((x >> y).into())), + "<<" => return Ok(Some((x << y).into())), _ => (), } } @@ -1274,9 +1278,9 @@ pub fn run_builtin_op_assignment( "*=" => return Ok(Some(*x *= y)), "/=" => return Ok(Some(*x /= y)), "%=" => return Ok(Some(*x %= y)), - "~=" => return Ok(Some(*x = (*x).pow(y as u32))), - ">>=" => return Ok(Some(*x = shr_u(*x, y)?)), - "<<=" => return Ok(Some(*x = shl_u(*x, y)?)), + "~=" => return Ok(Some(*x = x.pow(y as u32))), + ">>=" => return Ok(Some(*x = *x >> y)), + "<<=" => return Ok(Some(*x = *x << y)), _ => (), } } @@ -1317,7 +1321,7 @@ pub fn run_builtin_op_assignment( "*=" => return Ok(Some(*x *= y)), "/=" => return Ok(Some(*x /= y)), "%=" => return Ok(Some(*x %= y)), - "~=" => return Ok(Some(*x = (*x).powf(y))), + "~=" => return Ok(Some(*x = x.powf(y))), _ => (), } } From cd867b180ffd3566235d52646717207fbd79f173 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 30 Aug 2020 17:25:36 +0800 Subject: [PATCH 3/3] Restructure book chapters. --- doc/src/SUMMARY.md | 10 +++++----- doc/src/{rust => engine}/options.md | 0 doc/src/{rust => engine}/scope.md | 0 doc/src/links.md | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) rename doc/src/{rust => engine}/options.md (100%) rename doc/src/{rust => engine}/scope.md (100%) diff --git a/doc/src/SUMMARY.md b/doc/src/SUMMARY.md index 59584d28..f897b61e 100644 --- a/doc/src/SUMMARY.md +++ b/doc/src/SUMMARY.md @@ -26,15 +26,14 @@ The Rhai Scripting Language 4. [Create a Rust Closure from a Rhai Function](engine/func.md) 5. [Evaluate Expressions Only](engine/expressions.md) 6. [Raw Engine](engine/raw.md) + 7. [Scope - Initializing and Maintaining State](engine/scope.md) + 8. [Engine Configuration Options](engine/options.md) 4. [Extend Rhai with Rust](rust/index.md) 1. [Traits](rust/traits.md) 2. [Register a Rust Function](rust/functions.md) 1. [String Parameters in Rust Functions](rust/strings.md) 3. [Register a Generic Rust Function](rust/generic.md) 4. [Register a Fallible Rust Function](rust/fallible.md) - 5. [Packages](rust/packages/index.md) - 1. [Built-in Packages](rust/packages/builtin.md) - 2. [Create a Custom Package](rust/packages/create.md) 6. [Override a Built-in Function](rust/override.md) 7. [Operator Overloading](rust/operators.md) 8. [Register a Custom Type and its Methods](rust/custom.md) @@ -42,8 +41,9 @@ The Rhai Scripting Language 2. [Indexers](rust/indexers.md) 3. [Disable Custom Types](rust/disable-custom.md) 4. [Printing Custom Types](rust/print-custom.md) - 9. [Scope - Initializing and Maintaining State](rust/scope.md) - 10. [Engine Configuration Options](rust/options.md) + 9. [Packages](rust/packages/index.md) + 1. [Built-in Packages](rust/packages/builtin.md) + 2. [Create a Custom Package](rust/packages/create.md) 5. [Rhai Language Reference](language/index.md) 1. [Comments](language/comments.md) 2. [Values and Types](language/values-and-types.md) diff --git a/doc/src/rust/options.md b/doc/src/engine/options.md similarity index 100% rename from doc/src/rust/options.md rename to doc/src/engine/options.md diff --git a/doc/src/rust/scope.md b/doc/src/engine/scope.md similarity index 100% rename from doc/src/rust/scope.md rename to doc/src/engine/scope.md diff --git a/doc/src/links.md b/doc/src/links.md index 953c1164..e030478a 100644 --- a/doc/src/links.md +++ b/doc/src/links.md @@ -33,7 +33,7 @@ [packages]: {{rootUrl}}/rust/packages/index.md [custom package]: {{rootUrl}}/rust/packages/create.md [custom packages]: {{rootUrl}}/rust/packages/create.md -[`Scope`]: {{rootUrl}}/rust/scope.md +[`Scope`]: {{rootUrl}}/engine/scope.md [`serde`]: {{rootUrl}}/rust/serde.md [`type_of()`]: {{rootUrl}}/language/type-of.md