Fix no_std build.

This commit is contained in:
Stephen Chung
2020-04-24 12:39:24 +08:00
parent a306979a9c
commit b6d839c8a9
20 changed files with 238 additions and 167 deletions

View File

@@ -15,6 +15,7 @@ use num_traits::{
};
use crate::stdlib::{
boxed::Box,
fmt::Display,
format,
ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Rem, Shl, Shr, Sub},

View File

@@ -6,7 +6,7 @@ use crate::engine::Array;
use crate::fn_register::{map_dynamic as map, map_identity as pass};
use crate::parser::INT;
use crate::stdlib::any::TypeId;
use crate::stdlib::{any::TypeId, boxed::Box, string::String};
// Register array utility functions
fn push<T: Variant + Clone>(list: &mut Array, item: T) {

View File

@@ -8,6 +8,7 @@ use crate::parser::INT;
use crate::stdlib::{
any::TypeId,
boxed::Box,
ops::{Add, Range},
};

View File

@@ -4,6 +4,8 @@ use crate::def_package;
use crate::fn_register::map_dynamic as map;
use crate::parser::INT;
use crate::stdlib::string::String;
// Comparison operators
pub fn lt<T: PartialOrd>(x: T, y: T) -> bool {
x < y

View File

@@ -6,6 +6,11 @@ use crate::engine::Map;
use crate::fn_register::map_dynamic as map;
use crate::parser::INT;
use crate::stdlib::{
string::{String, ToString},
vec::Vec,
};
fn map_get_keys(map: &mut Map) -> Vec<Dynamic> {
map.iter()
.map(|(k, _)| k.to_string().into())

View File

@@ -9,7 +9,7 @@ use crate::token::Position;
#[cfg(not(feature = "no_float"))]
use crate::parser::FLOAT;
use crate::stdlib::{i32, i64};
use crate::stdlib::{boxed::Box, format, i32, i64};
#[cfg(feature = "only_i32")]
pub const MAX_INT: INT = i32::MAX;

View File

@@ -29,6 +29,7 @@ pub use pkg_core::CorePackage;
pub use pkg_std::StandardPackage;
pub use string_basic::BasicStringPackage;
pub use string_more::MoreStringPackage;
#[cfg(not(feature = "no_std"))]
pub use time_basic::BasicTimePackage;
pub use utils::*;

View File

@@ -5,6 +5,7 @@ use super::map_basic::BasicMapPackage;
use super::math_basic::BasicMathPackage;
use super::pkg_core::CorePackage;
use super::string_more::MoreStringPackage;
#[cfg(not(feature = "no_std"))]
use super::time_basic::BasicTimePackage;
use crate::def_package;
@@ -16,6 +17,7 @@ def_package!(crate:StandardPackage:"_Standard_ package containing all built-in f
BasicArrayPackage::init(lib);
#[cfg(not(feature = "no_object"))]
BasicMapPackage::init(lib);
#[cfg(not(feature = "no_std"))]
BasicTimePackage::init(lib);
MoreStringPackage::init(lib);
});

View File

@@ -8,6 +8,7 @@ use crate::parser::INT;
use crate::stdlib::{
fmt::{Debug, Display},
format,
string::{String, ToString},
};
// Register print and debug

View File

@@ -5,7 +5,12 @@ use crate::engine::Array;
use crate::fn_register::map_dynamic as map;
use crate::parser::INT;
use crate::stdlib::fmt::Display;
use crate::stdlib::{
fmt::Display,
format,
string::{String, ToString},
vec::Vec,
};
fn prepend<T: Display>(x: T, y: String) -> String {
format!("{}{}", x, y)

View File

@@ -8,64 +8,63 @@ use crate::parser::INT;
use crate::result::EvalAltResult;
use crate::token::Position;
#[cfg(not(feature = "no_std"))]
use crate::stdlib::time::Instant;
#[cfg(not(feature = "no_std"))]
def_package!(crate:BasicTimePackage:"Basic timing utilities.", lib, {
#[cfg(not(feature = "no_std"))]
{
// Register date/time functions
reg_none(lib, "timestamp", || Instant::now(), map);
// Register date/time functions
reg_none(lib, "timestamp", || Instant::now(), map);
reg_binary(
lib,
"-",
|ts1: Instant, ts2: Instant| {
if ts2 > ts1 {
#[cfg(not(feature = "no_float"))]
return Ok(-(ts2 - ts1).as_secs_f64());
reg_binary(
lib,
"-",
|ts1: Instant, ts2: Instant| {
if ts2 > ts1 {
#[cfg(not(feature = "no_float"))]
return Ok(-(ts2 - ts1).as_secs_f64());
#[cfg(feature = "no_float")]
#[cfg(feature = "no_float")]
{
let seconds = (ts2 - ts1).as_secs();
#[cfg(not(feature = "unchecked"))]
{
let seconds = (ts2 - ts1).as_secs();
#[cfg(not(feature = "unchecked"))]
{
if seconds > (MAX_INT as u64) {
return Err(Box::new(EvalAltResult::ErrorArithmetic(
format!(
"Integer overflow for timestamp duration: {}",
-(seconds as i64)
),
Position::none(),
)));
}
if seconds > (MAX_INT as u64) {
return Err(Box::new(EvalAltResult::ErrorArithmetic(
format!(
"Integer overflow for timestamp duration: {}",
-(seconds as i64)
),
Position::none(),
)));
}
return Ok(-(seconds as INT));
}
} else {
#[cfg(not(feature = "no_float"))]
return Ok((ts1 - ts2).as_secs_f64());
#[cfg(feature = "no_float")]
{
let seconds = (ts1 - ts2).as_secs();
#[cfg(not(feature = "unchecked"))]
{
if seconds > (MAX_INT as u64) {
return Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Integer overflow for timestamp duration: {}", seconds),
Position::none(),
)));
}
}
return Ok(seconds as INT);
}
return Ok(-(seconds as INT));
}
},
result,
);
}
} else {
#[cfg(not(feature = "no_float"))]
return Ok((ts1 - ts2).as_secs_f64());
#[cfg(feature = "no_float")]
{
let seconds = (ts1 - ts2).as_secs();
#[cfg(not(feature = "unchecked"))]
{
if seconds > (MAX_INT as u64) {
return Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Integer overflow for timestamp duration: {}", seconds),
Position::none(),
)));
}
}
return Ok(seconds as INT);
}
}
},
result,
);
reg_binary(lib, "<", lt::<Instant>, map);
reg_binary(lib, "<=", lte::<Instant>, map);

View File

@@ -6,7 +6,11 @@ use crate::engine::FnCallArgs;
use crate::result::EvalAltResult;
use crate::token::Position;
use crate::stdlib::{any::TypeId, boxed::Box};
use crate::stdlib::{
any::TypeId,
boxed::Box,
string::{String, ToString},
};
/// This macro makes it easy to define a _package_ and register functions into it.
///