Function types

This commit is contained in:
2026-06-28 05:45:13 +02:00
parent 3ddb1bc601
commit 76541040c9
6 changed files with 104 additions and 2 deletions
+14
View File
@@ -17,6 +17,15 @@ pub struct Identifier(pub String);
#[derive(Debug, Clone, Serialize, Default)]
pub struct ParamList(pub Vec<VarDecl>);
#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
pub enum FnKind {
Fn,
UnsafeFn,
FnClosure,
FnOnce,
FnMut,
}
#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
pub enum TypeExpr {
Ref {
@@ -33,6 +42,11 @@ pub enum TypeExpr {
StaticFn(Vec<TypeExpr>, Option<Box<TypeExpr>>),
Tuple(Vec<TypeExpr>),
Lifetime(Identifier),
Fn {
kind: FnKind,
return_type: Box<TypeExpr>,
params: Vec<TypeExpr>,
},
}
#[derive(Debug, Clone, Serialize)]
+23 -1
View File
@@ -111,12 +111,34 @@ ref_type = {
mutable? ~ lifetime? ~ "&"
}
fn_kind_fn = { "fn" }
fn_kind_unsafe = { "unsafe" ~ "fn" }
fn_kind_closure = { "Fn" }
fn_kind_once = { "FnOnce" }
fn_kind_mut = { "FnMut" }
fn_kind = _{
fn_kind_fn
| fn_kind_unsafe
| fn_kind_closure
| fn_kind_once
| fn_kind_mut
}
fn_type = {
fn_kind ~ "(" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ")"
}
dyn_type = {
"dyn" ~ type_expr
}
void_type = {
"void"
}
type_expr = {
(path_type | tuple_type | dyn_type) ~ (unsafe_ref_type | ref_type)*
(void_type | path_type | tuple_type | dyn_type) ~ (unsafe_ref_type | ref_type | fn_type)*
}
lifetime = {
+6
View File
@@ -42,6 +42,10 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExpr {
};
}
Rule::fn_type => {
}
_ => AstError::bug_unimplemented(ref_pair)?,
}
}
@@ -50,7 +54,9 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExpr {
}
Rule::lifetime => Ok(TypeExpr::Lifetime(inner.next().unwrap().try_into()?)),
Rule::void_type => Ok(TypeExpr::Tuple(Vec::new())),
Rule::tuple_type => Ok(TypeExpr::Tuple(collect_recovered(inner)?)),
Rule::path_type => Ok(TypeExpr::Path(
Path::try_from(inner.next().unwrap())?,
inner.next().map(Generics::try_from).transpose()?,