rhaiscript / rhai

Rhai - An embedded scripting language for Rust.
https://crates.io/crates/rhai
Apache License 2.0
3.79k stars 177 forks source link

rhai_repl: add readline support. #323

Closed stustd closed 3 years ago

stustd commented 3 years ago

I miss readline support in the REPL: scrolling through statement history is very convenient.

schungx commented 3 years ago

What do you mean? Something like !123 to recall certain lines?

Right now can you use the arrow keys to scroll through past lines? I can do this with Windows, not very sure with bash...

schungx commented 3 years ago

Alright, I tried it with bash on Linux and it doesn't seem to recognize the arrow keys. Not sure how I can add this feature, because I'm just reading from stdin... any pointers?

stustd commented 3 years ago

This probably will do it.

schungx commented 3 years ago

rhai-repl is intended to be an example only for tutorial purposes. Right now it is not constructed as a stand-alone tool.

In the future, rhai-repl and rhai-run may be promoted to full-blown tools, and they'll probably need to handle options etc.

In the meantime, I believe you can easily add rustyline to repl yourself...

stustd commented 3 years ago

That's true, I "can [..and might] easily add rustyline to repl" myself, but since they're in the master's src/bin directory I guessed they'd be promoted sooner rather than later.

schungx commented 3 years ago

I would hesitate to pull in another crate just for this tool... Maybe add new [[bin]] sections to Cargo.toml and put rustyline under the rhai-repl bin...

EDIT:: This doesn't look possible, so really it seems the only way to selectively add a dependency to a bin tool is to make it one of the projects inside the workspace...

Would you be nice enough to revise these two tools to make them proper? hint hint hint wink wink knudge knudge say no more...

stustd commented 3 years ago

I would hesitate to pull in another crate just for this tool... Maybe add new [[bin]] sections to Cargo.toml and put rustyline under the rhai-repl bin...

That would be the way.

Would you be nice enough to revise these two tools to make them proper? hint hint hint wink wink knudge knudge say no more...

If I had the time I would... (sorry to disappoint you...). However, I'm pretty familiar with clap (options parsing) and can quickly put this functionality together if I you can provide me with input what a rhai repl needs.

That said, at the moment my rhai work is focused on getting imported scripts compiled in a rhai top-level script; I followed your advise and it works. Is there a way to walk the AST so that I can retrieve all imports in a top-level script (this would avoid a separate imports.rhai script).

schungx commented 3 years ago

That said, at the moment my rhai work is focused on getting imported scripts compiled in a rhai top-level script; I followed your advise and it works. Is there a way to walk the AST so that I can retrieve all imports in a top-level script (this would avoid a separate imports.rhai script).

Not easily.. remember the import statements form part of your main script. You really need to evaluate the script in full, and then look at what modules are left behind.

Turn on the internals feature if you want to walk the AST (you'll be able to). Since you're only restricting yourself to import statements at the global (top) level, it is very easy... just do ast.statements() and it returns &[Stmt] which is a list of statements. Find if any of them is Stmt::Import.

However, getting rid of those import statements will require you to modify the AST itself... (you can use ast.statements_mut().

Or you can simply register a custom module resolver that silently returns.

schungx commented 3 years ago

Closing this for now since it doesn't seem possible to selectively add rustyline to repl as a bin tool without adding it to the entire lib.