refactor: type
This commit is contained in:
253
src/ty/internal/mod.rs
Normal file
253
src/ty/internal/mod.rs
Normal file
@@ -0,0 +1,253 @@
|
||||
use anyhow::Result;
|
||||
use derive_more::{Constructor, IsVariant, Unwrap};
|
||||
|
||||
use super::public as p;
|
||||
use super::common as c;
|
||||
|
||||
use c::Symbol;
|
||||
|
||||
use crate::vm::Env;
|
||||
use crate::vm::VM;
|
||||
|
||||
mod attrset;
|
||||
mod list;
|
||||
mod string;
|
||||
mod func;
|
||||
mod cnst;
|
||||
mod primop;
|
||||
|
||||
pub use attrset::AttrSet;
|
||||
pub use list::List;
|
||||
pub use string::ContextfulString;
|
||||
pub use func::*;
|
||||
pub use primop::*;
|
||||
pub use cnst::Const;
|
||||
|
||||
pub trait ToPublic {
|
||||
fn to_public(self, vm: &VM) -> p::Value;
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Constructor)]
|
||||
pub struct Thunk(usize);
|
||||
|
||||
#[derive(Debug, IsVariant, Unwrap, Clone, PartialEq)]
|
||||
pub enum Value {
|
||||
Const(Const),
|
||||
Thunk(Thunk),
|
||||
AttrSet(AttrSet),
|
||||
List(List),
|
||||
Catchable(c::Catchable),
|
||||
PrimOp(PrimOp),
|
||||
PartialPrimOp(PartialPrimOp),
|
||||
}
|
||||
|
||||
use Value::Const as VmConst;
|
||||
impl Value {
|
||||
pub fn call(self, args: Vec<Value>) -> Value {
|
||||
match self {
|
||||
Value::PrimOp(func) => func.call(args),
|
||||
Value::PartialPrimOp(func) => func.call(args),
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn not(self) -> Value {
|
||||
use Const::*;
|
||||
match self {
|
||||
VmConst(Bool(bool)) => VmConst(Bool(!bool)),
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn and(self, other: Value) -> Value {
|
||||
use Const::*;
|
||||
match (self, other) {
|
||||
(VmConst(Bool(a)), VmConst(Bool(b))) => VmConst(Bool(a && b)),
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn or(self, other: Value) -> Value {
|
||||
use Const::*;
|
||||
match (self, other) {
|
||||
(VmConst(Bool(a)), VmConst(Bool(b))) => VmConst(Bool(a || b)),
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eq(self, other: Value) -> Value {
|
||||
use Const::Bool;
|
||||
VmConst(Bool(self == other))
|
||||
}
|
||||
|
||||
pub fn lt(self, other: Value) -> Value {
|
||||
use Const::*;
|
||||
VmConst(Bool(match (self, other) {
|
||||
(VmConst(Int(a)), VmConst(Int(b))) => a < b,
|
||||
(VmConst(Int(a)), VmConst(Float(b))) => (a as f64) < b,
|
||||
(VmConst(Float(a)), VmConst(Int(b))) => a < b as f64,
|
||||
(VmConst(Float(a)), VmConst(Float(b))) => a < b,
|
||||
(VmConst(String(a)), VmConst(String(b))) => a < b,
|
||||
_ => todo!(),
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn neg(self) -> Value {
|
||||
use Const::*;
|
||||
match self {
|
||||
VmConst(Int(int)) => VmConst(Int(-int)),
|
||||
VmConst(Float(float)) => VmConst(Float(-float)),
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add(self, other: Value) -> Value {
|
||||
use Const::*;
|
||||
match (self, other) {
|
||||
(VmConst(Int(a)), VmConst(Int(b))) => VmConst(Int(a + b)),
|
||||
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(a as f64 + b)),
|
||||
(VmConst(Float(a)), VmConst(Int(b))) => VmConst(Float(a + b as f64)),
|
||||
(VmConst(Float(a)), VmConst(Float(b))) => VmConst(Float(a + b)),
|
||||
(VmConst(String(a)), VmConst(String(b))) => {
|
||||
let mut string = a.clone();
|
||||
string.push_str(b.as_str());
|
||||
VmConst(String(string))
|
||||
}
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mul(self, other: Value) -> Value {
|
||||
use Const::*;
|
||||
match (self, other) {
|
||||
(VmConst(Int(a)), VmConst(Int(b))) => VmConst(Int(a * b)),
|
||||
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(a as f64 * b)),
|
||||
(VmConst(Float(a)), VmConst(Int(b))) => VmConst(Float(a * b as f64)),
|
||||
(VmConst(Float(a)), VmConst(Float(b))) => VmConst(Float(a * b)),
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn div(self, other: Value) -> Value {
|
||||
use Const::*;
|
||||
match (self, other) {
|
||||
(_, VmConst(Int(0))) => todo!(),
|
||||
(_, VmConst(Float(0.))) => todo!(),
|
||||
(VmConst(Int(a)), VmConst(Int(b))) => VmConst(Int(a / b)),
|
||||
(VmConst(Int(a)), VmConst(Float(b))) => VmConst(Float(a as f64 / b)),
|
||||
(VmConst(Float(a)), VmConst(Int(b))) => VmConst(Float(a / b as f64)),
|
||||
(VmConst(Float(a)), VmConst(Float(b))) => VmConst(Float(a / b)),
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn concat_string(&mut self, mut other: Value) -> &mut Self {
|
||||
if let (VmConst(Const::String(a)), VmConst(Const::String(b))) =
|
||||
(self.coerce_to_string(), other.coerce_to_string())
|
||||
{
|
||||
a.push_str(b.as_str());
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn push(&mut self, elem: Value) -> &mut Self {
|
||||
if let Value::List(list) = self {
|
||||
list.push(elem);
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn concat(self, other: Value) -> Value {
|
||||
if let (Value::List(a), Value::List(b)) = (self, other) {
|
||||
Value::List(a.concat(b))
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_attr(&mut self, sym: Symbol, val: Value) -> &mut Self {
|
||||
if let Value::AttrSet(attrs) = self {
|
||||
attrs.push_attr(sym, val)
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn update(self, other: Value) -> Value {
|
||||
if let (Value::AttrSet(a), Value::AttrSet(b)) = (self, other) {
|
||||
Value::AttrSet(a.update(b))
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select(&mut self, sym: Symbol) -> &mut Self {
|
||||
if let Value::AttrSet(attrs) = self {
|
||||
let val = attrs
|
||||
.select(sym.clone())
|
||||
.unwrap_or(Value::Catchable(c::Catchable::new(Some(format!(
|
||||
"{sym:?} not found"
|
||||
)))));
|
||||
*self = val;
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn select_with_default(&mut self, sym: Symbol, default: Value) -> &mut Self {
|
||||
if let Value::AttrSet(attrs) = self {
|
||||
let val = attrs.select(sym).unwrap_or(default);
|
||||
*self = val;
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn has_attr(&mut self, sym: Symbol) -> &mut Self {
|
||||
if let Value::AttrSet(attrs) = self {
|
||||
let val = VmConst(Const::Bool(attrs.has_attr(sym)));
|
||||
*self = val;
|
||||
} else {
|
||||
*self = VmConst(Const::Bool(false));
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn coerce_to_string(&mut self) -> &mut Self {
|
||||
if let VmConst(Const::String(_)) = self {
|
||||
()
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn force(&mut self, vm: &VM, env: &mut Env) -> Result<&mut Self> {
|
||||
if let Value::Thunk(thunk) = self {
|
||||
let value = vm.get_thunk_value(thunk.0, env)?;
|
||||
*self = value
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPublic for Value {
|
||||
fn to_public(self, vm: &VM) -> p::Value {
|
||||
match self {
|
||||
Value::AttrSet(attrs) => attrs.to_public(vm),
|
||||
Value::List(list) => list.to_public(vm),
|
||||
Value::Catchable(catchable) => p::Value::Catchable(catchable),
|
||||
Value::Const(cnst) => p::Value::Const(cnst.into()),
|
||||
Value::Thunk(_) => p::Value::Thunk,
|
||||
Value::PrimOp(_) => p::Value::PrimOp,
|
||||
Value::PartialPrimOp(_) => p::Value::PartialPrimOp,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user