refactor: type
This commit is contained in:
65
src/ty/internal/attrset.rs
Normal file
65
src/ty/internal/attrset.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use derive_more::Constructor;
|
||||
use rpds::HashTrieMapSync;
|
||||
|
||||
use super::super::public as p;
|
||||
use super::super::common::Symbol;
|
||||
|
||||
use crate::vm::VM;
|
||||
use super::{ToPublic, Value};
|
||||
|
||||
#[derive(Debug, Constructor, Clone, PartialEq)]
|
||||
pub struct AttrSet {
|
||||
data: HashTrieMapSync<Symbol, Value>,
|
||||
}
|
||||
|
||||
impl AttrSet {
|
||||
pub fn empty() -> AttrSet {
|
||||
AttrSet { data: HashTrieMapSync::new_sync() }
|
||||
}
|
||||
|
||||
pub fn push_attr_force(&mut self, sym: Symbol, val: Value) {
|
||||
self.data.insert_mut(sym, val);
|
||||
}
|
||||
|
||||
pub fn push_attr(&mut self, sym: Symbol, val: Value) {
|
||||
if self.data.get(&sym).is_some() {
|
||||
todo!()
|
||||
}
|
||||
self.data.insert_mut(sym, val);
|
||||
}
|
||||
|
||||
pub fn select(&self, sym: Symbol) -> Option<Value> {
|
||||
self.data.get(&sym).cloned()
|
||||
}
|
||||
|
||||
pub fn has_attr(&self, sym: Symbol) -> bool {
|
||||
self.data.get(&sym).is_some()
|
||||
}
|
||||
|
||||
pub fn update(mut self, other: AttrSet) -> AttrSet {
|
||||
for (k, v) in other.data.iter() {
|
||||
self.push_attr_force(k.clone(), v.clone())
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn to_data(self) -> HashTrieMapSync<Symbol, Value> {
|
||||
self.data
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPublic for AttrSet {
|
||||
fn to_public(self, vm: &VM) -> p::Value {
|
||||
p::Value::AttrSet(p::AttrSet::new(
|
||||
self.data
|
||||
.iter()
|
||||
.map(|(sym, value)| {
|
||||
(
|
||||
sym.clone(),
|
||||
value.clone().to_public(vm),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
}
|
||||
124
src/ty/internal/cnst.rs
Normal file
124
src/ty/internal/cnst.rs
Normal file
@@ -0,0 +1,124 @@
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
use anyhow::Error;
|
||||
use derive_more::{IsVariant, Unwrap};
|
||||
|
||||
use ecow::EcoString;
|
||||
|
||||
use super::Func;
|
||||
|
||||
#[derive(Debug, Clone, IsVariant, Unwrap)]
|
||||
pub enum Const {
|
||||
Bool(bool),
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
String(EcoString),
|
||||
Func(Func),
|
||||
}
|
||||
|
||||
impl From<bool> for Const {
|
||||
fn from(value: bool) -> Self {
|
||||
Const::Bool(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i64> for Const {
|
||||
fn from(value: i64) -> Self {
|
||||
Const::Int(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<f64> for Const {
|
||||
fn from(value: f64) -> Self {
|
||||
Const::Float(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EcoString> for Const {
|
||||
fn from(value: EcoString) -> Self {
|
||||
Const::String(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Const {
|
||||
fn from(value: String) -> Self {
|
||||
Const::String(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Const {
|
||||
fn from(value: &str) -> Self {
|
||||
Const::String(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<&'a Const> for &'a bool {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: &'a Const) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
Const::Bool(b) => Ok(b),
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a> TryFrom<&'a Const> for &'a i64 {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: &'a Const) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
Const::Int(int) => Ok(int),
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<&'a Const> for &'a f64 {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: &'a Const) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
Const::Float(float) => Ok(float),
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TryFrom<&'a Const> for &'a str {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: &'a Const) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
Const::String(string) => Ok(string),
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Const {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
use Const::*;
|
||||
match (self, other) {
|
||||
(Bool(a), Bool(b)) => a == b,
|
||||
(Int(a), Int(b)) => a == b,
|
||||
(Float(a), Float(b)) => a == b,
|
||||
(String(a), String(b)) => a == b,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Const {}
|
||||
|
||||
/* impl Hash for Const {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
use Const::*;
|
||||
match self {
|
||||
Bool(b) => b.hash(state),
|
||||
Int(int) => int.hash(state),
|
||||
Float(float) => float.to_bits().hash(state),
|
||||
String(string) => string.hash(state),
|
||||
Func(func) => func.hash(state),
|
||||
}
|
||||
}
|
||||
} */
|
||||
25
src/ty/internal/func.rs
Normal file
25
src/ty/internal/func.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
use ecow::EcoString;
|
||||
|
||||
use crate::bytecode::{OpCodes, ThunkIdx};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Param {
|
||||
Ident(EcoString),
|
||||
Formals {
|
||||
formals: Vec<(EcoString, Option<ThunkIdx>)>,
|
||||
ellipsis: bool,
|
||||
alias: Option<EcoString>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Func {
|
||||
pub param: Param,
|
||||
pub opcodes: OpCodes
|
||||
}
|
||||
|
||||
impl PartialEq for Func {
|
||||
fn eq(&self, _: &Self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
42
src/ty/internal/list.rs
Normal file
42
src/ty/internal/list.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use derive_more::Constructor;
|
||||
use rpds::VectorSync;
|
||||
|
||||
use crate::ty::public as p;
|
||||
|
||||
use crate::vm::VM;
|
||||
use super::{ToPublic, Value};
|
||||
|
||||
#[derive(Debug, Constructor, Clone, PartialEq)]
|
||||
pub struct List {
|
||||
data: VectorSync<Value>,
|
||||
}
|
||||
|
||||
impl List {
|
||||
pub fn empty() -> List {
|
||||
List {
|
||||
data: VectorSync::new_sync()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&mut self, elem: Value) {
|
||||
self.data.push_back_mut(elem);
|
||||
}
|
||||
|
||||
pub fn concat(mut self, other: List) -> List {
|
||||
for elem in other.data.iter() {
|
||||
self.data.push_back_mut(elem.clone());
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPublic for List {
|
||||
fn to_public(self, vm: &VM) -> p::Value {
|
||||
p::Value::List(p::List::new(
|
||||
self.data
|
||||
.iter()
|
||||
.map(|value| value.clone().to_public(vm))
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
}
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
67
src/ty/internal/primop.rs
Normal file
67
src/ty/internal/primop.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use derive_more::Constructor;
|
||||
|
||||
use super::Value;
|
||||
|
||||
#[derive(Debug, Clone, Constructor)]
|
||||
pub struct PrimOp {
|
||||
pub name: &'static str,
|
||||
arity: u8,
|
||||
func: fn(Vec<Value>) -> Value,
|
||||
}
|
||||
|
||||
impl PartialEq for PrimOp {
|
||||
fn eq(&self, _: &Self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl PrimOp {
|
||||
pub fn call(self, args: Vec<Value>) -> Value {
|
||||
if (args.len() as u8) < self.arity {
|
||||
Value::PartialPrimOp(PartialPrimOp {
|
||||
name: self.name,
|
||||
arity: self.arity - args.len() as u8,
|
||||
args,
|
||||
func: self.func,
|
||||
})
|
||||
} else if args.len() as u8 == self.arity {
|
||||
(self.func)(args)
|
||||
} else {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PartialPrimOp {
|
||||
pub name: &'static str,
|
||||
arity: u8,
|
||||
args: Vec<Value>,
|
||||
func: fn(Vec<Value>) -> Value,
|
||||
}
|
||||
|
||||
impl PartialEq for PartialPrimOp {
|
||||
fn eq(&self, _: &Self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialPrimOp {
|
||||
pub fn call(mut self, args: Vec<Value>) -> Value {
|
||||
let len = args.len() as u8;
|
||||
self.args.extend(args);
|
||||
if len < self.arity {
|
||||
Value::PartialPrimOp(PartialPrimOp {
|
||||
name: self.name,
|
||||
arity: self.arity - len,
|
||||
args: self.args,
|
||||
func: self.func,
|
||||
})
|
||||
} else if len == self.arity {
|
||||
(self.func)(self.args)
|
||||
} else {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
30
src/ty/internal/string.rs
Normal file
30
src/ty/internal/string.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
// TODO: Contextful String
|
||||
|
||||
use ecow::EcoString;
|
||||
use rpds::List;
|
||||
|
||||
pub struct StringContext {
|
||||
context: List<()>,
|
||||
}
|
||||
|
||||
impl StringContext {
|
||||
pub fn new() -> StringContext {
|
||||
StringContext {
|
||||
context: List::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ContextfulString {
|
||||
string: EcoString,
|
||||
context: StringContext,
|
||||
}
|
||||
|
||||
impl ContextfulString {
|
||||
pub fn new(string: EcoString) -> ContextfulString {
|
||||
ContextfulString {
|
||||
string,
|
||||
context: StringContext::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user