feat: function (WIP)

This commit is contained in:
2025-05-04 13:58:34 +08:00
parent 63fd380514
commit 6ecd20854a
12 changed files with 228 additions and 79 deletions

View File

@@ -1,6 +1,6 @@
use ecow::EcoString;
use crate::ty::internal::{Const, Param};
use crate::ty::internal::Const;
type Slice<T> = Box<[T]>;
@@ -35,7 +35,17 @@ pub enum OpCode {
/// return a value
Ret,
/// make a function
Func { param: Param, length: usize },
Func { idx: ThunkIdx },
/// push param `sym` into TOS
PushIdentParam { sym: EcoString },
/// push formal param `sym` into TOS
PushFormalParam { sym: EcoString },
/// push default formal param with thunkidx `idx` into TOS
PushDefaultParam { idx: ThunkIdx },
/// TODO:
SetEllipsis,
/// TODO:
SetAlias { sym: EcoString },
/// consume 1 element, assert TOS is true
Assert,

View File

@@ -406,10 +406,25 @@ impl Compile for ir::Assert {
impl Compile for ir::Func {
fn compile(self, comp: &mut Compiler) {
let idx = comp.idx();
comp.push(OpCode::NoOp);
let length = self.body.compile_with_length(comp);
comp.modify(idx, OpCode::Func { param: self.param.into(), length });
comp.push(OpCode::Func { idx: self.body.idx });
use ir::Param::*;
match self.param {
Ident(sym) => comp.push(OpCode::PushIdentParam { sym }),
Formals { formals, ellipsis, alias } => {
for (sym, default) in formals {
comp.push(OpCode::PushFormalParam { sym });
if let Some(ir::Thunk { idx }) = default {
comp.push(OpCode::PushDefaultParam { idx });
}
}
if ellipsis {
comp.push(OpCode::SetEllipsis);
}
if let Some(sym) = alias {
comp.push(OpCode::SetAlias { sym });
}
}
}
}
}

View File

@@ -116,7 +116,7 @@ ir! {
UnOp => { rhs: Box<Ir>, kind: UnOpKind },
Select => { expr: Box<Ir>, attrpath: Vec<Attr>, default: Option<Box<Ir>> },
If => { cond: Box<Ir>, consq: Box<Ir>, alter: Box<Ir> },
Func => { param: Param, body: Box<Ir> },
Func => { param: Param, body: Thunk },
Call => { func: Box<Ir>, args: Vec<Ir> },
Let => { attrs: Attrs, expr: Box<Ir> },
@@ -630,7 +630,8 @@ impl Downgrade for ast::Lambda {
fn downgrade(self, state: &mut DowngradeState) -> Result<Ir> {
let body = self.body().unwrap();
let param = downgrade_param(self.param().unwrap(), state)?;
let body = body.downgrade(state)?.boxed();
let body = body.downgrade(state)?;
let body = state.new_thunk(body);
Func { param, body }.ir().ok()
}
}

View File

@@ -3,15 +3,12 @@ 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 {

View File

@@ -1,7 +1,7 @@
use ecow::EcoString;
use crate::ty::internal::Value;
use crate::bytecode::{OpCodes, ThunkIdx};
use crate::ty::internal::Value;
#[derive(Debug, Clone)]
pub enum Param {
@@ -15,14 +15,65 @@ pub enum Param {
#[derive(Debug, Clone)]
pub struct Func {
pub param: Param,
pub opcodes: OpCodes
pub param: Option<Param>,
pub opcodes: OpCodes,
}
impl Func {
pub fn new(opcodes: OpCodes) -> Func {
Func {
param: None,
opcodes,
}
}
pub fn call(self, _arg: Value) -> Value {
todo!()
}
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: ThunkIdx) {
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 {

View File

@@ -39,30 +39,88 @@ pub enum Value {
Catchable(c::Catchable),
PrimOp(PrimOp),
PartialPrimOp(PartialPrimOp),
Func(Func),
// FuncWithEnv(Func)
}
#[derive(Debug, IsVariant, Unwrap, Clone, PartialEq)]
pub enum ValueAsRef<'a> {
Const(&'a Const),
Thunk(&'a Thunk),
AttrSet(&'a AttrSet),
List(&'a List),
Catchable(&'a c::Catchable),
PrimOp(&'a PrimOp),
PartialPrimOp(&'a PartialPrimOp),
Func(&'a Func),
}
#[derive(Debug, IsVariant, Unwrap, PartialEq)]
pub enum ValueAsMut<'a> {
Const(&'a mut Const),
Thunk(&'a mut Thunk),
AttrSet(&'a mut AttrSet),
List(&'a mut List),
Catchable(&'a mut c::Catchable),
PrimOp(&'a mut PrimOp),
PartialPrimOp(&'a mut PartialPrimOp),
Func(&'a mut Func),
}
impl Value {
pub fn as_ref(&self) -> ValueAsRef<'_> {
use Value::*;
use ValueAsRef as R;
match self {
Const(x) => R::Const(x),
Thunk(x) => R::Thunk(x),
AttrSet(x) => R::AttrSet(x),
List(x) => R::List(x),
Catchable(x) => R::Catchable(x),
PrimOp(x) => R::PrimOp(x),
PartialPrimOp(x) => R::PartialPrimOp(x),
Func(x) => R::Func(x),
}
}
pub fn as_mut(&mut self) -> ValueAsMut<'_> {
use Value::*;
use ValueAsMut as M;
match self {
Const(x) => M::Const(x),
Thunk(x) => M::Thunk(x),
AttrSet(x) => M::AttrSet(x),
List(x) => M::List(x),
Catchable(x) => M::Catchable(x),
PrimOp(x) => M::PrimOp(x),
PartialPrimOp(x) => M::PartialPrimOp(x),
Func(x) => M::Func(x),
}
}
}
use Value::Const as VmConst;
impl Value {
pub fn callable(&self) -> bool {
match self {
Value::PrimOp(_) | Value::PartialPrimOp(_) | Value::Const(Const::Func(_)) => true,
Value::PrimOp(_) | Value::PartialPrimOp(_) | Value::Func(_) => true,
Value::AttrSet(_) => todo!(),
_ => false,
}
}
pub fn call(self, args: Vec<Value>) -> Value {
use Value as V;
use Value::*;
match self {
V::PrimOp(func) => func.call(args),
V::PartialPrimOp(func) => func.call(args),
mut func @ Value::Const(Const::Func(_)) => {
PrimOp(func) => func.call(args),
PartialPrimOp(func) => func.call(args),
mut func @ Value::Func(_) => {
let mut iter = args.into_iter();
while let Some(arg) = iter.next() {
func = match func {
V::PrimOp(func) => return func.call([arg].into_iter().chain(iter).collect()),
V::PartialPrimOp(func) => return func.call([arg].into_iter().chain(iter).collect()),
V::Const(Const::Func(func)) => func.call(arg),
PrimOp(func) => return func.call([arg].into_iter().chain(iter).collect()),
PartialPrimOp(func) => return func.call([arg].into_iter().chain(iter).collect()),
Func(func) => func.call(arg),
_ => todo!()
}
}
@@ -267,8 +325,9 @@ impl ToPublic for Value {
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,
Value::PrimOp(primop) => p::Value::PrimOp(primop.name),
Value::PartialPrimOp(primop) => p::Value::PartialPrimOp(primop.name),
Value::Func(_) => p::Value::Func,
}
}
}

View File

@@ -10,7 +10,6 @@ pub enum Const {
Int(i64),
Float(f64),
String(EcoString),
Func
}
impl From<i::Const> for Const {
@@ -21,7 +20,6 @@ impl From<i::Const> for Const {
Int(int) => Const::Int(int),
Float(float) => Const::Float(float),
String(string) => Const::String(string),
Func(_) => Const::Func
}
}
}

View File

@@ -38,6 +38,6 @@ pub enum Value {
Catchable(Catchable),
Thunk,
Func,
PrimOp,
PartialPrimOp,
PrimOp(&'static str),
PartialPrimOp(&'static str),
}

View File

@@ -1,3 +1,5 @@
use std::ops::Index;
use anyhow::Result;
use crate::builtins::env;
@@ -81,8 +83,23 @@ impl VM {
let func = stack.pop()?;
stack.push(func.call(args))?;
}
OpCode::Func { param, length } => {
todo!()
OpCode::Func { idx } => {
stack.push(Value::Func(Func::new(self.thunks[idx].unwrap_code())))?;
}
OpCode::PushIdentParam { sym } => {
stack.tos_mut()?.as_mut().unwrap_func().push_ident_param(sym);
}
OpCode::PushFormalParam { sym } => {
stack.tos_mut()?.as_mut().unwrap_func().push_formal_param(sym);
}
OpCode::PushDefaultParam { idx } => {
stack.tos_mut()?.as_mut().unwrap_func().push_default_param(idx);
}
OpCode::SetEllipsis => {
stack.tos_mut()?.as_mut().unwrap_func().set_ellipsis();
}
OpCode::SetAlias { sym } => {
stack.tos_mut()?.as_mut().unwrap_func().set_alias(sym);
}
OpCode::Ret => {
todo!()

View File

@@ -15,7 +15,7 @@ pub struct VmThunk {
lock: RwLock<()>,
}
#[derive(IsVariant, Unwrap)]
#[derive(IsVariant, Unwrap, Clone)]
enum _VmThunk {
Code(OpCodes),
SuspendedFrom(*const VmThunk),
@@ -30,6 +30,11 @@ impl VmThunk {
}
}
pub fn unwrap_code(&self) -> OpCodes {
let _guard = self.lock.read().unwrap();
self.thunk.borrow().clone().unwrap_code()
}
pub fn force(&self, vm: &VM, env: &mut Env) -> Result<Value> {
{
let _guard = self.lock.read().unwrap();
@@ -43,20 +48,18 @@ impl VmThunk {
_VmThunk::Code(_) => (),
}
}
{
let _guard = self.lock.write().unwrap();
let opcodes = std::mem::replace(
&mut *self.thunk.borrow_mut(),
_VmThunk::SuspendedFrom(self as *const VmThunk),
)
.unwrap_code();
let value = vm.eval(opcodes, env).unwrap();
let _ = std::mem::replace(
&mut *self.thunk.borrow_mut(),
_VmThunk::Value(value.clone()),
);
Ok(value)
}
let _guard = self.lock.write().unwrap();
let opcodes = std::mem::replace(
&mut *self.thunk.borrow_mut(),
_VmThunk::SuspendedFrom(self as *const VmThunk),
)
.unwrap_code();
let value = vm.eval(opcodes, env).unwrap();
let _ = std::mem::replace(
&mut *self.thunk.borrow_mut(),
_VmThunk::Value(value.clone()),
);
Ok(value)
}
pub fn value(&self) -> Option<Value> {