From a6b78944c9d9a4df431375beb0fb051d1c597ae9 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 21 Sep 2021 00:14:50 +0800 Subject: [PATCH 1/3] Add example to README. --- README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/README.md b/README.md index e4746f9f..3f2ac980 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,47 @@ For those who actually want their own language * Extend the language with [custom syntax](https://rhai.rs/book/engine/custom-syntax.html). +Example +------- + +The [`scripts`](https://github.com/rhaiscript/rhai/tree/master/scripts) subdirectory contains sample Rhai scripts. + +Below is a the standard _Fibonacci_ example for scripting languages: + +```js +// This Rhai script calculates the n-th Fibonacci number using a really dumb algorithm +// to test the speed of the scripting engine. + +const TARGET = 28; +const REPEAT = 5; + +fn fib(n) { + if n < 2 { + n + } else { + fib(n-1) + fib(n-2) + } +} + +print(`Running Fibonacci(28) x ${REPEAT} times...`); +print("Ready... Go!"); + +let result; +let now = timestamp(); + +for n in range(0, REPEAT) { + result = fib(TARGET); +} + +print(`Finished. Run time = ${now.elapsed} seconds.`); + +print(`Fibonacci number #${TARGET} = ${result}`); + +if result != 317_811 { + print("The answer is WRONG! Should be 317,811!"); +} +``` + Project Site ------------ From 562731c15499914a9ec13f2744cfa776f66b8357 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 21 Sep 2021 10:41:09 +0800 Subject: [PATCH 2/3] Fix fibonacci example. --- README.md | 4 ++-- scripts/fibonacci.rhai | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3f2ac980..ba6ddf2c 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ Example The [`scripts`](https://github.com/rhaiscript/rhai/tree/master/scripts) subdirectory contains sample Rhai scripts. -Below is a the standard _Fibonacci_ example for scripting languages: +Below is the standard _Fibonacci_ example for scripting languages: ```js // This Rhai script calculates the n-th Fibonacci number using a really dumb algorithm @@ -93,7 +93,7 @@ fn fib(n) { } } -print(`Running Fibonacci(28) x ${REPEAT} times...`); +print(`Running Fibonacci(${TARGET}) x ${REPEAT} times...`); print("Ready... Go!"); let result; diff --git a/scripts/fibonacci.rhai b/scripts/fibonacci.rhai index fd3ba5c0..dac77d22 100644 --- a/scripts/fibonacci.rhai +++ b/scripts/fibonacci.rhai @@ -12,7 +12,7 @@ fn fib(n) { } } -print(`Running Fibonacci(28) x ${REPEAT} times...`); +print(`Running Fibonacci(${TARGET}) x ${REPEAT} times...`); print("Ready... Go!"); let result; From d9dca6ef98ff234dd2b89a736a61c3f753c9e849 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Fri, 24 Sep 2021 09:26:35 +0800 Subject: [PATCH 3/3] Expore more internal data structures. --- CHANGELOG.md | 2 ++ src/dynamic.rs | 3 ++- src/lib.rs | 11 +++++++++-- src/parse.rs | 26 ++++++++++++++------------ src/token.rs | 25 ++++++++++++++----------- 5 files changed, 41 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85391dad..b6c4d8c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Rhai Release Notes Version 1.0.6 ============= +* `MultiInputsStream`, `ParseState`, `TokenIterator`, `IdentifierBuilder` and `AccessMode` are exported under the `internals` feature. + Version 1.0.5 ============= diff --git a/src/dynamic.rs b/src/dynamic.rs index 401e3907..d2143361 100644 --- a/src/dynamic.rs +++ b/src/dynamic.rs @@ -147,7 +147,8 @@ impl dyn Variant { } } -/// Modes of access. +/// _(internals)_ Modes of access. +/// Exported under the `internals` feature only. #[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)] pub enum AccessMode { /// Mutable. diff --git a/src/lib.rs b/src/lib.rs index f26ad35b..72a6d61a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -212,7 +212,7 @@ pub use optimize::OptimizationLevel; #[cfg(feature = "internals")] #[deprecated = "this type is volatile and may change"] -pub use dynamic::{DynamicReadLock, DynamicWriteLock, Variant}; +pub use dynamic::{AccessMode, DynamicReadLock, DynamicWriteLock, Variant}; // Expose internal data structures. #[cfg(feature = "internals")] @@ -222,7 +222,14 @@ pub use token::{get_next_token, parse_string_literal}; // Expose internal data structures. #[cfg(feature = "internals")] #[deprecated = "this type is volatile and may change"] -pub use token::{InputStream, Token, TokenizeState, TokenizerControl, TokenizerControlBlock}; +pub use token::{ + InputStream, MultiInputsStream, Token, TokenIterator, TokenizeState, TokenizerControl, + TokenizerControlBlock, +}; + +#[cfg(feature = "internals")] +#[deprecated = "this type is volatile and may change"] +pub use parse::{IdentifierBuilder, ParseState}; #[cfg(feature = "internals")] #[deprecated = "this type is volatile and may change"] diff --git a/src/parse.rs b/src/parse.rs index 3750599f..db73a0eb 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -44,7 +44,8 @@ const SCOPE_SEARCH_BARRIER_MARKER: &str = "$BARRIER$"; /// The message: never fails because `TokenStream` never ends const NEVER_ENDS: &str = "never fails because `TokenStream` never ends"; -/// A factory of identifiers from text strings. +/// _(internals)_ A factory of identifiers from text strings. +/// Exported under the `internals` feature only. /// /// When [`SmartString`](https://crates.io/crates/smartstring) is used as [`Identifier`], /// this just returns a copy because most identifiers in Rhai are short and ASCII-based. @@ -74,38 +75,39 @@ impl IdentifierBuilder { } } -/// A type that encapsulates the current state of the parser. +/// _(internals)_ A type that encapsulates the current state of the parser. +/// Exported under the `internals` feature only. #[derive(Debug)] pub struct ParseState<'e> { /// Reference to the scripting [`Engine`]. - engine: &'e Engine, + pub engine: &'e Engine, /// Input stream buffer containing the next character to read. - tokenizer_control: TokenizerControl, + pub tokenizer_control: TokenizerControl, /// Interned strings. - interned_strings: IdentifierBuilder, + pub interned_strings: IdentifierBuilder, /// Encapsulates a local stack with variable names to simulate an actual runtime scope. - stack: Vec<(Identifier, AccessMode)>, + pub stack: Vec<(Identifier, AccessMode)>, /// Size of the local variables stack upon entry of the current block scope. - entry_stack_len: usize, + pub entry_stack_len: usize, /// Tracks a list of external variables (variables that are not explicitly declared in the scope). #[cfg(not(feature = "no_closure"))] - external_vars: BTreeMap, + pub external_vars: BTreeMap, /// An indicator that disables variable capturing into externals one single time /// up until the nearest consumed Identifier token. /// If set to false the next call to [`access_var`][ParseState::access_var] will not capture the variable. /// All consequent calls to [`access_var`][ParseState::access_var] will not be affected #[cfg(not(feature = "no_closure"))] - allow_capture: bool, + pub allow_capture: bool, /// Encapsulates a local stack with imported [module][crate::Module] names. #[cfg(not(feature = "no_module"))] - modules: StaticVec, + pub modules: StaticVec, /// Maximum levels of expression nesting. #[cfg(not(feature = "unchecked"))] - max_expr_depth: Option, + pub max_expr_depth: Option, /// Maximum levels of expression nesting in functions. #[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "no_function"))] - max_function_expr_depth: Option, + pub max_function_expr_depth: Option, } impl<'e> ParseState<'e> { diff --git a/src/token.rs b/src/token.rs index 3fd2d8b3..1779a34b 100644 --- a/src/token.rs +++ b/src/token.rs @@ -2057,15 +2057,17 @@ pub fn is_id_continue(x: char) -> bool { x.is_ascii_alphanumeric() || x == '_' } -/// A type that implements the [`InputStream`] trait. +/// _(internals)_ A type that implements the [`InputStream`] trait. +/// Exported under the `internals` feature only. +/// /// Multiple character streams are jointed together to form one single stream. pub struct MultiInputsStream<'a> { /// Buffered character, if any. - buf: Option, + pub buf: Option, /// The current stream index. - index: usize, + pub index: usize, /// The input character streams. - streams: StaticVec>>, + pub streams: StaticVec>>, } impl InputStream for MultiInputsStream<'_> { @@ -2115,20 +2117,21 @@ impl InputStream for MultiInputsStream<'_> { } } -/// An iterator on a [`Token`] stream. +/// _(internals)_ An iterator on a [`Token`] stream. +/// Exported under the `internals` feature only. pub struct TokenIterator<'a> { /// Reference to the scripting `Engine`. - engine: &'a Engine, + pub engine: &'a Engine, /// Current state. - state: TokenizeState, + pub state: TokenizeState, /// Current position. - pos: Position, + pub pos: Position, /// External buffer containing the next character to read, if any. - tokenizer_control: TokenizerControl, + pub tokenizer_control: TokenizerControl, /// Input character stream. - stream: MultiInputsStream<'a>, + pub stream: MultiInputsStream<'a>, /// A processor function that maps a token to another. - map: Option Token>, + pub map: Option Token>, } impl<'a> Iterator for TokenIterator<'a> {