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
+29
View File
@@ -200,8 +200,37 @@ impl GetMist for TypeExpr {
Self::Dyn(ty) => { Self::Dyn(ty) => {
format!("dyn {}", ty.get_mist()) format!("dyn {}", ty.get_mist())
} }
Self::Fn {
kind,
return_type,
params,
} => {
format!(
"{} {}({})",
return_type.get_mist(),
kind.get_mist(),
params
.iter()
.map(TypeExpr::get_mist)
.collect::<Vec<_>>()
.join(", ")
)
} }
} }
}
}
impl GetMist for FnKind {
fn get_mist(&self) -> String {
match self {
Self::Fn => "fn",
Self::UnsafeFn => "unsafe fn",
Self::FnClosure => "Fn",
Self::FnMut => "FnMut",
Self::FnOnce => "FnOnce",
}
.to_string()
}
} }
impl GetMist for ExprPath { impl GetMist for ExprPath {
+30 -1
View File
@@ -239,7 +239,6 @@ impl GetRust for TypeExpr {
Self::UnsafePtr { mutable, ty } => { Self::UnsafePtr { mutable, ty } => {
let mutable = if *mutable { "mut " } else { "const " }; let mutable = if *mutable { "mut " } else { "const " };
format!("*{mutable}{}", ty.get_rust()) format!("*{mutable}{}", ty.get_rust())
} }
Self::Ref { Self::Ref {
@@ -262,8 +261,38 @@ impl GetRust for TypeExpr {
Self::Dyn(ty) => { Self::Dyn(ty) => {
format!("dyn {}", ty.get_rust()) format!("dyn {}", ty.get_rust())
} }
Self::Fn {
kind,
return_type,
params,
} => {
format!(
"{} -> {}({})",
kind.get_rust(),
return_type.get_rust(),
params
.iter()
.map(TypeExpr::get_rust)
.collect::<Vec<_>>()
.join(", ")
)
} }
} }
}
}
impl GetRust for FnKind {
fn get_rust(&self) -> String {
match self {
Self::Fn => "fn",
Self::UnsafeFn => "unsafe fn",
Self::FnClosure => "Fn",
Self::FnMut => "FnMut",
Self::FnOnce => "FnOnce",
}
.to_string()
}
} }
impl GenRust for Pattern { impl GenRust for Pattern {
+14
View File
@@ -17,6 +17,15 @@ pub struct Identifier(pub String);
#[derive(Debug, Clone, Serialize, Default)] #[derive(Debug, Clone, Serialize, Default)]
pub struct ParamList(pub Vec<VarDecl>); 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)] #[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
pub enum TypeExpr { pub enum TypeExpr {
Ref { Ref {
@@ -33,6 +42,11 @@ pub enum TypeExpr {
StaticFn(Vec<TypeExpr>, Option<Box<TypeExpr>>), StaticFn(Vec<TypeExpr>, Option<Box<TypeExpr>>),
Tuple(Vec<TypeExpr>), Tuple(Vec<TypeExpr>),
Lifetime(Identifier), Lifetime(Identifier),
Fn {
kind: FnKind,
return_type: Box<TypeExpr>,
params: Vec<TypeExpr>,
},
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
+23 -1
View File
@@ -111,12 +111,34 @@ ref_type = {
mutable? ~ lifetime? ~ "&" 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 = {
"dyn" ~ type_expr "dyn" ~ type_expr
} }
void_type = {
"void"
}
type_expr = { 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 = { 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)?, _ => 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::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::tuple_type => Ok(TypeExpr::Tuple(collect_recovered(inner)?)),
Rule::path_type => Ok(TypeExpr::Path( Rule::path_type => Ok(TypeExpr::Path(
Path::try_from(inner.next().unwrap())?, Path::try_from(inner.next().unwrap())?,
inner.next().map(Generics::try_from).transpose()?, inner.next().map(Generics::try_from).transpose()?,
+2
View File
@@ -15,4 +15,6 @@ void main()
items::impl_test::run_tests(); items::impl_test::run_tests();
items::attribute_test::run_tests(); items::attribute_test::run_tests();
println!("\x1b[32m\nAll tests passed!\x1b[0m"); println!("\x1b[32m\nAll tests passed!\x1b[0m");
void fn() hello = () => {};
} }