Remove no-std feature and rename stdlib to no_stdlib.

This commit is contained in:
Stephen Chung 2020-03-05 10:18:46 +08:00
parent 347f6d607a
commit c9395049e2
4 changed files with 27 additions and 25 deletions

View File

@ -16,5 +16,4 @@ include = [
[features] [features]
debug_msgs = [] debug_msgs = []
no-std = [] no_stdlib = []
stdlib = []

View File

@ -33,6 +33,16 @@ to use the latest version.
Beware that in order to use pre-releases (alpha and beta) you need to specify the exact version in your `Cargo.toml`. Beware that in order to use pre-releases (alpha and beta) you need to specify the exact version in your `Cargo.toml`.
## Optional Features
### `debug_msgs`
Print debug messages to stdout (using `println!`) related to function registrations and function calls.
### `no_stdlib`
Exclude the standard library of utility functions in the build, and only include the minimum necessary functionalities.
## Related ## Related
Other cool projects to check out: Other cool projects to check out:
@ -601,7 +611,7 @@ println!(result); // prints "Runtime error: 42 is too large! (line 5, position
You can create arrays of values, and then access them with numeric indices. You can create arrays of values, and then access them with numeric indices.
The following standard functions operate on arrays: The following functions (defined in the standard library but excluded if you use the `no_stdlib` feature) operate on arrays:
* `push` - inserts an element at the end * `push` - inserts an element at the end
* `pop` - removes the last element and returns it (() if empty) * `pop` - removes the last element and returns it (() if empty)
@ -699,7 +709,7 @@ let x = 0o777; // i64 in oct
let x = 0b1010_1111; // i64 in binary let x = 0b1010_1111; // i64 in binary
``` ```
Conversion functions: Conversion functions (defined in the standard library but excluded if you use the `no_stdlib` feature):
* `to_int` - converts an `f32` or `f64` to `i64` * `to_int` - converts an `f32` or `f64` to `i64`
* `to_float` - converts an integer type to `f64` * `to_float` - converts an integer type to `f64`
@ -714,7 +724,7 @@ let last = "Davis";
let full_name = name + " " + middle_initial + ". " + last; let full_name = name + " " + middle_initial + ". " + last;
full_name == "Bob C. Davis"; full_name == "Bob C. Davis";
// String building with different types // String building with different types (not available if 'no_stdlib' features is used)
let age = 42; let age = 42;
let record = full_name + ": age " + age; let record = full_name + ": age " + age;
record == "Bob C. Davis: age 42"; record == "Bob C. Davis: age 42";
@ -738,7 +748,7 @@ record[4] = '\x58'; // 0x58 = 'X'
record == "Bob X. Davis: age 42 ❤\n"; record == "Bob X. Davis: age 42 ❤\n";
``` ```
The following standard functions operate on strings: The following standard functions (defined in the standard library but excluded if you use the `no_stdlib` feature) operate on strings:
* `len` - returns the number of characters (not number of bytes) in the string * `len` - returns the number of characters (not number of bytes) in the string
* `pad` - pads the string with an character until a specified number of characters * `pad` - pads the string with an character until a specified number of characters

View File

@ -4,9 +4,6 @@ use crate::fn_register::RegisterFn;
use std::fmt::{Debug, Display}; use std::fmt::{Debug, Display};
use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Range, Rem, Shl, Shr, Sub}; use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Range, Rem, Shl, Shr, Sub};
#[cfg(not(feature = "no-std"))]
use crate::fn_register::RegisterDynamicFn;
macro_rules! reg_op { macro_rules! reg_op {
($self:expr, $x:expr, $op:expr, $( $y:ty ),*) => ( ($self:expr, $x:expr, $op:expr, $( $y:ty ),*) => (
$( $(
@ -39,7 +36,7 @@ macro_rules! reg_func1 {
) )
} }
#[cfg(any(not(feature = "no-std"), feature = "stdlib"))] #[cfg(not(feature = "no_stdlib"))]
macro_rules! reg_func2x { macro_rules! reg_func2x {
($self:expr, $x:expr, $op:expr, $v:ty, $r:ty, $( $y:ty ),*) => ( ($self:expr, $x:expr, $op:expr, $v:ty, $r:ty, $( $y:ty ),*) => (
$( $(
@ -48,7 +45,7 @@ macro_rules! reg_func2x {
) )
} }
#[cfg(any(not(feature = "no-std"), feature = "stdlib"))] #[cfg(not(feature = "no_stdlib"))]
macro_rules! reg_func2y { macro_rules! reg_func2y {
($self:expr, $x:expr, $op:expr, $v:ty, $r:ty, $( $y:ty ),*) => ( ($self:expr, $x:expr, $op:expr, $v:ty, $r:ty, $( $y:ty ),*) => (
$( $(
@ -57,7 +54,7 @@ macro_rules! reg_func2y {
) )
} }
#[cfg(any(not(feature = "no-std"), feature = "stdlib"))] #[cfg(not(feature = "no_stdlib"))]
macro_rules! reg_func3 { macro_rules! reg_func3 {
($self:expr, $x:expr, $op:expr, $v:ty, $w:ty, $r:ty, $( $y:ty ),*) => ( ($self:expr, $x:expr, $op:expr, $v:ty, $w:ty, $r:ty, $( $y:ty ),*) => (
$( $(
@ -111,9 +108,6 @@ impl Engine<'_> {
fn not(x: bool) -> bool { fn not(x: bool) -> bool {
!x !x
} }
fn concat(x: String, y: String) -> String {
x + &y
}
fn binary_and<T: BitAnd>(x: T, y: T) -> <T as BitAnd>::Output { fn binary_and<T: BitAnd>(x: T, y: T) -> <T as BitAnd>::Output {
x & y x & y
} }
@ -141,9 +135,6 @@ impl Engine<'_> {
fn pow_f64_i64(x: f64, y: i64) -> f64 { fn pow_f64_i64(x: f64, y: i64) -> f64 {
x.powi(y as i32) x.powi(y as i32)
} }
fn unit_eq(_a: (), _b: ()) -> bool {
true
}
reg_op!(self, "+", add, i8, u8, i16, u16, i32, i64, u32, u64, f32, f64); reg_op!(self, "+", add, i8, u8, i16, u16, i32, i64, u32, u64, f32, f64);
reg_op!(self, "-", sub, i8, u8, i16, u16, i32, i64, u32, u64, f32, f64); reg_op!(self, "-", sub, i8, u8, i16, u16, i32, i64, u32, u64, f32, f64);
@ -179,8 +170,8 @@ impl Engine<'_> {
reg_un!(self, "-", neg, i8, i16, i32, i64, f32, f64); reg_un!(self, "-", neg, i8, i16, i32, i64, f32, f64);
reg_un!(self, "!", not, bool); reg_un!(self, "!", not, bool);
self.register_fn("+", concat); self.register_fn("+", |x: String, y: String| x + &y); // String + String
self.register_fn("==", unit_eq); self.register_fn("==", |_: (), _: ()| true); // () == ()
// Register print and debug // Register print and debug
fn print_debug<T: Debug>(x: T) -> String { fn print_debug<T: Debug>(x: T) -> String {
@ -221,8 +212,10 @@ impl Engine<'_> {
} }
/// Register the built-in library. /// Register the built-in library.
#[cfg(any(not(feature = "no-std"), feature = "stdlib"))] #[cfg(not(feature = "no_stdlib"))]
pub(crate) fn register_stdlib(&mut self) { pub(crate) fn register_stdlib(&mut self) {
use crate::fn_register::RegisterDynamicFn;
// Register conversion functions // Register conversion functions
self.register_fn("to_float", |x: i8| x as f64); self.register_fn("to_float", |x: i8| x as f64);
self.register_fn("to_float", |x: u8| x as f64); self.register_fn("to_float", |x: u8| x as f64);

View File

@ -751,19 +751,19 @@ impl Engine<'_> {
engine.register_core_lib(); engine.register_core_lib();
#[cfg(any(not(feature = "no-std"), feature = "stdlib"))] #[cfg(not(feature = "no_stdlib"))]
engine.register_stdlib(); // Register the standard library when not no-std or stdlib is set engine.register_stdlib(); // Register the standard library when no_stdlib is not set
engine engine
} }
} }
/// Print/debug to stdout /// Print/debug to stdout
#[cfg(not(feature = "no-std"))] #[cfg(not(feature = "no_stdlib"))]
fn default_print(s: &str) { fn default_print(s: &str) {
println!("{}", s); println!("{}", s);
} }
/// No-op /// No-op
#[cfg(feature = "no-std")] #[cfg(feature = "no_stdlib")]
fn default_print(_: &str) {} fn default_print(_: &str) {}