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

@ -8,7 +8,7 @@ Literals Syntax
| `INT` | decimal: `42`, `-123`, `0`<br/>hex: `0x????..`<br/>binary: `0b????..`<br/>octal: `0o????..` | | `INT` | decimal: `42`, `-123`, `0`<br/>hex: `0x????..`<br/>binary: `0b????..`<br/>octal: `0o????..` |
| `FLOAT` | `42.0`, `-123.456`, `0.0` | | `FLOAT` | `42.0`, `-123.456`, `0.0` |
| [String] | `"... \x?? \u???? \U???????? ..."` | | [String] | `"... \x?? \u???? \U???????? ..."` |
| Character | single: `'?'`<br/>ASCII hex: `'\x??'`<br/>Unicode: `'\u????'`, `'\U????????'` | | [Character][string] | single: `'?'`<br/>ASCII hex: `'\x??'`<br/>Unicode: `'\u????'`, `'\U????????'` |
| [`Array`] | `[ ???, ???, ??? ]` | | [`Array`] | `[ ???, ???, ??? ]` |
| [Object map] | `#{ a: ???, b: ???, c: ???, "def": ??? }` | | [Object map] | `#{ a: ???, b: ???, c: ???, "def": ??? }` |
| Boolean true | `true` | | Boolean true | `true` |

View File

@ -7,46 +7,62 @@ Operators and Symbols
Operators Operators
--------- ---------
| Operator | Description | Binary? | Binding direction | | Operator | Description | Binary? | Binding direction |
| :---------------: | ------------------------------ | :-----: | :---------------: | | :-----------------------------------------------------------------------------------------: | -------------------------------------- | :--------: | :---------------: |
| `+` | add | yes | left | | `+` | add | yes | left |
| `-` | subtract, Minus | yes/no | left | | `-` | 1) subtract<br/>2) negative | yes<br/>no | left<br/>right |
| `*` | multiply | yes | left | | `*` | multiply | yes | left |
| `/` | divide | yes | left | | `/` | divide | yes | left |
| `%` | modulo | yes | left | | `%` | modulo | yes | left |
| `~` | power | yes | left | | `~` | power | yes | left |
| `>>` | right bit-shift | yes | left | | `>>` | right bit-shift | yes | left |
| `<<` | left bit-shift | yes | left | | `<<` | left bit-shift | yes | left |
| `&` | bit-wise _And_, boolean _And_ | yes | left | | `&` | 1) bit-wise _And_<br/>2) boolean _And_ | yes | left |
| <code>\|</code> | bit-wise _Or_, boolean _Or_ | yes | left | | <code>\|</code> | 1) bit-wise _Or_<br/>2) boolean _Or_ | yes | left |
| `^` | bit-wise _Xor_, boolean _Xor_ | yes | left | | `^` | 1) bit-wise _Xor_<br/>2) boolean _Xor_ | yes | left |
| `==` | equals to | yes | left | | `=`, `+=`, `-=`, `*=`, `/=`,<br/>`~=`, `%=`, `<<=`, `>>=`, `&=`,<br/><code>\|=</code>, `^=` | assignments | yes | right |
| `~=` | not equals to | yes | left | | `==` | equals to | yes | left |
| `>` | greater than | yes | left | | `~=` | not equals to | yes | left |
| `>=` | greater than or equals to | yes | left | | `>` | greater than | yes | left |
| `<` | less than | yes | left | | `>=` | greater than or equals to | yes | left |
| `<=` | less than or equals to | yes | left | | `<` | less than | yes | left |
| `&&` | boolean _And_ (short-circuits) | yes | left | | `<=` | less than or equals to | yes | left |
| <code>\|\|</code> | boolean _Or_ (short-circuits) | yes | left | | `&&` | boolean _And_ (short-circuits) | yes | left |
| `!` | boolean _Not_ | no | left | | <code>\|\|</code> | boolean _Or_ (short-circuits) | yes | left |
| `[` .. `]` | indexing | yes | right | | `!` | boolean _Not_ | no | left |
| `.` | property access, method call | yes | right | | `[` .. `]` | indexing | yes | right |
| `.` | 1) property access<br/>2) method call | yes | right |
Symbols Symbols and Patterns
------- --------------------
| Symbol | Description | | Symbol | Name | Description |
| ------------ | ------------------------ | | ---------------------------------- | :------------------: | ------------------------------------- |
| `:` | property value separator | | `;` | semicolon | statement separator |
| `::` | module path separator | | `,` | comma | list separator |
| `#` | _reserved_ | | `:` | colon | [object map] property value separator |
| `=>` | _reserved_ | | `::` | path | module path separator |
| `->` | _reserved_ | | `#{` .. `}` | hash map | [object map] literal |
| `<-` | _reserved_ | | `"` .. `"` | double quote | [string] |
| `===` | _reserved_ | | `'` .. `'` | single quote | [character][string] |
| `!==` | _reserved_ | | `\` | escape | escape character literal |
| `:=` | _reserved_ | | `(` .. `)` | parentheses | expression grouping |
| `::<` .. `>` | _reserved_ | | `{` .. `}` | braces | block statement |
| `@` | _reserved_ | | <code>\|</code> .. <code>\|</code> | pipes | closure |
| `(*` .. `*)` | _reserved_ | | `[` .. `]` | brackets | [array] literal |
| `!` | bang | function call in calling scope |
| `//` | comment | line comment |
| `/*` .. `*/` | comment | block comment |
| `(*` .. `*)` | comment | _reserved_ |
| `<` .. `>` | angular brackets | _reserved_ |
| `#` | hash | _reserved_ |
| `@` | at | _reserved_ |
| `$` | dollar | _reserved_ |
| `=>` | double arrow | _reserved_ |
| `->` | arrow | _reserved_ |
| `<-` | left arrow | _reserved_ |
| `===` | strict equals to | _reserved_ |
| `!==` | strict not equals to | _reserved_ |
| `:=` | assignment | _reserved_ |
| `::<` .. `>` | turbofish | _reserved_ |

