Crate tinyscript

Crate tinyscript 

Source
Expand description

§tinyscript - A small, C-like scripting language

tinyscript is considered to ba a superset of the scripting language defined in BehviorTree.CPP.

The implementation follows the pattern of clox as described in Part III of crafting interpreters

§Usage

use tinyscript::{Runtime, environment::DefaultEnvironment};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut runtime = Runtime::default();
    let mut env = DefaultEnvironment::default();
    let value = runtime.run("2 * 2 == 4", &mut env)?;
    Ok(())
}

§License

Licensed under either of

  • Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  • MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.

§Contribution

Any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above, without any additional terms or conditions.

§Data types

TypeExamples
Booleantrue/false
String‘hello world’
EnumRED, GREEN, BLUE
Numbers:
Integer42
Hexadecimal0x01
Float3.14

Note: under the hood an Enum is always interpreted as its integer value.

§Boolean Values

Boolean values can be one of true or false.

Setting booleans:

val_a = true
val_b := !false

The logical ! works with boolean literals. !false is the equivalent of true. val_a and val_b above are equivalent.

§Strings

Strings are enclosed by ‘…’.

§Enums

Enums must be registered to the Runtime before they can be used.

§Numbers

§Negative numbers
OperatorDescription
~Negate

§Statements

Examples:

print 42
print 42;
42; print 42; variable:= 42; (13 + 12)

Multiple statements in a single script are separated by a semicolon. The last statements may or may not end with a semicolon.

§Assignment operators

Examples:

var_a := 42
var_b = 3.14
message = 'hello world'
  • The first line assigns the number 42 to the variable var_a.
  • The second line assigns the number 3.14 to the variable var_b.
  • The third line assigns the string “hello world” to the variable message.

§Arithmetic operators and parenthesis

Examples:

var_a := 7
var_b := 5
var_b *= 2
var_c := (var_a * 3) + var_b

The resulting values of var_a is 10 and var_c is 31.

The following operators are supported:

OperatorAssign OperatorDescription
++=Add
--=Subtract
**=Multiply
//=Divide

These operators can be used only on Number data types, only the addition acan also be used on Strings.

§Bitwise operators

These operators work only on integer and hexadecimal numbers. Using them with a string or float number will cause an error.

Examples:

value:= 0x7F
val_a:= value & 0x0F
val_b:= value | 0xF0

The value of val_a is 0x0F (or 15); val_b is 0xFF (or 255).

OperatorDescription
|Bitwise or
&Bitwise and
^Bitwise xor

§Logic and comparison operators

Operators which return a boolean.

Example:

val_a := true
val_b := 5 > 3
val_c := (val_a == val_b)
val_d := (val_a && val_b) || !val_c
OperatorDescription
&&Logic and
||Logic or
!Negation
==Equality
!=Inequality
<Less
<=Less equal
>Greater
>=Greater equal

§Ternary operator if-then-else

Example:

val_b = (val_a > 1) ? 42 : 24

Re-exports§

pub use environment::DefaultEnvironment;
pub use environment::Environment;
pub use error::Error;
pub use error::Result;
pub use execution::Chunk;
pub use runtime::Runtime;
pub use runtime::SharedRuntime;
pub use scripting_value::ScriptingValue;

Modules§

compilation
Bytecode compiler implementation.
environment
A tratt to work with the outside world and a default implementation.
error
tinyscripts external errors, passes through the internal errors.
execution
Execution implementations.
runtime
A runtime for executing tinyscript code.
scripting_value
A universal ScriptingValue type.

Traits§

ScriptEnum
The trait for script enums.

Derive Macros§

ScriptEnum
Derive macro ScriptEnum. Enables a Rust enum to be used in a ‘C’ like mannner within the tinyscript language.