A customizable interpreted microlanguage designed to run untrusted code.
Project description
Scrolls
Scrolls is a small interpreter originally designed to allow users of my discord bots to make custom commands. It prioritizes control over the interpreter to help prevent abuse, while still allowing tight integration with python code.Links
- Documentation: https://a-bison.github.io/scrolls-py-docs/
- Source Code: https://github.com/a-bison/scrolls-py
- PyPI: https://pypi.org/project/scrolls-py/
Why?
The two other candidates for user scripts were python and Lua. Python code is a nightmare to sandbox properly, and the available Lua interpreters for python didn't give me the kind of control I wanted over the interpreter. In addition, Lua was a bit much for simple custom commands. So, I made my own interpreter.
There is a scripting language available for Rust called Rhai with a similar concept.
Also, I just kinda wanted to try making an interpreted language...
Goals
- Allow the developer to prevent abuse.
- Integrate tightly with the parent python application.
- Keep the syntax as simple as possible.
Getting Started
Check out the Links section above.
Installing
Scrolls may be installed through pip
:
Linux
python3 -m pip install scrolls-py
Windows
py -3 -m pip install scrolls-py
Command Line Usage
The module comes with a built-in interpreter that may be invoked by executing the module. (Note that the linux version will be used from now on.)
python3 -m scrolls FILE
Try running some of the examples:
python3 -m scrolls ./examples/arithmetic.scrl
If no file is specified, the interpreter will run in interactive mode:
python3 -m scrolls
Example Code
Here are some sample programs showcasing the basic syntax of Scrolls. See the examples directory for more.
!def(divisible? a b) {
return $(eq? 0 $(% $a $b))
}
!for(n in $^(rangev 1 101)) {
!if($(divisible? $n 15)) {
print "FizzBuzz"
} !elif($(divisible? $n 3)) {
print "Fizz"
} !elif($(divisible? $n 5)) {
print "Buzz"
} !else {
print $n
}
}
Nth Fibonacci Number
print "This will calculate fib N where fib 0 = 0, fib 1 = 1"
print "Enter N."
input n
set output_msg "Fibonacci number" $n "is"
!if($(< $n 2)) {
print $^output_msg $n
stop
}
set fib_prev 0
set fib 1
set i 2
!while($(<= $i $n)) {
set tmp $fib_prev
set fib_prev $fib
set fib $(+ $tmp $fib)
set i $(+ $i 1)
}
print $^output_msg $fib
Syntax Quirks
# In scrolls everything is a string
"print" "Everything is a string:"
!"for"("operator" "in" "+" "-" "*" "/") {
"print" $($"operator" "5" "8")
}
String Escapes
print "\"test escapes\"\n\ttabbed\n\tlines\nunicode is supported: \u0398hello\u0398\n"
Programmatic Usage
Scrolls may be embedded into any python program:
import scrolls
# Create an interpreter. Note that an interpreter created this
# way will not actually do anything. It's the responsibility of
# the user to configure with the desired language features.
interpreter = scrolls.Interpreter()
# Configure the interpreter with the base language.
# scrolls.base_config is provided to make this common task
# a bit easier.
scrolls.base_config.configure(interpreter)
# Configure with stdio commands like input, and print
interpreter.command_handlers.add(scrolls.StdIoCommandHandler())
# Run your script.
script = """
print "Please enter your name:"
input name
!repeat(4) {
print "Hello," $(cat $name "!")
}
"""
interpreter.run(script)
Known Issues
Interactive Control Calls
Multiple control calls without an explicit command separator will break parsing
in interactive mode. For example, the following wrongly identifies else
as a
command call:
>>> !if($false) { print "from if" } !else() { print "from else" }
error:
0 !if($false) { print "from if" } !else() { print "from else" }
^
line 0: Command_call 'else' not found.
A workaround for this is to enter them one line at a time:
>>> !if($false) { print "from if" }
>>> !else() { print "from else" }
from else
Or use a semicolon:
>>> !if($false) { print "from if" }; !else() { print "from else" }
from else
This bug does not affect the parsing of whole scripts.
Acknowledgements
- hikari-lightbulb by tandemdude, which inspired the CallBase extension (see here).
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file scrolls-py-0.4.0.tar.gz
.
File metadata
- Download URL: scrolls-py-0.4.0.tar.gz
- Upload date:
- Size: 50.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d67ff26be327f45c814564651198712ca2f53ac55f44742367e9a379a010c2a |
|
MD5 | f4b15d63173c3133ec2afb1f6f8a43e6 |
|
BLAKE2b-256 | 59bd6bfd35120f5dee825100d6270e30c744a7b99f7116a1d8256395769d96e6 |
File details
Details for the file scrolls_py-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: scrolls_py-0.4.0-py3-none-any.whl
- Upload date:
- Size: 56.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6658ff1f00e21e5c2b52e7ce89ca6a99c95a25ce4e3cbdda660e8efe4bf310ab |
|
MD5 | d66263cc954827d09480b41bdc892fdb |
|
BLAKE2b-256 | e8b81b1e419f98f598eb6a5005f2fd7bec745078dff70a7edacb7bd6d9e20a50 |