Revise using string interpolation.

This commit is contained in:
Stephen Chung 2021-04-04 23:22:45 +08:00
parent bc9c1ab850
commit 0807c474a1
16 changed files with 33 additions and 32 deletions

View File

@ -1,7 +1,7 @@
Rhai Release Notes Rhai Release Notes
================== ==================
This version adds string interpolation. This version adds string interpolation with `` `... ${`` ... ``} ...` `` syntax.
Version 0.19.16 Version 0.19.16
=============== ===============

View File

@ -93,8 +93,8 @@ fn bench_parse_primes(bench: &mut Bencher) {
} }
} }
print("Total " + total_primes_found + " primes."); print(`Total ${total_primes_found} primes.`);
print("Run time = " + now.elapsed + " seconds."); print(`Run time = ${now.elapsed} seconds.`);
"#; "#;
let mut engine = Engine::new(); let mut engine = Engine::new();

View File

@ -40,9 +40,9 @@ fn main() {
loop { loop {
let x = get(); let x = get();
print("Script Read: " + x); print(`Script Read: ${x}`);
x += 1; x += 1;
print("Script Write: " + x); print(`Script Write: ${x}`);
put(x); put(x);
} }
"#, "#,

View File

@ -21,9 +21,9 @@ for n in range(0, 5) {
result = fib(target); result = fib(target);
} }
print("Finished. Run time = " + now.elapsed + " seconds."); print(`Finished. Run time = ${now.elapsed} seconds.`);
print("Fibonacci number #" + target + " = " + result); print(`Fibonacci number #${target} = ${result}`);
if result != 317_811 { if result != 317_811 {
print("The answer is WRONG! Should be 317,811!"); print("The answer is WRONG! Should be 317,811!");

View File

@ -4,7 +4,7 @@ let arr = [1, 2, 3, 4];
for a in arr { for a in arr {
for b in [10, 20] { for b in [10, 20] {
print(a + "," + b); print(`${a}, ${b}`);
} }
if a == 3 { break; } if a == 3 { break; }

View File

@ -1,6 +1,6 @@
const MAX = 1_000_000; const MAX = 1_000_000;
print("Iterating an array with " + MAX + " items..."); print(`Iterating an array with ${MAX} items...`);
print("Ready... Go!"); print("Ready... Go!");
@ -18,5 +18,5 @@ for i in list {
sum += i; sum += i;
} }
print("Sum = " + sum); print(`Sum = ${sum}`);
print("Finished. Run time = " + now.elapsed + " seconds."); print(`Finished. Run time = ${now.elapsed} seconds.`);

View File

@ -68,4 +68,4 @@ for i in range(0, SIZE) {
} }
*/ */
print("Finished. Run time = " + now.elapsed + " seconds."); print(`Finished. Run time = ${now.elapsed} seconds.`);

View File

@ -7,7 +7,7 @@ let last_value = ();
let obj1 = #{ let obj1 = #{
_data: 42, // data field _data: 42, // data field
get_data: || this._data, // property getter get_data: || this._data, // property getter
action: || print("Data=" + this._data), // method action: || print(`Data=${this._data}`), // method
update: |x| { // property setter update: |x| { // property setter
this._data = x; this._data = x;
last_value = this._data; // capture 'last_value' last_value = this._data; // capture 'last_value'
@ -38,4 +38,4 @@ if obj2.get_data() > 0 { // property access
obj2.update(42); // call method obj2.update(42); // call method
} }
print("Should be 84: " + last_value); print(`Should be 84: ${last_value}`);

View File

@ -25,8 +25,8 @@ for p in range(2, MAX_NUMBER_TO_CHECK) {
} }
} }
print("Total " + total_primes_found + " primes <= " + MAX_NUMBER_TO_CHECK); print(`Total ${total_primes_found} primes <= ${MAX_NUMBER_TO_CHECK}`);
print("Run time = " + now.elapsed + " seconds."); print(`Run time = ${now.elapsed} seconds.`);
if total_primes_found != 78_498 { if total_primes_found != 78_498 {
print("The answer is WRONG! Should be 78,498!"); print("The answer is WRONG! Should be 78,498!");

View File

@ -10,4 +10,4 @@ while x > 0 {
x -= 1; x -= 1;
} }
print("Finished. Run time = " + now.elapsed + " seconds."); print(`Finished. Run time = ${now.elapsed} seconds.`);

View File

@ -11,17 +11,18 @@ print("foo" >= "bar"); // string comparison
print("the answer is " + 42); // string building using non-string types print("the answer is " + 42); // string building using non-string types
let s = "\u2764 hello, world! \U0001F603"; // string variable let s = "\u2764 hello, world! \U0001F603"; // string variable
print("length=" + s.len); // should be 17 print(`length=${s.len}`); // should be 17
s[s.len-3] = '?'; // change the string s[s.len-3] = '?'; // change the string
print("Question: " + s); // should print 'Question: hello, world?' print(`Question: ${s}`); // should print 'Question: hello, world?'
// Line continuation: // Line continuation:
let s = "This is a long \ let s = "This is a long \
string constructed using \ string constructed using \
line continuation"; line continuation";
print("One string: " + s); // String interpolation
print(`One string: ${s}`);
// Multi-line literal string: // Multi-line literal string:
let s = ` let s = `

View File

@ -75,7 +75,7 @@ let keys = [];
for animal in animals { for animal in animals {
for adjective in adjectives { for adjective in adjectives {
for adverb in adverbs { for adverb in adverbs {
keys.push(adverb + " " + adjective + " " + animal) keys.push(`${adverb} ${adjective} ${animal}`)
} }
} }
} }
@ -99,5 +99,5 @@ for key in keys {
map.remove(key); map.remove(key);
} }
print("Sum = " + sum); print(`Sum = ${sum}`);
print("Finished. Run time = " + now.elapsed + " seconds."); print(`Finished. Run time = ${now.elapsed} seconds.`);

