Reserve $ symbol.

This commit is contained in:
Stephen Chung
2020-10-05 23:02:50 +08:00
parent 44f8d9e429
commit 1de44c7ecd
5 changed files with 76 additions and 52 deletions

View File

@@ -47,9 +47,13 @@ Constants propagation is used to remove dead code:
```rust
const ABC = true;
if ABC || some_work() { print("done!"); } // 'ABC' is constant so it is replaced by 'true'...
if true || some_work() { print("done!"); } // since '||' short-circuits, 'some_work' is never called
if true { print("done!"); } // <- the line above is equivalent to this
print("done!"); // <- the line above is further simplified to this
// because the condition is always true
```
@@ -84,13 +88,15 @@ Rhai guarantees that no external function will be run (in order not to trigger s
optimization process (unless the optimization level is set to [`OptimizationLevel::Full`]).
```rust
const DECISION = 1; // this is an integer, one of the standard types
// The following is most likely generated by machine.
if DECISION == 1 { // this is optimized into 'true'
const DECISION = 1; // this is an integer, one of the standard types
if DECISION == 1 { // this is optimized into 'true'
:
} else if DECISION == 2 { // this is optimized into 'false'
} else if DECISION == 2 { // this is optimized into 'false'
:
} else if DECISION == 3 { // this is optimized into 'false'
} else if DECISION == 3 { // this is optimized into 'false'
:
} else {
:
@@ -101,9 +107,9 @@ Because of the eager evaluation of operators for [standard types], many constant
and replaced by the result.
```rust
let x = (1+2)*3-4/5%6; // will be replaced by 'let x = 9'
let x = (1+2) * 3-4 / 5%6; // will be replaced by 'let x = 9'
let y = (1>2) || (3<=4); // will be replaced by 'let y = true'
let y = (1 > 2) || (3 < =4); // will be replaced by 'let y = true'
```
For operators that are not optimized away due to one of the above reasons, the function calls
@@ -116,12 +122,12 @@ const DECISION_1 = new_state(1);
const DECISION_2 = new_state(2);
const DECISION_3 = new_state(3);
if DECISION == 1 { // NOT optimized away because the operator is not built-in
: // and may cause side-effects if called!
if DECISION == 1 { // NOT optimized away because the operator is not built-in
: // and may cause side-effects if called!
:
} else if DECISION == 2 { // same here, NOT optimized away
} else if DECISION == 2 { // same here, NOT optimized away
:
} else if DECISION == 3 { // same here, NOT optimized away
} else if DECISION == 3 { // same here, NOT optimized away
:
} else {
: