2021-05-20 18:18:01 -05:00
|
|
|
use std::{convert::TryFrom, fmt::Display};
|
|
|
|
|
2021-04-18 16:40:41 -05:00
|
|
|
use rand::Rng;
|
2021-04-13 18:01:19 -05:00
|
|
|
|
2021-05-20 18:18:01 -05:00
|
|
|
use crate::error::{Error, ErrorKind};
|
|
|
|
|
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-05-02 11:12:51 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2021-04-18 09:39:43 -05:00
|
|
|
pub enum Value {
|
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-04-29 11:50:51 -05:00
|
|
|
Nul,
|
2021-04-13 18:01:19 -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 {
|
|
|
|
Value::Str(v) => write!(f, "{}", v),
|
|
|
|
Value::Int(v) => write!(f, "{}", v),
|
|
|
|
Value::Bool(v) => write!(f, "{}", v),
|
|
|
|
Value::Abool(v) => write!(f, "{}", v),
|
|
|
|
Value::Nul => write!(f, "nul"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Value> for i32 {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(value: Value) -> Result<Self, Self::Error> {
|
|
|
|
if let Value::Int(i) = value {
|
|
|
|
Ok(i)
|
|
|
|
} else {
|
|
|
|
Err(Error {
|
|
|
|
kind: ErrorKind::TypeError(format!("Expected int, got {}", value)),
|
|
|
|
// TODO: either add some kind of metadata to `Value`
|
|
|
|
// so we can tell where the value came from and assign
|
|
|
|
// this `position` correctly, or re-write the
|
|
|
|
// `error::Error` struct so we can omit the `position`
|
|
|
|
// when using some error kinds.
|
|
|
|
position: 0..0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Value> for bool {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(value: Value) -> Result<Self, Self::Error> {
|
|
|
|
if let Value::Bool(b) = value {
|
|
|
|
Ok(b)
|
|
|
|
} else {
|
|
|
|
Err(Error {
|
|
|
|
kind: ErrorKind::TypeError(format!("Expected bool, got {}", value)),
|
|
|
|
position: 0..0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 18:01:19 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Variable {
|
2021-05-20 18:18:01 -05:00
|
|
|
pub melo: bool,
|
|
|
|
pub value: Value,
|
2021-04-27 06:48:56 -05:00
|
|
|
}
|