View File

@ -384,7 +384,7 @@ impl AST {
/// "#)?; /// "#)?;
/// ///
/// let ast2 = engine.compile(r#" /// let ast2 = engine.compile(r#"
/// fn foo(n) { "hello" + n } /// fn foo(n) { `hello${n}` }
/// foo("!") /// foo("!")
/// "#)?; /// "#)?;
/// ///
@ -395,7 +395,7 @@ impl AST {
/// ///
/// // 'ast' is essentially: /// // 'ast' is essentially:
/// // /// //
/// // fn foo(n) { "hello" + n } // <- definition of first 'foo' is overwritten /// // fn foo(n) { `hello${n}` } // <- definition of first 'foo' is overwritten
/// // foo(1) // <- notice this will be "hello1" instead of 43, /// // foo(1) // <- notice this will be "hello1" instead of 43,
/// // // but it is no longer the return value /// // // but it is no longer the return value
/// // foo("!") // returns "hello!" /// // foo("!") // returns "hello!"
@ -436,7 +436,7 @@ impl AST {
/// "#)?; /// "#)?;
/// ///
/// let ast2 = engine.compile(r#" /// let ast2 = engine.compile(r#"
/// fn foo(n) { "hello" + n } /// fn foo(n) { `hello${n}` }
/// foo("!") /// foo("!")
/// "#)?; /// "#)?;
/// ///
@ -447,7 +447,7 @@ impl AST {
/// ///
/// // 'ast1' is essentially: /// // 'ast1' is essentially:
/// // /// //
/// // fn foo(n) { "hello" + n } // <- definition of first 'foo' is overwritten /// // fn foo(n) { `hello${n}` } // <- definition of first 'foo' is overwritten
/// // foo(1) // <- notice this will be "hello1" instead of 43, /// // foo(1) // <- notice this will be "hello1" instead of 43,
/// // // but it is no longer the return value /// // // but it is no longer the return value
/// // foo("!") // returns "hello!" /// // foo("!") // returns "hello!"
@ -490,7 +490,7 @@ impl AST {
/// "#)?; /// "#)?;
/// ///
/// let ast2 = engine.compile(r#" /// let ast2 = engine.compile(r#"
/// fn foo(n) { "hello" + n } /// fn foo(n) { `hello${n}` }
/// fn error() { 0 } /// fn error() { 0 }
/// foo("!") /// foo("!")
/// "#)?; /// "#)?;
@ -574,7 +574,7 @@ impl AST {
/// "#)?; /// "#)?;
/// ///
/// let ast2 = engine.compile(r#" /// let ast2 = engine.compile(r#"
/// fn foo(n) { "hello" + n } /// fn foo(n) { `hello${n}` }
/// fn error() { 0 } /// fn error() { 0 }
/// foo("!") /// foo("!")
/// "#)?; /// "#)?;

View File

@ -41,7 +41,7 @@ pub trait FuncArgs {
/// ///
/// let ast = engine.compile(r#" /// let ast = engine.compile(r#"
/// fn hello(x, y, z) { /// fn hello(x, y, z) {
/// if x { "hello " + y } else { y + z } /// if x { `hello ${y}` } else { y + z }
/// } /// }
/// "#)?; /// "#)?;
/// ///

View File

@ -81,7 +81,7 @@ fn test_call_fn_args() -> Result<(), Box<EvalAltResult>> {
let ast = engine.compile( let ast = engine.compile(
r#" r#"
fn hello(x, y, z) { fn hello(x, y, z) {
if x { "hello " + y } else { y + z } if x { `hello ${y}` } else { y + z }
} }
"#, "#,
)?; )?;

View File

@ -315,7 +315,7 @@ fn test_module_from_ast() -> Result<(), Box<EvalAltResult>> {
// Final variable values become constant module variable values // Final variable values become constant module variable values
foo = calc(foo); foo = calc(foo);
hello = "hello, " + foo + " worlds!"; hello = `hello, ${foo} worlds!`;
export export
x as abc, x as abc,