feat: functions with formal parameters
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user