refactor: function
This commit is contained in:
@@ -2,43 +2,46 @@ use ecow::EcoString;
|
||||
use itertools::Itertools;
|
||||
use rpds::HashTrieMap;
|
||||
|
||||
use crate::bytecode::OpCodes;
|
||||
use crate::bytecode::{OpCodes, ThunkIdx};
|
||||
use crate::error::Result;
|
||||
use crate::ty::internal::{Thunk, Value};
|
||||
use crate::ty::internal::Value;
|
||||
use crate::vm::{CapturedEnv, VM};
|
||||
use crate::ir;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Param {
|
||||
Ident(EcoString),
|
||||
Formals {
|
||||
formals: Vec<(EcoString, Option<Thunk>)>,
|
||||
formals: Vec<(EcoString, Option<ThunkIdx>)>,
|
||||
ellipsis: bool,
|
||||
alias: Option<EcoString>,
|
||||
},
|
||||
}
|
||||
|
||||
impl From<ir::Param> for Param {
|
||||
fn from(value: ir::Param) -> Self {
|
||||
match value {
|
||||
ir::Param::Ident(ident) => Param::Ident(ident),
|
||||
ir::Param::Formals { formals, ellipsis, alias } =>
|
||||
Param::Formals { formals: formals.into_iter().map(|(sym, default)| (sym, default.map(|default| default.idx))).collect(), ellipsis, alias }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Func {
|
||||
pub env: CapturedEnv,
|
||||
pub param: Option<Param>,
|
||||
pub opcodes: OpCodes,
|
||||
pub env: Option<CapturedEnv>,
|
||||
pub param: Param,
|
||||
pub opcodes: OpCodes
|
||||
}
|
||||
|
||||
impl Func {
|
||||
pub fn new(env: CapturedEnv, opcodes: OpCodes) -> Func {
|
||||
Func {
|
||||
env,
|
||||
opcodes,
|
||||
param: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn call(self, vm: &VM, arg: Value) -> Result<Value> {
|
||||
use Param::*;
|
||||
|
||||
let env = self.env.released();
|
||||
let env = self.env.unwrap().released();
|
||||
|
||||
match self.param.unwrap() {
|
||||
match self.param {
|
||||
Ident(ident) => env.enter(HashTrieMap::new_sync().insert(ident.into(), arg)),
|
||||
Formals {
|
||||
formals,
|
||||
@@ -60,7 +63,7 @@ impl Func {
|
||||
for (formal, default) in formals {
|
||||
let arg = arg
|
||||
.select(formal.clone().into())
|
||||
.or_else(|| default.map(Value::Thunk))
|
||||
.or_else(|| default.map(|idx| Value::Thunk(vm.get_thunk(idx))))
|
||||
.unwrap();
|
||||
new.insert_mut(formal.into(), arg);
|
||||
}
|
||||
@@ -73,50 +76,6 @@ impl Func {
|
||||
|
||||
vm.eval(self.opcodes, env)
|
||||
}
|
||||
|
||||
pub fn push_ident_param(&mut self, param: EcoString) {
|
||||
self.param = Some(Param::Ident(param))
|
||||
}
|
||||
|
||||
pub fn push_formal_param(&mut self, param: EcoString) {
|
||||
let Param::Formals { formals, .. } = self.param.get_or_insert_with(|| Param::Formals {
|
||||
formals: Vec::with_capacity(1),
|
||||
ellipsis: false,
|
||||
alias: None,
|
||||
}) else {
|
||||
panic!()
|
||||
};
|
||||
formals.push((param, None));
|
||||
}
|
||||
|
||||
pub fn push_default_param(&mut self, default: Thunk) {
|
||||
let Param::Formals { formals, .. } = self.param.as_mut().unwrap() else {
|
||||
panic!()
|
||||
};
|
||||
formals.last_mut().unwrap().1 = Some(default)
|
||||
}
|
||||
|
||||
pub fn set_ellipsis(&mut self) {
|
||||
let Param::Formals { ellipsis, .. } = self.param.get_or_insert_with(|| Param::Formals {
|
||||
formals: Vec::new(),
|
||||
ellipsis: false,
|
||||
alias: None,
|
||||
}) else {
|
||||
panic!()
|
||||
};
|
||||
*ellipsis = true;
|
||||
}
|
||||
|
||||
pub fn set_alias(&mut self, sym: EcoString) {
|
||||
let Param::Formals { alias, .. } = self.param.get_or_insert_with(|| Param::Formals {
|
||||
formals: Vec::new(),
|
||||
ellipsis: false,
|
||||
alias: None,
|
||||
}) else {
|
||||
panic!()
|
||||
};
|
||||
*alias = Some(sym);
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Func {
|
||||
|
||||
@@ -60,17 +60,15 @@ impl Thunk {
|
||||
}
|
||||
|
||||
pub fn force(&self, vm: &VM) -> Result<Value> {
|
||||
{
|
||||
match &*self.thunk.borrow() {
|
||||
_Thunk::Value(value) => return Ok(value.as_ref().clone()),
|
||||
_Thunk::SuspendedFrom(from) => {
|
||||
return Err(Error::EvalError(format!(
|
||||
"thunk {:p} already suspended from {from:p} (infinite recursion encountered)",
|
||||
self as *const Thunk
|
||||
)));
|
||||
}
|
||||
_Thunk::Code(_) => (),
|
||||
match &*self.thunk.borrow() {
|
||||
_Thunk::Value(value) => return Ok(value.as_ref().clone()),
|
||||
_Thunk::SuspendedFrom(from) => {
|
||||
return Err(Error::EvalError(format!(
|
||||
"thunk {:p} already suspended from {from:p} (infinite recursion encountered)",
|
||||
self as *const Thunk
|
||||
)));
|
||||
}
|
||||
_Thunk::Code(_) => (),
|
||||
}
|
||||
let opcodes = std::mem::replace(
|
||||
&mut *self.thunk.borrow_mut(),
|
||||
@@ -366,14 +364,14 @@ impl Value {
|
||||
if let Value::AttrSet(attrs) = self {
|
||||
let val = attrs
|
||||
.select(sym.clone())
|
||||
.unwrap_or(Value::Catchable(c::Catchable::new(Some(format!(
|
||||
.unwrap_or_else(|| Value::Catchable(c::Catchable::new(Some(format!(
|
||||
"{sym:?} not found"
|
||||
)))));
|
||||
*self = val;
|
||||
} else if let Value::RecAttrSet(attrs) = self {
|
||||
let val = attrs
|
||||
.select(sym.clone())
|
||||
.unwrap_or(Value::Catchable(c::Catchable::new(Some(format!(
|
||||
.unwrap_or_else(|| Value::Catchable(c::Catchable::new(Some(format!(
|
||||
"{sym:?} not found"
|
||||
)))));
|
||||
*self = val;
|
||||
|
||||
Reference in New Issue
Block a user