Format example scripts better.

This commit is contained in:
Stephen Chung 2022-01-20 12:06:36 +08:00
parent 6b06019265
commit b63b4cb3af
22 changed files with 72 additions and 30 deletions

View File

@ -1,5 +1,5 @@
print("x should be 78:"); // This script contains a single assignment statement.
let x = 78; let x = 78;
print(x); print(`x should be 78: ${x}`);

View File

@ -3,9 +3,9 @@
/// ///
/// # Parameters /// # Parameters
/// ///
/// `x` - `i64` /// * `x` - `i64`
/// `y` - `string` /// * `y` - `string`
/// `z` - `bool` /// * `z` - `bool`
/// ///
/// # Notes /// # Notes
/// ///
@ -13,6 +13,13 @@
/// ///
/// An example is the `rhai-doc` app. /// An example is the `rhai-doc` app.
/// ///
/// # Example
///
/// ```rhai
/// let x = foo(42, "hello", true);
///
/// print(x); // prints 47
/// ```
fn foo(x, y, z) { fn foo(x, y, z) {
print(`hello, world! ${if z { x + y.len() } else { x } }`); print(`hello, world! ${if z { x + y.len() } else { x } }`);
} }

View File

@ -1,7 +1,8 @@
// This script runs for-loops // This script runs for-loops.
let arr = [1, true, 123.456, "hello", 3, 42]; let arr = [1, true, 123.456, "hello", 3, 42];
// Loop over array with counter
for (a, i) in arr { for (a, i) in arr {
for (b, j) in ['x', 42, (), 123, 99, 0.5] { for (b, j) in ['x', 42, (), 123, 99, 0.5] {
if b > 100 { continue; } if b > 100 { continue; }

View File

@ -1,3 +1,5 @@
// This script runs for-loops
const MAX = 1_000_000; const MAX = 1_000_000;
print(`Iterating an array with ${MAX} items...`); print(`Iterating an array with ${MAX} items...`);
@ -8,6 +10,7 @@ let now = timestamp();
let list = []; let list = [];
// Loop over range
for i in 0..MAX { for i in 0..MAX {
list.push(i); list.push(i);
} }
@ -16,6 +19,7 @@ print(`Time = ${now.elapsed} seconds...`);
let sum = 0; let sum = 0;
// Loop over array
for i in list { for i in list {
sum += i; sum += i;
} }

View File

@ -1,3 +1,5 @@
// This script runs for-loops with closures.
const MAX = 100; const MAX = 100;
const CHECK = ((MAX - 1) ** 2) * MAX; const CHECK = ((MAX - 1) ** 2) * MAX;
@ -9,6 +11,7 @@ print(`Creating ${MAX} closures...`);
let list = []; let list = [];
// Loop over range
for i in 0..MAX { for i in 0..MAX {
list.push(|| i ** 2); list.push(|| i ** 2);
} }
@ -18,6 +21,7 @@ print(`Summing ${MAX} closures...`);
let sum = 0; let sum = 0;
// Loop over array
for f in list { for f in list {
sum += f.call(); sum += f.call();
} }

View File

@ -1,9 +1,9 @@
// This script defines a function and calls it // This script defines a function and calls it.
fn bob() { fn call_me() {
return 3; return 3;
} }
let result = bob(); let result = call_me();
print(`bob() should be 3: ${result}`); print(`call_me() should be 3: ${result}`);

View File

@ -1,14 +1,14 @@
// This script defines a function with two parameters // This script defines a function with two parameters and local variables.
let a = 3; let a = 3;
fn addme(a, b) { fn add(a, b) {
a = 42; // notice that 'a' is passed by value a = 42; // notice that 'a' is passed by value
a + b; // notice that the last value is returned even if terminated by a semicolon a + b; // notice that the last value is returned even if terminated by a semicolon
} }
let result = addme(a, 4); let result = add(a, 4);
print(`addme(a, 4) should be 46: ${result}`); print(`add(a, 4) should be 46: ${result}`);
print(`a should still be 3: ${a}`); // should print 3 - 'a' is never changed print(`a should still be 3: ${a}`); // prints 3: 'a' is never changed

View File

@ -1,9 +1,11 @@
// This script defines a function with many parameters and calls it // This script defines a function with many parameters.
//
const KEY = 38; const KEY = 38;
fn f(a, b, c, d, e, f) { fn f(a, b, c, d, e, f) {
a - b * c - d * e - f + global::KEY let x = global::KEY; // <- access global module
a - b * c - d * e - f + x
} }
let result = f(100, 5, 2, 9, 6, 32); let result = f(100, 5, 2, 9, 6, 32);

View File

@ -1,4 +1,4 @@
// This script defines a function that acts as a method // This script defines a function that acts as a method.
// Use 'this' to refer to the object of a method call // Use 'this' to refer to the object of a method call
fn action(x, y) { fn action(x, y) {

View File

@ -1,3 +1,5 @@
// This script runs if statements.
let a = 42; let a = 42;
let b = 123; let b = 123;
let x = 999; let x = 999;
@ -7,7 +9,7 @@ if a > b {
} else if a < b { } else if a < b {
print("a < b, x should be 0"); print("a < b, x should be 0");
let x = 0; // this 'x' shadows the global 'x' let x = 0; // <- this 'x' shadows the global 'x'
print(x); // should print 0 print(x); // should print 0
} else { } else {
print("Oops! a == b"); print("Oops! a == b");

View File

@ -1,7 +1,9 @@
// This script runs an if expression.
let a = 42; let a = 42;
let b = 123; let b = 123;
let x = if a <= b { // if-expression let x = if a <= b { // <- if-expression
b - a b - a
} else { } else {
a - b a - b

View File

@ -1,4 +1,4 @@
// This script runs an infinite loop, ending it with a break statement // This script runs an infinite loop, ending it with a break statement.
let x = 10; let x = 10;

View File

@ -1,3 +1,5 @@
// This script simulates multi-dimensional matrix calculations.
const SIZE = 50; const SIZE = 50;
fn new_mat(x, y) { fn new_mat(x, y) {

View File

@ -1,3 +1,5 @@
// This script imports an external script as a module.
import "loop" as x; import "loop" as x;
print(`Module test! foo = ${x::foo}`); print(`Module test! foo = ${x::foo}`);

View File

@ -1,3 +1,5 @@
// This script runs a single expression.
print("The result should be 46:"); print("The result should be 46:");
print(34 + 12); print(34 + 12);

View File

@ -1,3 +1,5 @@
// This script runs a complex expression.
print("The result should be 182:"); print("The result should be 182:");
let x = 12 + 34 * 5; let x = 12 + 34 * 5;

View File

@ -1,3 +1,5 @@
// This script runs a complex expression.
print("The result should be 230:"); print("The result should be 230:");
let x = (12 + 34) * 5; let x = (12 + 34) * 5;

View File

@ -1,5 +1,4 @@
// This script runs 1 million iterations // This script runs 1 million iterations to test the speed of the scripting engine.
// to test the speed of the scripting engine.
let now = timestamp(); let now = timestamp();
let x = 1_000_000; let x = 1_000_000;

View File

@ -1,4 +1,4 @@
// This script tests string operations // This script tests string operations.
print("hello"); print("hello");
print("this\nis \\ nice"); // escape sequences print("this\nis \\ nice"); // escape sequences

View File

@ -1,3 +1,5 @@
// This script tests object maps and strings.
print("Ready... Go!"); print("Ready... Go!");
let now = timestamp(); let now = timestamp();

View File

@ -1,13 +1,22 @@
// This script runs a switch statement in a for-loop.
let arr = [42, 123.456, "hello", true, "hey", 'x', 999, 1, 2, 3, 4]; let arr = [42, 123.456, "hello", true, "hey", 'x', 999, 1, 2, 3, 4];
for item in arr { for item in arr {
switch item { switch item {
// Match single integer
42 => print("The Answer!"), 42 => print("The Answer!"),
// Match single floating-point number
123.456 => print(`Floating point... ${item}`), 123.456 => print(`Floating point... ${item}`),
// Match single string
"hello" => print(`${item} world!`), "hello" => print(`${item} world!`),
// Match another integer
999 => print(`Got 999: ${item}`), 999 => print(`Got 999: ${item}`),
// Match range with condition
0..100 if item % 2 == 0 => print(`A small even number: ${item}`), 0..100 if item % 2 == 0 => print(`A small even number: ${item}`),
// Match another range
0..100 => print(`A small odd number: ${item}`), 0..100 => print(`A small odd number: ${item}`),
// Default case
_ => print(`Something else: <${item}> is ${type_of(item)}`) _ => print(`Something else: <${item}> is ${type_of(item)}`)
} }
} }

View File

@ -1,4 +1,4 @@
// This script runs a while loop // This script runs a while loop.
let x = 10; let x = 10;