View File

@ -47,9 +47,13 @@ Constants propagation is used to remove dead code:
```rust ```rust
const ABC = true; const ABC = true;
if ABC || some_work() { print("done!"); } // 'ABC' is constant so it is replaced by '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 || some_work() { print("done!"); } // since '||' short-circuits, 'some_work' is never called
if true { print("done!"); } // <- the line above is equivalent to this if true { print("done!"); } // <- the line above is equivalent to this
print("done!"); // <- the line above is further simplified to this print("done!"); // <- the line above is further simplified to this
// because the condition is always true // 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`]). optimization process (unless the optimization level is set to [`OptimizationLevel::Full`]).
```rust ```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 { } else {
: :
@ -101,9 +107,9 @@ Because of the eager evaluation of operators for [standard types], many constant
and replaced by the result. and replaced by the result.
```rust ```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 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_2 = new_state(2);
const DECISION_3 = new_state(3); const DECISION_3 = new_state(3);
if DECISION == 1 { // NOT optimized away because the operator is not built-in if DECISION == 1 { // NOT optimized away because the operator is not built-in
: // and may cause side-effects if called! : // 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 { } else {
: :

View File

@ -26,8 +26,8 @@ more control over what a script can (or cannot) do.
| `no_closure` | no | disables [capturing][automatic currying] external variables in [anonymous functions] to simulate _closures_, or [capturing the calling scope]({{rootUrl}}/language/fn-capture.md) in function calls | | `no_closure` | no | disables [capturing][automatic currying] external variables in [anonymous functions] to simulate _closures_, or [capturing the calling scope]({{rootUrl}}/language/fn-capture.md) in function calls |
| `no_std` | no | builds for `no-std` (implies `no_closure`). Notice that additional dependencies will be pulled in to replace `std` features | | `no_std` | no | builds for `no-std` (implies `no_closure`). Notice that additional dependencies will be pulled in to replace `std` features |
| `serde` | yes | enables serialization/deserialization via `serde`. Notice that the [`serde`](https://crates.io/crates/serde) crate will be pulled in together with its dependencies | | `serde` | yes | enables serialization/deserialization via `serde`. Notice that the [`serde`](https://crates.io/crates/serde) crate will be pulled in together with its dependencies |
| `internals` | yes | exposes internal data structures (e.g. [`AST`] nodes). Beware that Rhai internals are volatile and may change from version to version |
| `unicode-xid-ident` | no | allows [Unicode Standard Annex #31](http://www.unicode.org/reports/tr31/) as identifiers | | `unicode-xid-ident` | no | allows [Unicode Standard Annex #31](http://www.unicode.org/reports/tr31/) as identifiers |
| `internals` | yes | exposes internal data structures (e.g. [`AST`] nodes). Beware that Rhai internals are volatile and may change from version to version |
Example Example

View File

@ -1391,6 +1391,8 @@ fn get_next_token_inner(
('@', _) => return Some((Token::Reserved("@".into()), start_pos)), ('@', _) => return Some((Token::Reserved("@".into()), start_pos)),
('$', _) => return Some((Token::Reserved("$".into()), start_pos)),
('\0', _) => unreachable!(), ('\0', _) => unreachable!(),
(ch, _) if ch.is_whitespace() => (), (ch, _) if ch.is_whitespace() => (),