Crates.io | scriptful |
lib.rs | scriptful |
version | 0.4.0 |
source | src |
created_at | 2020-01-25 00:13:51.954098 |
updated_at | 2022-08-09 09:36:06.692489 |
description | A minimalist, `no_std` stack machine library for interpreting domain specific interpreted languages. |
homepage | https://github.com/aesedepece/scriptful |
repository | https://github.com/aesedepece/scriptful |
max_upload_size | |
id | 201695 |
size | 93,426 |
Scriptful is a minimalist no_std
, zero dependency stack machine for interpreting scripts written with domain
specific interpreted languages.
This library is heavily inspired by the Forth programming language and Script (the scripting language in Bitcoin).
The whole library is built around these 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).enum
whose variants are all the possible data types allowed in a Stack
.Using this library is as easy as:
op_systems
module.Value
type system that comes bundled in the core::value
module.op_systems
module.use scriptful::prelude::*;
use scriptful::core::value::Value::*;
// You can define your own operators.
#[derive(Debug, PartialEq, Eq)]
enum MyOperator {
Add,
Equal,
Sub,
}
// An operator system decides what to do with the stack when each operator is applied on it.
fn my_operator_system(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(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(&my_operator_system);
// 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, Some(&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.