Giter Site home page Giter Site logo

rhaiscript / rhai Goto Github PK

View Code? Open in Web Editor NEW
3.5K 26.0 168.0 13.62 MB

Rhai - An embedded scripting language for Rust.

Home Page: https://crates.io/crates/rhai

License: Apache License 2.0

Rust 91.74% JavaScript 8.26%
scripting-engine embedded-scripting-language scripting-language no-std embedded webassembly wasm rhai

rhai's People

Contributors

alvinhochun avatar ekicyou avatar eliah-lakhin avatar ethanfrey avatar felixrabe avatar garypen avatar jhwgh1968 avatar l1npengtul avatar ltabis avatar luciusmagn avatar magnusja avatar mathieu-lala avatar mavethgh avatar nabijaczleweli avatar petrochenkov avatar quake avatar rustysec avatar schungx avatar silvergasp avatar sophiajt avatar stevedonovan avatar stevefan1999-personal avatar tamasfe avatar tguichaoua avatar timfish avatar torkleyy avatar udoprog avatar victorkoenders avatar wackbyte avatar zitsen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rhai's Issues

More than six arguments?

Having read how Rhai functions are dispatched, I don't quite get why there is a fixed number of arguments limit. Arguments are basically passed as a six-elements-long homogeneous tuple of Option<&mut Box<Any>> only to assume last N of them are None. Not only does it introduce a argument number limit but implies quite a lot of code repetition. Wouldn't it be better to use slices instead of tuples?

Of course fn_register needs heterogeneousity for native Rust functions, at least until variadic generics come into play. But as for now, why not introduce some macro to automate N-arguments-precisely problem? Then it would be trivial to write another macro to abstract it away just to the limit of N. And even then, any limit doesn't seem really rational for Rhai-defined functions as it doesn't depend on variadic generics (it's dynamic anyway).

threads

Implement basic threads:

// syntax is a to-do, too, a bit
//spawns a new thread with the identifier 'myname'. Thread names are optional
thread myname {
    stuff1();
    stuff2();
    etc();
}

join myname; //joins a thread, no-op if thread has already finished

let x = 5;
let y = 6;

// captures variables for a thread. Currently unsure if it should actually move them or just copy
// Not really sure about syntax here
thread |x, y| {
    print(x);
    print(y);
}

// all threads are joined when the main script thread is about to finish to prevent threads being stopped unexpectedly

Futures

Add futures to Rhai (exact design TODO)

boolean operators

Add boolean AND and OR:

let x = true && false; //false
let y = true || false; //true

Vectors

Would be nice to add vectors so that you can do more than single data values.

var x = [1, 2, 3];
print(x[1]);
x[0] = 4;

Similar to #5, this would need some way of handling assigning into an indexed position through possibly a setter.

Support anonymous functions

It would be great to have anonymous functions in Rhai, and be able to programmatically call them from Rust.

Something like:

let add = engine.eval::<Fn(i64, i64) -> i64>("fn (x,y) { x + y }").unwrap();
println!("{}", add(2,3));

more integer literals

Implement binary, hex and octal literals. Maybe allow _ for readability like in Rust:

let x = 0xaf; // 175
let x = 0b0101; // 5
let x = 0o77; //63
let x = 0b0011_1100_1010_0101; // 15525

binary operators

Implement the missing binary operators:

let rem = 10 % 4; // 2
let lsh = 10 << 4; // 160
let rsh = 10 >> 4; // 0
let and = 10 & 4; // 0
let or = 10 | 4; // 14
let xor = 10 ^ 4; // 14

Disabling debug messages?

Is there a way to disable the debug_msgs feature when using the crate?
I tried this

[dependencies.rhai]
version= "0.8.1"
default-features = false
features=[]

but I still get the messages

Documentation!()

Great job!
By simply looking the readme file, I found that your job seems very promissing, and I'll, just like many other people, ask you more documentation. I've looked into the project dates, and as I can see, you are changing it constantly: that's good. I really wanted to use your Script System, but I found a little bit hard with the current state of your examples(docs). It would be nice, if you create a document showing to the people the differences between Rhai to Rust. So if we study Rust, and later on, we can study your language! Presenting a table with the diffrences between the both (Rust and Rhai) may be enough.
Thanks and keep doing it!

compound assignment operators

Implement compound assignment operators:

let x = 5;
x += 2; // 7
x -= 1; // 6
x *= 3; // 18
x /= 2; // 9
x >>= 1; // 4
x <<= 2; // 16
x &= 31; // 16
x |= 74; // 90
x ^= 12; // 28

Return better error messages

Currently, Rhai doesn't track source positions so errors have no location information. Additionally, it doesn't track the names of where errors occurred (eg: Variable not found vs Variable 'a' not found).

Comments Syntax

Would be really nice to be able to write comments in a script :)

