1 个不稳定版本
0.1.0 | 2022年5月22日 |
---|
#1110 在 编码
用于 serde_watson
70KB
2K SLoC
watson_rs
watson_rs是浪费但惊人的图灵不完整基于堆栈的对象表示法(WATSON)的Rust实现。
文档
用法
将以下内容添加到您的Cargo.toml
[dependencies]
watson_rs = "0.1.0"
示例
基本用法(使用serde_watson crate)
use std::io;
use serde::Serialize;
use serde_watson::ser::Serializer;
#[derive(Serialize)]
struct Transaction {
id: u64,
from: String,
to: String,
amount: f64,
}
fn main() -> serde_watson::Result<()> {
let tx = Transaction {
id: 0xabcd1234,
from: "Motoaki Tanigo".to_owned(),
to: "Oozora Subaru".to_owned(),
amount: 123.45,
};
let mut ser = Serializer::from_writer(io::stdout());
tx.serialize(&mut ser)?;
Ok(())
}
您可以通过在watson_example目录中运行cargo run --example basic
来运行上面的示例。
$ cargo run --example basic
Finished dev [unoptimized + debuginfo] target(s) in 0.08s
Running `target/debug/examples/basic`
~?SShkShaaakShaaaaakShaaaaaak-SShaakShaaaaakShaaaaaak-SShaakShaaaakShaaaaakShaaa
aaaaaakShaaaaaaaaaaaakShaaaaaaaaaaaaaaaakShaaaaaaaaaaaaaaaaaakShaaaaaaaaaaaaaaaa
aaakShaaaaaaaaaaaaaaaaaaaaaakShaaaaaaaaaaaaaaaaaaaaaaakShaaaaaaaaaaaaaaaaaaaaaaa
akShaaaaaaaaaaaaaaaaaaaaaaaaakShaaaaaaaaaaaaaaaaaaaaaaaaaaakShaaaaaaaaaaaaaaaaaa
aaaaaaaaaaakShaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaakig$BBubaBubbaBubbbbbaBubbbbbba!BBu
baBubbbbaBubbbbbaBubbbbbba!BBuaBubaBubbaBubbbaBubbbbbaBubbbbbba!BBuaBubbaBubbbaB
ubbbbbaBubbbbbba!?SShkShaakShaaakShaaaaaak-SShkShakShaakShaaakShaaaaakShaaaaaak-
SShaakShaaaakShaaaaakShaaaaaak-SShkShakShaakShaaakShaaaaakShaaaaaak-SShkShaaaaak
Shaaaaaak-SShkShakShaaakShaaaaakShaaaaaak-SShkShaaakShaaaaakShaaaaaak-SShaaaaak-
SShaakShaaaakShaaaaaak-SShkShaaaaakShaaaaaak-SShakShaakShaaakShaaaaakShaaaaaak-S
ShkShaaakShaaaaakShaaaaaak-SShkShakShaakShaaaaakShaaaaaak-SShkShakShaakShaaakSha
aaaakShaaaaaak-g$BBubbaBubbbbaBubbbbbaBubbbbbba!BBuaBubaBubbaBubbbaBubbbbbaBubbb
bbba!?SShkShakShaakShaaakShaaaaaak-SShkShakShaakShaaakShaaaaakShaaaaaak-SShakSha
aakShaaaakShaaaaakShaaaaaak-SShkShakShaakShaaakShaaaaakShaaaaaak-SShakShaaaakSha
aaaakShaaaaaak-SShkShaaaaakShaaaaaak-SShaaaaak-SShkShakShaaaakShaaaaaak-SShkShaa
kShaaaakShaaaaakShaaaaaak-SShakShaaaaakShaaaaaak-SShkShaaaaakShaaaaaak-SShakShaa
aakShaaaaakShaaaaaak-SShkShaakShaaaakShaaaaakShaaaaaak-g$BBuaBubbbbbaBubbbbbba!B
BuaBubbaBubbbaBubbbbbaBubbbbbba!BBuaBubaBubbaBubbbaBubbbbbaBubbbbbba!BBuaBubbaBu
bbbbaBubbbbbaBubbbbbba!BBubaBubbaBubbbaBubbbbbaBubbbbbba!BBubbaBubbbbaBubbbbbaBu
bbbbbba!BBuaBubbaBubbbaBubbbbbbaBubbbbbbbaBubbbbbbbbbbaBubbbbbbbbbbbaBubbbbbbbbb
bbbbbaBubbbbbbbbbbbbbbbaBubbbbbbbbbbbbbbbbbbaBubbbbbbbbbbbbbbbbbbbaBubbbbbbbbbbb
bbbbbbbbbbbaBubbbbbbbbbbbbbbbbbbbbbbbaBubbbbbbbbbbbbbbbbbbbbbbbbbbaBubbbbbbbbbbb
bbbbbbbbbbbbbbbbaBubbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaBubbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbaBubbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaBubbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaB
ubbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaBubbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
baBubbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaBubbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbaBubbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaBubbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbaBubbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaB
ubbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaBubbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbaBubbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaBu
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaBubbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbaBubbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbaiM
您可以使用WATSON CLI来验证输出。
$ cargo run --example basic | watson decode -t json
Finished dev [unoptimized + debuginfo] target(s) in 0.05s
Running `target/debug/examples/basic`
{"amount":123.45,"from":"Motoaki Tanigo","id":2882343476,"to":"Oozora Subaru"}
直接运行WATSON虚拟机
您可以通过此crate直接运行WATSON虚拟机。
use watson_rs::vm::VM;
use watson_rs::{Insn, Location, Token};
fn main() -> watson_rs::Result<()> {
let mut vm = VM::new();
// Instructions are defined in https://github.com/genkami/watson/blob/main/doc/spec.md.
vm.execute(token(Insn::Inew))?; // Push a signed integer `0` to the stack.
vm.execute(token(Insn::Iinc))?; // Increment a value on the top of the stack.
vm.execute(token(Insn::Ishl))?; // Shift a value on the top of the stack to the left by one bit.
println!("result: {:?}", vm.peek_top().unwrap());
Ok(())
}
fn token(insn: Insn) -> Token {
Token {
insn: insn,
location: Location::unknown(),
}
}
您可以通过在watson_example目录中运行cargo run --example run_vm
来运行上面的示例。
$ cargo run --example run_vm
Finished dev [unoptimized + debuginfo] target(s) in 0.12s
Running `target/debug/examples/run_vm`
result: Int(2)