feat: functions with formal parameters

This commit is contained in:
2025-05-04 15:21:44 +08:00
parent bc50464db9
commit eea4a4ce9f
6 changed files with 53 additions and 10 deletions

View File

@@ -43,9 +43,13 @@ impl AttrSet {
self
}
pub fn to_data(self) -> HashTrieMapSync<Symbol, Value> {
pub fn into_inner(self) -> HashTrieMapSync<Symbol, Value> {
self.data
}
pub fn as_inner(&self) -> &HashTrieMapSync<Symbol, Value> {
&self.data
}
}
impl ToPublic for AttrSet {

View File

@@ -1,8 +1,9 @@
use ecow::EcoString;
use itertools::Itertools;
use rpds::HashTrieMap;
use crate::bytecode::{OpCodes, ThunkIdx};
use crate::ty::internal::Value;
use crate::ty::internal::{Thunk, Value};
use crate::vm::{LockedEnv, VM};
@@ -39,7 +40,38 @@ impl Func {
match self.param.unwrap() {
Ident(ident) => env.enter(HashTrieMap::new_sync().insert(ident.into(), arg)),
Formals { .. } => todo!()
Formals {
formals,
ellipsis,
alias,
} => {
let arg = arg.unwrap_attr_set();
let mut new = HashTrieMap::new_sync();
if !ellipsis
&& arg
.as_inner()
.iter()
.map(|(k, _)| k.as_inner())
.sorted()
.ne(formals.iter().map(|(k, _)| k).sorted())
{
todo!()
}
for (formal, default) in formals {
// let arg = if let Some(default) = default {
// arg.select(format)
// }
let arg = arg
.select(formal.clone().into())
.or_else(|| default.map(|idx| Value::Thunk(Thunk(idx))))
.unwrap();
new.insert_mut(formal.into(), arg);
}
if let Some(alias) = alias {
new.insert_mut(alias.into(), Value::AttrSet(arg));
}
env.enter(new);
}
}
vm.eval(self.opcodes, &mut env).unwrap()