I guess something like:

// some comment

or...

# another comment

Let me know if you need more info.

'Raise to the power of' operator

It'd be nice to have a 'raise to the power of' operator:

let b = 2 ~ 2; // -> 4
let c = 4 ~ 3; // -> 64

As far as operator precendence goes, I reckon it should be at the same level as multiplication/division & modulo.

Support dot operator

It'd be nice to support the dot operator:

For example:

var x = get_object();
x.y = 3;
print(x.z);

For this to work, we may have to add support for getters and setters. This needs a spec that fits with the "no unsafe code" part of the design.

Associativity for Dot Operator

First, I must say - excellent project!. But I'm puzzled by this comment in parser.rs

            } else if curr_prec >= 100 {
                // Always bind right to left for precedence over 100
                rhs = try!(parse_binop(input, curr_prec, rhs));
            }

The only such operator is Dot, and Dot associativity must be left to right (a.b).c, not a.(b.c)
If this is resolved, then I suspect that the engine can be simplified, since there are several special-cases for the dot operator.

Tighten parser

The parser is currently pretty loosey-goosey about strictness to any particular language design. For example, you can elide semicolons between adjacent statements. This makes for some confusing issues when a script refuses to run and it's because you missed something that's hard to see.

Before 1.0, it'd be nice to have a pretty straightforward set of grammar rules that are enforced by the parser.

Operators for string

I want infix operators for string to compare("==") and concat.

Rust have no concat op. for &str or String, but they are useful in other languages.
Fortran: s = x // y
Perl: $s = $x . $y
Java: s = x + y
Lua: s = x .. y
VisualBasic: s = x & y

Would you consider to implement it in Rhai?

Easy interface for calling Rhai functions from Rust

Calling Rust functions from Rhai is simple and easy, the other way around, not so much. The way I see it, we need a macro or two. Therefore, we need to:

  1. Make call_fn() public (Already done)
  2. Make a macro like call!() for calling Rhai functions
  3. Document everything

However, this open to discussion, as there might be better ways to handle function calling from Rhai

Ranges, rustic for loop

Implement ranges and a for loop like the one in Rust:

for i in 0..10 {
    print(i);
}

let x = 1..5;

Advanced threads and actors

Turn threads into possible expressions, an extremely handy feature. Possibly introduce actors from actress for communication with other threads during their execution:

// thread syntax TBD
let mynum = spawn || {
    stuff1();
    stuff2();
    etc();
    4
}

// Do other stuff
let x = 4 + mynum; // mynum is joined now and it's return value is taken
// after this point mynum is a regular integer variable
// mynum.join() for manual joining, noop if already joined

debug statements

Let's have some nice solution for tracing information from rhai when we need to debug. Current debug messeges are only a temporary solution

Question: How to bring references into scope?

Hi,

I have a problem: I'm trying to use rhai to implement a filter type for my imag library, but I fail to bring an object into scope which I have as reference. Here's the codebase: https://github.com/matthiasbeyer/imag/pull/371/files

Note that the Filter trait provides a function filter which gets a &Entry. I need to bring this reference into scope of rhai, but I fail to do so. As you can see I added a wrapper type, but it doesn't help.

