Use impl Value rather than trait BfWriter

It probably makes more sense to be writing values with
`Value::bf_write` than to go for the complexity of using a trait to
allow `Writer::write_value` (which also looks ambiguous because it's
not immediately clear that a generic name like "write_value" relates
to BF or AbleScript).
This commit is contained in:
Alex Bethel 2021-06-02 18:20:30 -05:00
parent 326d0511e7
commit ce02aebd91
2 changed files with 58 additions and 66 deletions

View file

@ -205,10 +205,9 @@ impl ExecEnv {
Value::Functio(func) => { Value::Functio(func) => {
match func { match func {
Functio::BfFunctio(body) => { Functio::BfFunctio(body) => {
use crate::variables::BfWriter;
let mut input: Vec<u8> = vec![]; let mut input: Vec<u8> = vec![];
for arg in args { for arg in args {
input.write_value(&self.eval_expr(arg)?); self.eval_expr(arg)?.bf_write(&mut input);
} }
println!("input = {:?}", input); println!("input = {:?}", input);
let mut output = vec![]; let mut output = vec![];

View file

@ -50,6 +50,63 @@ pub enum Value {
Functio(Functio), Functio(Functio),
} }
impl Value {
/// Writes an AbleScript value to a Brainfuck input stream. This
/// should generally only be called on `Write`rs that cannot fail,
/// e.g., `Vec<u8>`, because any IO errors will cause a panic.
///
/// The mapping from values to encodings is as follows, where all
/// multi-byte integers are little-endian:
///
/// | AbleScript representation | Brainfuck representation |
/// |---------------------------|-------------------------------------------------------------|
/// | Nul | `00` |
/// | Str | `01` [length, 4 bytes] [string, \[LENGTH\] bytes, as UTF-8] |
/// | Int | `02` [value, 4 bytes] |
/// | Bool | `03` `00` false, `03` `01` true. |
/// | Abool | `04` `00` never, `04` `01` always, `04` `02` sometimes. |
/// | Brainfuck Functio | `05` `00` [length, 4 bytes] [source code, \[LENGTH\] bytes] |
/// | AbleScript Functio | `05` `01` (todo, not yet finalized or implemented) |
///
/// The existing mappings should never change, as they are
/// directly visible from Brainfuck code and modifying them would
/// break a significant amount of AbleScript code. If more types
/// are added in the future, they should be assigned the remaining
/// discriminant bytes from 06..FF.
pub fn bf_write(&mut self, stream: &mut impl Write) {
match self {
Value::Nul => stream.write_all(&[0]),
Value::Str(s) => stream
.write_all(&[1])
.and_then(|_| stream.write_all(&(s.len() as u32).to_le_bytes()))
.and_then(|_| stream.write_all(&s.as_bytes())),
Value::Int(v) => stream
.write_all(&[2])
.and_then(|_| stream.write_all(&v.to_le_bytes())),
Value::Bool(b) => stream
.write_all(&[3])
.and_then(|_| stream.write_all(&[*b as _])),
Value::Abool(a) => stream.write_all(&[4]).and_then(|_| {
stream.write_all(&[match *a {
Abool::Never => 0,
Abool::Sometimes => 2,
Abool::Always => 1,
}])
}),
Value::Functio(f) => stream.write_all(&[5]).and_then(|_| match f {
Functio::BfFunctio(f) => stream
.write_all(&[0])
.and_then(|_| stream.write_all(&(f.len() as u32).to_le_bytes()))
.and_then(|_| stream.write_all(&f)),
Functio::AbleFunctio(_) => {
todo!()
}
}),
}
.expect("Failed to write to Brainfuck input");
}
}
impl Display for Value { impl Display for Value {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
@ -119,70 +176,6 @@ impl From<Value> for bool {
} }
} }
/// Allows writing AbleScript values to Brainfuck.
///
/// This trait is blanket implemented for all `Write`rs, but should
/// typically only be used for `Write`rs that cannot fail, e.g.,
/// `Vec<u8>`, because any IO errors will cause a panic.
///
/// The mapping from values to encodings is as follows, where all
/// multi-byte integers are little-endian:
///
/// | AbleScript representation | Brainfuck representation |
/// |---------------------------|-----------------------------------------------------------|
/// | Nul | 00 |
/// | Str | 01 [length, 4 bytes] [string, \[LENGTH\] bytes, as UTF-8] |
/// | Int | 02 [value, 4 bytes] |
/// | Bool | 03 00 false, 03 01 true. |
/// | Abool | 04 00 never, 04 01 always, 04 02 sometimes. |
/// | Brainfuck Functio | 05 00 [length, 4 bytes] [source code, \[LENGTH\] bytes] |
/// | AbleScript Functio | 05 01 (todo, not yet finalized or implemented) |
///
/// The existing mappings should never change, as they are directly
/// visible from Brainfuck code and modifying them would break a
/// significant amount of AbleScript code. If more types are added in
/// the future, they should be assigned the remaining discriminant
/// bytes from 06..FF.
pub trait BfWriter {
/// Write a value. Panic if writing fails for any reason.
fn write_value(&mut self, value: &Value);
}
impl<T: Write> BfWriter for T {
fn write_value(&mut self, value: &Value) {
match value {
Value::Nul => self.write_all(&[0]),
Value::Str(s) => self
.write_all(&[1])
.and_then(|_| self.write_all(&(s.len() as u32).to_le_bytes()))
.and_then(|_| self.write_all(&s.as_bytes())),
Value::Int(v) => self
.write_all(&[2])
.and_then(|_| self.write_all(&v.to_le_bytes())),
Value::Bool(b) => self
.write_all(&[3])
.and_then(|_| self.write_all(&[*b as _])),
Value::Abool(a) => self.write_all(&[4]).and_then(|_| {
self.write_all(&[match *a {
Abool::Never => 0,
Abool::Sometimes => 2,
Abool::Always => 1,
}])
}),
Value::Functio(f) => self.write_all(&[5]).and_then(|_| match f {
Functio::BfFunctio(f) => self
.write_all(&[0])
.and_then(|_| self.write_all(&(f.len() as u32).to_le_bytes()))
.and_then(|_| self.write_all(&f)),
Functio::AbleFunctio(_) => {
todo!()
}
}),
}
.expect("Failed to write to Brainfuck input");
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct Variable { pub struct Variable {
pub melo: bool, pub melo: bool,