2021-08-07 17:33:28 -05:00
|
|
|
use std::{
|
2021-09-01 10:46:17 -05:00
|
|
|
cell::RefCell, collections::HashMap, fmt::Display, hash::Hash, io::Write, mem::discriminant,
|
|
|
|
ops, rc::Rc, vec,
|
2021-08-07 17:33:28 -05:00
|
|
|
};
|
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-08-28 16:43:59 -05:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
|
2021-04-18 16:40:41 -05:00
|
|
|
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-08-07 17:33:28 -05:00
|
|
|
#[derive(Debug, PartialEq, Clone, Hash)]
|
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-08-28 16:27:35 -05:00
|
|
|
Eval(String),
|
2021-06-02 15:29:31 -05:00
|
|
|
}
|
|
|
|
|
2021-08-28 16:27:35 -05:00
|
|
|
pub type Cart = HashMap<Value, Rc<RefCell<Value>>>;
|
|
|
|
|
2021-07-29 13:29:23 -05:00
|
|
|
#[derive(Debug, 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-08-28 16:27:35 -05:00
|
|
|
Cart(Cart),
|
2021-07-29 13:29:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Hash for Value {
|
|
|
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
2021-08-07 17:33:28 -05:00
|
|
|
discriminant(self).hash(state);
|
2021-07-29 13:29:23 -05:00
|
|
|
match self {
|
|
|
|
Value::Nul => (),
|
|
|
|
Value::Str(v) => v.hash(state),
|
|
|
|
Value::Int(v) => v.hash(state),
|
|
|
|
Value::Bool(v) => v.hash(state),
|
|
|
|
Value::Abool(v) => v.to_string().hash(state),
|
2021-08-07 17:33:28 -05:00
|
|
|
Value::Functio(statements) => statements.hash(state),
|
2021-07-29 13:29:23 -05:00
|
|
|
Value::Cart(_) => self.to_string().hash(state),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2021-08-28 16:27:35 -05:00
|
|
|
.write_all(&[self.clone().into_i32() as u8])
|
2021-07-22 22:08:04 -05:00
|
|
|
.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.
|
2021-08-28 16:27:35 -05:00
|
|
|
pub fn into_i32(self) -> i32 {
|
2021-06-07 19:57:44 -05:00
|
|
|
match self {
|
2021-08-28 16:27:35 -05:00
|
|
|
Value::Abool(a) => a as _,
|
|
|
|
Value::Bool(b) => b as _,
|
2021-07-16 18:56:45 -05:00
|
|
|
Value::Functio(func) => match func {
|
2021-08-28 17:34:08 -05:00
|
|
|
// Compares lengths of functions:
|
|
|
|
// BfFunctio - Sum of lengths of instructions and length of tape
|
|
|
|
// AbleFunctio - Sum of argument count and body length
|
|
|
|
// Eval - Length of input code
|
2021-07-16 18:56:45 -05:00
|
|
|
Functio::BfFunctio {
|
|
|
|
instructions,
|
|
|
|
tape_len,
|
|
|
|
} => (instructions.len() + tape_len) as _,
|
2021-08-28 17:34:08 -05:00
|
|
|
Functio::AbleFunctio { params, body } => {
|
|
|
|
(params.len() + format!("{:?}", body).len()) as _
|
|
|
|
}
|
2021-08-28 17:07:26 -05:00
|
|
|
Functio::Eval(s) => s.len() as _,
|
2021-07-16 18:56:45 -05:00
|
|
|
},
|
2021-08-28 16:27:35 -05:00
|
|
|
Value::Int(i) => i,
|
2021-07-16 18:56:45 -05:00
|
|
|
Value::Nul => consts::ANSWER,
|
|
|
|
Value::Str(text) => text.parse().unwrap_or(consts::ANSWER),
|
2021-07-29 13:29:23 -05:00
|
|
|
Value::Cart(c) => c.len() as _,
|
2021-06-07 19:57:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 11:54:53 -05:00
|
|
|
/// Coerce a value to a boolean.
|
2021-08-28 16:27:35 -05:00
|
|
|
pub fn into_bool(self) -> bool {
|
2021-06-07 19:57:44 -05:00
|
|
|
match self {
|
2021-08-28 16:27:35 -05:00
|
|
|
Value::Abool(b) => b.into(),
|
|
|
|
Value::Bool(b) => b,
|
2021-06-07 19:57:44 -05:00
|
|
|
Value::Functio(_) => true,
|
2021-08-28 16:27:35 -05:00
|
|
|
Value::Int(x) => x != 0,
|
|
|
|
Value::Nul => false,
|
|
|
|
Value::Str(s) => match s.to_lowercase().as_str() {
|
|
|
|
"false" | "no" | "🇳🇴" => false,
|
|
|
|
"true" | "yes" => true,
|
|
|
|
s => !s.is_empty(),
|
|
|
|
},
|
2021-07-29 13:29:23 -05:00
|
|
|
Value::Cart(c) => !c.is_empty(),
|
2021-06-07 19:57:44 -05:00
|
|
|
}
|
|
|
|
}
|
2021-07-29 13:29:23 -05:00
|
|
|
|
2021-09-04 11:54:53 -05:00
|
|
|
/// Coerce a value to an aboolean.
|
2021-08-28 16:27:35 -05:00
|
|
|
pub fn into_abool(self) -> Abool {
|
|
|
|
match self {
|
|
|
|
Value::Nul => Abool::Never,
|
|
|
|
Value::Str(s) => match s.to_lowercase().as_str() {
|
|
|
|
"never" => Abool::Never,
|
|
|
|
"sometimes" => Abool::Sometimes,
|
|
|
|
"always" => Abool::Always,
|
|
|
|
s => {
|
|
|
|
if s.is_empty() {
|
|
|
|
Abool::Never
|
|
|
|
} else {
|
|
|
|
Abool::Always
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Value::Int(x) => match x.cmp(&0) {
|
|
|
|
std::cmp::Ordering::Less => Abool::Never,
|
|
|
|
std::cmp::Ordering::Equal => Abool::Sometimes,
|
|
|
|
std::cmp::Ordering::Greater => Abool::Always,
|
|
|
|
},
|
|
|
|
Value::Bool(b) => {
|
|
|
|
if b {
|
|
|
|
Abool::Always
|
|
|
|
} else {
|
|
|
|
Abool::Never
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Value::Abool(a) => a,
|
|
|
|
Value::Functio(_) => todo!(),
|
|
|
|
Value::Cart(c) => {
|
|
|
|
if c.is_empty() {
|
|
|
|
Abool::Never
|
|
|
|
} else {
|
|
|
|
Abool::Always
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 11:54:53 -05:00
|
|
|
/// Coerce a value to a functio.
|
2021-08-28 16:27:35 -05:00
|
|
|
pub fn into_functio(self) -> Functio {
|
|
|
|
match self {
|
|
|
|
Value::Nul => Functio::AbleFunctio {
|
|
|
|
body: vec![],
|
|
|
|
params: vec![],
|
|
|
|
},
|
|
|
|
Value::Str(s) => Functio::Eval(s),
|
2021-08-30 17:41:05 -05:00
|
|
|
Value::Int(_) => todo!(),
|
2021-08-28 16:27:35 -05:00
|
|
|
Value::Bool(_) => todo!(),
|
|
|
|
Value::Abool(_) => todo!(),
|
|
|
|
Value::Functio(f) => f,
|
|
|
|
Value::Cart(_) => todo!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 11:54:53 -05:00
|
|
|
/// Coerce a value into a cart.
|
2021-08-28 16:27:35 -05:00
|
|
|
pub fn into_cart(self) -> Cart {
|
|
|
|
match self {
|
|
|
|
Value::Nul => HashMap::new(),
|
|
|
|
Value::Str(s) => s
|
|
|
|
.chars()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, x)| {
|
|
|
|
(
|
|
|
|
Value::Int(i as i32 + 1),
|
|
|
|
Rc::new(RefCell::new(Value::Str(x.to_string()))),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
Value::Int(i) => Value::Str(i.to_string()).into_cart(),
|
|
|
|
Value::Bool(b) => Value::Str(b.to_string()).into_cart(),
|
|
|
|
Value::Abool(a) => Value::Str(a.to_string()).into_cart(),
|
|
|
|
Value::Functio(f) => match f {
|
|
|
|
Functio::AbleFunctio { params, body } => {
|
|
|
|
let params: Cart = params
|
|
|
|
.into_iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, x)| {
|
|
|
|
(
|
|
|
|
Value::Int(i as i32 + 1),
|
|
|
|
Rc::new(RefCell::new(Value::Str(x))),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let body: Cart = body
|
|
|
|
.into_iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, x)| {
|
|
|
|
(
|
|
|
|
Value::Int(i as i32 + 1),
|
|
|
|
Rc::new(RefCell::new(Value::Str(format!("{:?}", x)))),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let mut cart = HashMap::new();
|
|
|
|
cart.insert(
|
|
|
|
Value::Str("params".to_owned()),
|
|
|
|
Rc::new(RefCell::new(Value::Cart(params))),
|
|
|
|
);
|
|
|
|
|
|
|
|
cart.insert(
|
|
|
|
Value::Str("body".to_owned()),
|
|
|
|
Rc::new(RefCell::new(Value::Cart(body))),
|
|
|
|
);
|
|
|
|
|
|
|
|
cart
|
|
|
|
}
|
2021-08-30 15:14:13 -05:00
|
|
|
Functio::BfFunctio {
|
|
|
|
instructions,
|
|
|
|
tape_len,
|
|
|
|
} => {
|
|
|
|
let mut cart: Cart = instructions
|
|
|
|
.into_iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, x)| {
|
|
|
|
(
|
|
|
|
Value::Int(i as i32 + 1),
|
|
|
|
Rc::new(RefCell::new(
|
|
|
|
char::from_u32(x as u32)
|
|
|
|
.map(|x| Value::Str(x.to_string()))
|
|
|
|
.unwrap_or(Value::Nul),
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
cart.insert(
|
|
|
|
Value::Str("tapelen".to_owned()),
|
|
|
|
Rc::new(RefCell::new(Value::Int(tape_len as _))),
|
|
|
|
);
|
|
|
|
cart
|
|
|
|
}
|
2021-08-28 16:27:35 -05:00
|
|
|
Functio::Eval(s) => Value::Str(s).into_cart(),
|
|
|
|
},
|
|
|
|
Value::Cart(c) => c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Add for Value {
|
|
|
|
type Output = Value;
|
|
|
|
|
|
|
|
fn add(self, rhs: Self) -> Self::Output {
|
|
|
|
match self {
|
2021-08-30 15:14:13 -05:00
|
|
|
Value::Nul => match rhs {
|
|
|
|
Value::Nul => Value::Nul,
|
|
|
|
Value::Str(_) => Value::Str(self.to_string()) + rhs,
|
|
|
|
Value::Int(_) => Value::Int(self.into_i32()) + rhs,
|
|
|
|
Value::Bool(_) => Value::Bool(self.into_bool()) + rhs,
|
|
|
|
Value::Abool(_) => Value::Abool(self.into_abool()) + rhs,
|
|
|
|
Value::Functio(_) => Value::Functio(self.into_functio()) + rhs,
|
|
|
|
Value::Cart(_) => Value::Cart(self.into_cart()) + rhs,
|
|
|
|
},
|
2021-08-28 16:27:35 -05:00
|
|
|
Value::Str(s) => Value::Str(format!("{}{}", s, rhs.to_string())),
|
2021-08-30 15:14:13 -05:00
|
|
|
Value::Int(i) => Value::Int(i.wrapping_add(rhs.into_i32())),
|
|
|
|
Value::Bool(b) => Value::Bool(b || rhs.into_bool()),
|
2021-08-30 17:41:05 -05:00
|
|
|
Value::Abool(_) => {
|
|
|
|
Value::Abool(Value::Int(self.into_i32().max(rhs.into_i32())).into_abool())
|
|
|
|
}
|
|
|
|
Value::Functio(_) => todo!(),
|
2021-07-29 13:29:23 -05:00
|
|
|
Value::Cart(c) => {
|
2021-08-28 16:27:35 -05:00
|
|
|
Value::Cart(c.into_iter().chain(rhs.into_cart().into_iter()).collect())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Sub for Value {
|
|
|
|
type Output = Value;
|
|
|
|
|
|
|
|
fn sub(self, rhs: Self) -> Self::Output {
|
2021-08-30 15:14:13 -05:00
|
|
|
match self {
|
|
|
|
Value::Nul => match rhs {
|
|
|
|
Value::Nul => Value::Nul,
|
|
|
|
Value::Str(_) => Value::Str(self.to_string()) - rhs,
|
|
|
|
Value::Int(_) => Value::Int(self.into_i32()) - rhs,
|
|
|
|
Value::Bool(_) => Value::Bool(self.into_bool()) - rhs,
|
|
|
|
Value::Abool(_) => Value::Abool(self.into_abool()) - rhs,
|
|
|
|
Value::Functio(_) => Value::Functio(self.into_functio()) - rhs,
|
|
|
|
Value::Cart(_) => Value::Cart(self.into_cart()) - rhs,
|
|
|
|
},
|
|
|
|
Value::Str(s) => Value::Str(s.replace(&rhs.to_string(), "")),
|
|
|
|
Value::Int(i) => Value::Int(i.wrapping_sub(rhs.into_i32())),
|
|
|
|
Value::Bool(b) => Value::Bool(b ^ rhs.into_bool()),
|
2021-08-30 17:41:05 -05:00
|
|
|
Value::Abool(_) => (self.clone() + rhs.clone()) * !(self * rhs),
|
2021-08-30 15:14:13 -05:00
|
|
|
Value::Functio(_) => todo!(),
|
|
|
|
Value::Cart(c) => Value::Cart({
|
|
|
|
let rhs_cart = rhs.into_cart();
|
|
|
|
c.into_iter()
|
|
|
|
.filter(|(k, v)| rhs_cart.get(k) != Some(v))
|
|
|
|
.collect()
|
|
|
|
}),
|
|
|
|
}
|
2021-08-28 16:27:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Mul for Value {
|
|
|
|
type Output = Value;
|
|
|
|
|
|
|
|
fn mul(self, rhs: Self) -> Self::Output {
|
2021-08-30 15:14:13 -05:00
|
|
|
match self {
|
|
|
|
Value::Nul => match rhs {
|
|
|
|
Value::Nul => Value::Nul,
|
|
|
|
Value::Str(_) => Value::Str(self.to_string()) * rhs,
|
|
|
|
Value::Int(_) => Value::Int(self.into_i32()) * rhs,
|
|
|
|
Value::Bool(_) => Value::Bool(self.into_bool()) * rhs,
|
|
|
|
Value::Abool(_) => Value::Abool(self.into_abool()) * rhs,
|
|
|
|
Value::Functio(_) => Value::Functio(self.into_functio()) * rhs,
|
|
|
|
Value::Cart(_) => Value::Cart(self.into_cart()) * rhs,
|
|
|
|
},
|
|
|
|
Value::Str(s) => Value::Str(s.repeat(rhs.into_i32() as usize)),
|
|
|
|
Value::Int(i) => Value::Int(i.wrapping_mul(rhs.into_i32())),
|
|
|
|
Value::Bool(b) => Value::Bool(b && rhs.into_bool()),
|
2021-08-30 17:41:05 -05:00
|
|
|
Value::Abool(_) => {
|
|
|
|
Value::Abool(Value::Int(self.into_i32().min(rhs.into_i32())).into_abool())
|
|
|
|
}
|
2021-08-30 15:14:13 -05:00
|
|
|
Value::Functio(_) => todo!(),
|
2021-08-30 17:41:05 -05:00
|
|
|
Value::Cart(c) => {
|
|
|
|
let rhsc = rhs.into_cart();
|
|
|
|
|
|
|
|
Value::Cart(
|
|
|
|
c.into_iter()
|
|
|
|
.map(|(k, v)| {
|
|
|
|
if let Some(k) = rhsc.get(&k) {
|
|
|
|
(k.borrow().clone(), v)
|
|
|
|
} else {
|
|
|
|
(k, v)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
}
|
2021-08-30 15:14:13 -05:00
|
|
|
}
|
2021-08-28 16:27:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Div for Value {
|
|
|
|
type Output = Value;
|
|
|
|
|
|
|
|
fn div(self, rhs: Self) -> Self::Output {
|
2021-08-30 15:14:13 -05:00
|
|
|
match self {
|
|
|
|
Value::Nul => match rhs {
|
|
|
|
Value::Nul => Value::Nul,
|
|
|
|
Value::Str(_) => Value::Str(self.to_string()) / rhs,
|
|
|
|
Value::Int(_) => Value::Int(self.into_i32()) / rhs,
|
|
|
|
Value::Bool(_) => Value::Bool(self.into_bool()) / rhs,
|
|
|
|
Value::Abool(_) => Value::Abool(self.into_abool()) / rhs,
|
|
|
|
Value::Functio(_) => Value::Functio(self.into_functio()) / rhs,
|
|
|
|
Value::Cart(_) => Value::Cart(self.into_cart()) / rhs,
|
|
|
|
},
|
|
|
|
Value::Str(s) => Value::Cart(
|
|
|
|
s.split(&rhs.to_string())
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, x)| {
|
|
|
|
(
|
|
|
|
Value::Int(i as i32 + 1),
|
|
|
|
Rc::new(RefCell::new(Value::Str(x.to_owned()))),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
),
|
2021-09-02 11:35:33 -05:00
|
|
|
Value::Int(i) => Value::Int(i.wrapping_div(match rhs.into_i32() {
|
|
|
|
0 => consts::ANSWER,
|
2021-09-02 11:36:25 -05:00
|
|
|
x => x,
|
2021-09-02 11:35:33 -05:00
|
|
|
})),
|
2021-08-30 17:41:05 -05:00
|
|
|
Value::Bool(b) => Value::Bool(!b || rhs.into_bool()),
|
|
|
|
Value::Abool(_) => !self + rhs,
|
2021-08-30 15:14:13 -05:00
|
|
|
Value::Functio(_) => todo!(),
|
2021-08-30 17:41:05 -05:00
|
|
|
Value::Cart(c) => {
|
|
|
|
let cart_len = c.len();
|
|
|
|
let chunk_len = rhs.into_i32() as usize;
|
|
|
|
|
|
|
|
Value::Cart(
|
|
|
|
c.into_iter()
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.chunks(cart_len / chunk_len + (cart_len % chunk_len != 0) as usize)
|
|
|
|
.enumerate()
|
|
|
|
.map(|(k, v)| {
|
|
|
|
(
|
|
|
|
Value::Int(k as i32 + 1),
|
2021-09-01 10:46:17 -05:00
|
|
|
Rc::new(RefCell::new(Value::Cart(v.iter().cloned().collect()))),
|
2021-08-30 17:41:05 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
}
|
2021-08-30 15:14:13 -05:00
|
|
|
}
|
2021-08-28 16:27:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 15:55:31 -05:00
|
|
|
impl ops::Not for Value {
|
|
|
|
type Output = Value;
|
|
|
|
|
|
|
|
fn not(self) -> Self::Output {
|
2021-08-30 16:19:25 -05:00
|
|
|
match self {
|
|
|
|
Value::Nul => Value::Nul,
|
|
|
|
Value::Str(s) => Value::Str(s.chars().rev().collect()),
|
|
|
|
Value::Int(i) => Value::Int(i.swap_bytes()),
|
|
|
|
Value::Bool(b) => Value::Bool(!b),
|
|
|
|
Value::Abool(a) => Value::Abool(match a {
|
|
|
|
Abool::Never => Abool::Always,
|
|
|
|
Abool::Sometimes => Abool::Sometimes,
|
|
|
|
Abool::Always => Abool::Never,
|
|
|
|
}),
|
|
|
|
Value::Functio(_) => todo!(),
|
|
|
|
Value::Cart(c) => Value::Cart(
|
|
|
|
c.into_iter()
|
|
|
|
.map(|(k, v)| (v.borrow().clone(), Rc::new(RefCell::new(k))))
|
|
|
|
.collect(),
|
|
|
|
),
|
|
|
|
}
|
2021-08-30 15:55:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-28 16:27:35 -05:00
|
|
|
impl PartialEq for Value {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
let other = other.clone();
|
|
|
|
|
|
|
|
match self {
|
2021-09-01 10:46:17 -05:00
|
|
|
Value::Nul => matches!(other, Value::Nul),
|
2021-08-28 16:27:35 -05:00
|
|
|
Value::Str(s) => *s == other.to_string(),
|
|
|
|
Value::Int(i) => *i == other.into_i32(),
|
|
|
|
Value::Bool(b) => *b == other.into_bool(),
|
|
|
|
Value::Abool(a) => *a == other.into_abool(),
|
|
|
|
Value::Functio(f) => *f == other.into_functio(),
|
|
|
|
Value::Cart(c) => *c == other.into_cart(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for Value {}
|
|
|
|
|
|
|
|
impl PartialOrd for Value {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
|
|
|
use std::cmp::Ordering::*;
|
|
|
|
let other = other.clone();
|
|
|
|
|
|
|
|
match self {
|
|
|
|
Value::Nul => {
|
|
|
|
if other == Value::Nul {
|
|
|
|
Some(Equal)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2021-07-29 13:29:23 -05:00
|
|
|
}
|
2021-08-28 16:43:59 -05:00
|
|
|
Value::Str(s) => Some(s.cmp(&other.to_string())),
|
|
|
|
Value::Int(i) => Some(i.cmp(&other.into_i32())),
|
|
|
|
Value::Bool(b) => Some(b.cmp(&other.into_bool())),
|
|
|
|
Value::Abool(a) => a.partial_cmp(&other.into_abool()),
|
2021-08-28 17:28:45 -05:00
|
|
|
Value::Functio(_) => self.clone().into_i32().partial_cmp(&other.into_i32()),
|
2021-08-28 16:43:59 -05:00
|
|
|
Value::Cart(c) => Some(c.len().cmp(&other.into_cart().len())),
|
2021-08-28 16:27:35 -05:00
|
|
|
}
|
2021-07-29 13:29:23 -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-08-28 16:27:35 -05:00
|
|
|
Functio::Eval(s) => write!(f, "{}", s),
|
2021-06-02 15:29:31 -05:00
|
|
|
},
|
2021-07-29 13:29:23 -05:00
|
|
|
Value::Cart(c) => {
|
|
|
|
write!(f, "[")?;
|
2021-09-01 10:46:17 -05:00
|
|
|
let mut cart_vec = c.iter().collect::<Vec<_>>();
|
|
|
|
cart_vec.sort_by(|x, y| x.0.partial_cmp(y.0).unwrap_or(std::cmp::Ordering::Less));
|
2021-07-29 13:29:23 -05:00
|
|
|
|
2021-08-28 16:52:58 -05:00
|
|
|
for (key, value) in cart_vec {
|
|
|
|
write!(f, "{} <= {}, ", value.borrow(), key)?;
|
2021-07-29 13:29:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "]")
|
|
|
|
}
|
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
|
|
|
}
|