crftng-intrprtrs/src/interpreter/types.rs

37 lines
821 B
Rust

use crate::ast::expression;
use from_variants::FromVariants;
use gc::trace::GCTrace;
#[derive(Debug, PartialEq, PartialOrd, Clone, FromVariants)]
pub enum Primitive {
Int(i32),
Float(f32),
Bool(bool),
Nil,
// TODO: create a cow(not rust cow) string instead of cloning a normal string
String(String),
}
unsafe impl GCTrace for Primitive {}
impl From<expression::Literal> for Primitive {
fn from(l: expression::Literal) -> Self {
match_any::match_any!(l,
expression::Literal::Int(v) | expression::Literal::Bool(v) | expression::Literal::Float(v) | expression::Literal::String(v) => v.into(),
expression::Literal::Nil => Self::Nil
)
}
}
impl Default for Primitive {
fn default() -> Self {
Self::Nil
}
}
impl Primitive {
pub fn truthy(&self) -> bool {
!(matches!(*self, Self::Bool(false)))
}
}