Maybe you have an idea?

loop

Implement a basic infinite loop like loop in Rust:

loop {
    // do stuff
    break;
}

comments

Implement comments, possibly through very basic preprocessor, rather than by editing the parser. Through thorough discussion, we have settled on Rust/C/C++ style comments:

// I am a single-line comment
/* I am a multiline
   comment with multiple lines
   and a  /* nested comment */
*/

How to inject values into my script

Following usecase:

I have users input rhai script. I want some environment to be set up for every invocation of the script (Lets think of a global variable that should be available when the script is run).

How do I do that?

Return values from loops

A while ago, Rust introduced yet another amazing feature:

let x = loop {
    // complicated computations
    break 7;
};

assert_eq!(x, 7);

Would be cool if Rhai had it too. Maybe even for while & for (when it comes around)

Support more control structures

Would be nice to have a for loop and/or a for..in.. loop. Right now the control structure story in Rhai is a bit thin, and before 1.0 it'd be nice to fill it out.

Parse error against negative sign

Parser cannot seem to handle negative sign.
Two evals below print "Can't parse: Minus" in stdout, and ErrorFunctionArgMismatch is returned.

engine.eval::<i64>("var n = -1; n");
engine.eval::<i64>("-1+5");

How to use with no-std

I'm trying out rust on a cheap microcontroller following this tutorial https://polyfractal.com/post/rustl8710/

The tutorial works great and the sample echo program runs over serial.

I'd love to add in a rust scripting language and I really like the design of Rhai. But when I add the library, the compile fails because of std is missing.

info: using existing install for 'nightly-x86_64-unknown-linux-gnu'
info: override toolchain for '/home/tim/rustl8710/src/rust' set to 'nightly-x86_64-unknown-linux-gnu'

  nightly-x86_64-unknown-linux-gnu unchanged - rustc 1.16.0-nightly (4ce7accaa 2017-01-17)

cd src/rust && xargo build --target thumbv7m-none-eabi
   Compiling rhai v0.4.0
error[E0463]: can't find crate for `std`
  |
  = note: the `thumbv7m-none-eabi` target may not be installed

I'm fairly new to rust still and am not quite clear on what this all means. Is Rhai meant to be used in such embedded use cases? It sure would be neat if it or something like it did work.

no method named `register_fn` found for type `rhai::Engine` in the current scope

I'm getting the above error on the following source code. I ran cargo doc and the function seems to exist so probably a silly mistake on my part and not a bug, but I can't figure out what's going wrong.

extern crate rhai;
use rhai::Engine;

fn add(x: i64, y: i64) -> i64 {
    x + y
}

fn main() {
    let mut engine = Engine::new();
    engine.register_fn("add", add); // "no method named `register_fn` found for type `rhai::Engine` in the current scope"
}

Is it dead?

The last commit was nearly a year ago. I really like the Rhai syntax and the way to use it from Rust, but I don't want to use a dead project...

Fill out array support

Basic array support is in, but it's not complete. Need to be able to do more complex expressions like:

x[0].update();
x[1].g;
x[2].f = 4;

[RFC] Consider adding a `Value` type

It might be worth adding an enum called Value and replace the current Box<Any> with that. That would allow us to have primitives and user data, while the former one is cheaper.

One related question is whether we want to encode basic types into the language. Currently, e.g. arrays are special cased while integers and floats are partially special cased (they don't define basic operators except through register_default_lib).

One motivation I see for that is that currently the integer type is i64 which isn't very obvious at first. If you're trying to index an array by u64, that would fail. Looking at issues like #52 or #32 , having special types that we handle in another way would make these things easier.

A disadvantage of encoding basic types and operations into the language would be that it would make the language and / or the library more complex.

I am myself unsure which way to go here.

floating point literals

Parse floating point numbers. Rhai already supports them, but has no way of actually creating them:

let x = 5.00001;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.