diff --git a/crates/codegen/src/top_level.rs b/crates/codegen/src/top_level.rs index a9ef282..94073c5 100644 --- a/crates/codegen/src/top_level.rs +++ b/crates/codegen/src/top_level.rs @@ -88,8 +88,11 @@ impl GenRust for FunctionDecl { param.gen_rust(ctx, cg); } - cg.add(") -> "); - cg.add(&self.return_type.get_rust()); + cg.add(") "); + if let Some(return_type) = &self.return_type { + cg.add("-> "); + cg.add(&return_type.get_rust()); + } if let Some(body) = &self.body { cg.add(" "); @@ -254,7 +257,7 @@ impl GenRust for (&Vec>, &Spanned) { name: Identifier(String::from("constructor")), generics: self.1.item.generics.clone(), params: ParamList(constructor_params), - return_type: TypeExpr::Tuple(Vec::new()), + return_type: Some(TypeExpr::Tuple(Vec::new())), body: Some(self.1.item.body.clone()), }, } diff --git a/crates/parser/src/ast/top_level.rs b/crates/parser/src/ast/top_level.rs index f87d011..dd29e9b 100644 --- a/crates/parser/src/ast/top_level.rs +++ b/crates/parser/src/ast/top_level.rs @@ -97,7 +97,7 @@ pub struct FunctionDecl { pub name: Identifier, pub generics: GenericsDecl, pub params: ParamList, - pub return_type: TypeExpr, + pub return_type: Option, pub body: Option, } diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index 97e1613..07f4b7b 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -192,7 +192,7 @@ attributes = { // DECLARATION HELPERS // ====================================================== var_decl = { - "let" ~ WHITESPACE ~ pattern ~ type_expr? + "let" ~ pattern ~ type_expr? } field = { @@ -323,7 +323,7 @@ trait_decl = { // ====================================================== function_decl = { - visibility? ~ type_expr ~ identifier ~ generics_decl? ~ "(" ~ method_args ~ ")" ~ (block | semicolon) + visibility? ~ "fn" ~ identifier ~ generics_decl? ~ "(" ~ method_args ~ ")" ~ type_expr? ~ (block | semicolon) } // ====================================================== diff --git a/crates/parser/src/parser/items/function.rs b/crates/parser/src/parser/items/function.rs index d44d00d..7dbd50c 100644 --- a/crates/parser/src/parser/items/function.rs +++ b/crates/parser/src/parser/items/function.rs @@ -13,7 +13,6 @@ impl<'a> TryFrom> for FunctionDecl { ast_ensure!(pair, Rule::function_decl => { let mut inner = pair.into_inner(); let visibility = Visibility::try_from(&mut inner); - let return_type = TypeExpr::try_from(inner.next().unwrap()); let name = Identifier::try_from(inner.next().unwrap()); let generics = consume_rule(&mut inner, Rule::generics_decl) @@ -51,6 +50,10 @@ impl<'a> TryFrom> for FunctionDecl { }) .unwrap_or_else(|| Ok(ParamList(self_param.into_iter().collect()))); + let return_type = consume_rule(&mut inner, Rule::type_expr) + .map(TypeExpr::try_from) + .transpose(); + let body = inner.next().map(Block::try_from).transpose(); ast_expr!(Self { diff --git a/mist-test/src/main.mist b/mist-test/src/main.mist index 0e319b9..6ea27b3 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -1,9 +1,9 @@ -void main() {} +fn main() {} struct MyStruct<'a> { name *'a String } -i32 add(a i32, b i32) { +fn add(a i32, b i32) i32 { a + b }