2021-07-16 18:56:45 -05:00
|
|
|
use std::{cell::RefCell, fmt::Display, io::Write, rc::Rc};
|
2021-05-20 18:18:01 -05:00
|
|
|
|
2021-04-18 16:40:41 -05:00
|
|
|
use rand::Rng;
|
2021-04-13 18:01:19 -05:00
|
|
|
|
2021-07-16 18:56:45 -05:00
|
|
|
use crate::{ast::Stmt, consts};
|
2021-06-07 16:18:07 -05:00
|
|
|
|
2021-04-18 16:40:41 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum Abool {
|
|
|
|
Never = -1,
|
|
|
|
Sometimes = 0,
|
|
|
|
Always = 1,
|
|
|
|
}
|
|
|
|
|
2021-05-20 18:18:01 -05:00
|
|
|
impl Display for Abool {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Abool::Never => write!(f, "never"),
|
|
|
|
Abool::Sometimes => write!(f, "sometimes"),
|
|
|
|
Abool::Always => write!(f, "always"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-02 10:38:12 -05:00
|
|
|
impl From<Abool> for bool {
|
|
|
|
fn from(val: Abool) -> Self {
|
|
|
|
match val {
|
2021-04-18 16:40:41 -05:00
|
|
|
Abool::Never => false,
|
|
|
|
Abool::Always => true,
|
2021-05-03 19:33:21 -05:00
|
|
|
Abool::Sometimes => rand::thread_rng().gen(), // NOTE(Able): This is amazing and should be applied anywhere abooleans exist
|
2021-04-18 16:40:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 16:18:07 -05:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2021-06-02 15:29:31 -05:00
|
|
|
pub enum Functio {
|
2021-06-12 09:26:21 -05:00
|
|
|
BfFunctio {
|
|
|
|
instructions: Vec<u8>,
|
|
|
|
tape_len: usize,
|
|
|
|
},
|
2021-06-12 22:07:58 -05:00
|
|
|
AbleFunctio {
|
|
|
|
params: Vec<String>,
|
|
|
|
body: Vec<Stmt>,
|
|
|
|
},
|
2021-06-02 15:29:31 -05:00
|
|
|
}
|
|
|
|
|
2021-06-07 16:18:07 -05:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2021-04-18 09:39:43 -05:00
|
|
|
pub enum Value {
|
2021-06-02 15:29:31 -05:00
|
|
|
Nul,
|
2021-04-13 18:01:19 -05:00
|
|
|
Str(String),
|
|
|
|
Int(i32),
|
|
|
|
Bool(bool),
|
2021-04-18 16:40:41 -05:00
|
|
|
Abool(Abool),
|
2021-06-02 15:29:31 -05:00
|
|
|
Functio(Functio),
|
2021-04-13 18:01:19 -05:00
|
|
|
}
|
|
|
|
|
2021-06-02 18:20:30 -05:00
|
|
|
impl Value {
|
2021-07-22 22:08:04 -05:00
|
|
|
/// Write an AbleScript value to a Brainfuck input stream by
|
|
|
|
/// coercing the value to an integer, then truncating that integer
|
|
|
|
/// to a single byte, then writing that byte. This should only be
|
|
|
|
/// called on `Write`rs that cannot fail, e.g., `Vec<u8>`, because
|
|
|
|
/// any IO errors will cause a panic.
|
2021-06-12 22:07:58 -05:00
|
|
|
pub fn bf_write(&self, stream: &mut impl Write) {
|
2021-07-22 22:08:04 -05:00
|
|
|
stream
|
|
|
|
.write_all(&[self.clone().into_i32() as u8])
|
|
|
|
.expect("Failed to write to Brainfuck input");
|
2021-06-02 18:20:30 -05:00
|
|
|
}
|
2021-06-07 19:57:44 -05:00
|
|
|
|
2021-07-16 18:56:45 -05:00
|
|
|
/// Coerce a value to an integer.
|
|
|
|
pub fn into_i32(self) -> i32 {
|
2021-06-07 19:57:44 -05:00
|
|
|
match self {
|
2021-07-16 18:56:45 -05:00
|
|
|
Value::Abool(a) => a as _,
|
|
|
|
Value::Bool(b) => b as _,
|
|
|
|
Value::Functio(func) => match func {
|
|
|
|
Functio::BfFunctio {
|
|
|
|
instructions,
|
|
|
|
tape_len,
|
|
|
|
} => (instructions.len() + tape_len) as _,
|
|
|
|
Functio::AbleFunctio { params, body } => (params.len() + body.len()) as _,
|
|
|
|
},
|
|
|
|
Value::Int(i) => i,
|
|
|
|
Value::Nul => consts::ANSWER,
|
|
|
|
Value::Str(text) => text.parse().unwrap_or(consts::ANSWER),
|
2021-06-07 19:57:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Coerce a Value to a boolean. The conversion cannot fail.
|
2021-06-16 10:35:06 -05:00
|
|
|
pub fn into_bool(self) -> bool {
|
2021-06-07 19:57:44 -05:00
|
|
|
match self {
|
|
|
|
Value::Abool(b) => b.into(),
|
2021-07-16 18:56:45 -05:00
|
|
|
Value::Bool(b) => b,
|
2021-06-07 19:57:44 -05:00
|
|
|
Value::Functio(_) => true,
|
2021-07-16 18:56:45 -05:00
|
|
|
Value::Int(x) => x != 0,
|
2021-06-07 19:57:44 -05:00
|
|
|
Value::Nul => true,
|
2021-07-16 18:56:45 -05:00
|
|
|
Value::Str(s) => !s.is_empty(),
|
2021-06-07 19:57:44 -05:00
|
|
|
}
|
|
|
|
}
|
2021-06-02 18:20:30 -05:00
|
|
|
}
|
|
|
|
|
2021-05-20 18:18:01 -05:00
|
|
|
impl Display for Value {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2021-06-02 15:29:31 -05:00
|
|
|
Value::Nul => write!(f, "nul"),
|
2021-05-20 18:18:01 -05:00
|
|
|
Value::Str(v) => write!(f, "{}", v),
|
|
|
|
Value::Int(v) => write!(f, "{}", v),
|
|
|
|
Value::Bool(v) => write!(f, "{}", v),
|
|
|
|
Value::Abool(v) => write!(f, "{}", v),
|
2021-06-02 15:29:31 -05:00
|
|
|
Value::Functio(v) => match v {
|
2021-06-12 22:07:58 -05:00
|
|
|
Functio::BfFunctio {
|
|
|
|
instructions,
|
|
|
|
tape_len,
|
|
|
|
} => {
|
2021-06-02 15:29:31 -05:00
|
|
|
write!(
|
|
|
|
f,
|
2021-06-12 09:26:21 -05:00
|
|
|
"({}) {}",
|
|
|
|
tape_len,
|
|
|
|
String::from_utf8(instructions.to_owned())
|
2021-06-02 15:29:31 -05:00
|
|
|
.expect("Brainfuck functio source should be UTF-8")
|
|
|
|
)
|
|
|
|
}
|
2021-06-12 22:07:58 -05:00
|
|
|
Functio::AbleFunctio { params, body } => {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"({}) -> {:?}",
|
|
|
|
params.join(", "),
|
|
|
|
// Maybe we should have a pretty-printer for
|
|
|
|
// statement blocks at some point?
|
|
|
|
body,
|
|
|
|
)
|
2021-06-02 15:29:31 -05:00
|
|
|
}
|
|
|
|
},
|
2021-05-20 18:18:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 18:01:19 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Variable {
|
2021-05-20 18:18:01 -05:00
|
|
|
pub melo: bool,
|
2021-06-13 12:06:38 -05:00
|
|
|
|
|
|
|
// Multiple Variables can reference the same underlying Value when
|
|
|
|
// pass-by-reference is used, therefore we use Rc here.
|
|
|
|
pub value: Rc<RefCell<Value>>,
|
2021-04-27 06:48:56 -05:00
|
|
|
}
|