Scriptful is a minimalistic no_std
stack machine for executing domain specific interpreted languages.
This library is heavily inspired by [FORTH] and Script, the scripting language in Bitcoin.
The whole library is built around four main concepts:
Value
(a piece of data to be pushed into the stack) or an Operator
(the descriptor for an action that operates on the topmost items in the stack).Using this library is as easy as:
op_systems
module.op_systems
module.```rust use scriptful::prelude::; use scriptful::prelude::Value::;
/// You can define your own operators. enum MyOperator { Add, Equal, Sub, }
/// An operator system decides what to do with the stack when each operator is /// applied on it. fn myoperatorsystem(stack: &mut Stack, operator: &MyOperator) { match operator { MyOperator::Add => { let a = stack.pop(); let b = stack.pop(); stack.push(a + b); } MyOperator::Equal => { let a = stack.pop(); let b = stack.pop(); stack.push(Value::Boolean(a == b)); } MyOperator::Sub => { let a = stack.pop(); let b = stack.pop(); stack.push(a - b); } } }
// Instantiate the machine with a reference to your operator system. let mut machine = Machine::new(&myoperatorsystem);
// Run a script that simply adds 1
and 2
let result = machine.run_script(&[
Item::Value(Integer(1)),
Item::Value(Integer(2)),
Item::Operator(MyOperator::Add),
]);
// The result should unsurprisingly be 3
assert_eq!(*result, Integer(3));
```
Scriptful is distributed under the terms of both the MIT license and the Apache License (Version 2.0).
See [LICENSE-APACHE] and [LICENSE-MIT], and [COPYRIGHT] for details.