From 0b82cf058fd7136b2722deccafb6364fbe549633 Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Sat, 12 Feb 2022 20:37:28 +0700 Subject: [PATCH] return type hint --- example/hello_world.hyc | 6 +++--- src/front/lex.rs | 26 ++++++++++++++------------ src/front/model.rs | 4 ++-- src/front/parser.rs | 5 ++++- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/example/hello_world.hyc b/example/hello_world.hyc index 9efe307..1671678 100644 --- a/example/hello_world.hyc +++ b/example/hello_world.hyc @@ -1,10 +1,10 @@ // user defined function -func foo :: (a, b) = { - return a; +func foo :: (a, b) -> Bool = { + return a == b; }; // entry point -func main :: () = { +func main :: () -> Int = { // if else in variable definition let cond_str :: String = if true { return "t" } else { return "f" }; diff --git a/src/front/lex.rs b/src/front/lex.rs index 8dbb9d8..86369dd 100644 --- a/src/front/lex.rs +++ b/src/front/lex.rs @@ -31,18 +31,24 @@ syntax! { divide_operator , "/", Token::Div } syntax! { not_operator , "!", Token::Not } // Punctuations -syntax! { typehint_punctuation , "::", Token::Typehint } -syntax! { lparen_punctuation , "(", Token::LParen } -syntax! { rparen_punctuation , ")", Token::RParen } -syntax! { lbrace_punctuation , "{", Token::LBrace } -syntax! { rbrace_punctuation , "}", Token::RBrace } -syntax! { semicolon_punctuation , ";", Token::Semicolon } -syntax! { colon_punctuation , ":", Token::Colon } -syntax! { comma_punctuation , ",", Token::Comma } +syntax! { typehint_punctuation , "::", Token::Typehint } +syntax! { returnhint_punctuation , "->", Token::Return } +syntax! { lparen_punctuation , "(", Token::LParen } +syntax! { rparen_punctuation , ")", Token::RParen } +syntax! { lbrace_punctuation , "{", Token::LBrace } +syntax! { rbrace_punctuation , "}", Token::RBrace } +syntax! { semicolon_punctuation , ";", Token::Semicolon } +syntax! { colon_punctuation , ":", Token::Colon } +syntax! { comma_punctuation , ",", Token::Comma } // Operator & Punctuation fn lex_operator_punctuation(input: &Bytes) -> IResult<&Bytes, Token> { alt(( + typehint_punctuation, returnhint_punctuation, + lparen_punctuation, rparen_punctuation, + lbrace_punctuation, rbrace_punctuation, + semicolon_punctuation, colon_punctuation, comma_punctuation, + equal_operator, not_equal_operator, less_than_operator, greater_than_operator, less_than_equal_operator, greater_than_equal_operator, @@ -51,10 +57,6 @@ fn lex_operator_punctuation(input: &Bytes) -> IResult<&Bytes, Token> { add_operator, subtract_operator, multiply_operator, divide_operator, not_operator, - typehint_punctuation, - lparen_punctuation, rparen_punctuation, - lbrace_punctuation, rbrace_punctuation, - semicolon_punctuation, colon_punctuation, comma_punctuation, ))(input) } diff --git a/src/front/model.rs b/src/front/model.rs index 07de247..2c87064 100644 --- a/src/front/model.rs +++ b/src/front/model.rs @@ -9,7 +9,7 @@ pub enum Token { Identifier(String), String(String), Int(i64), Bool(bool), - Assign, Typehint, + Assign, Typehint, Returnhint, Plus, Minus, Mul, Div, Not, Eq, NEq, Lt, Gt, Lte, Gte, @@ -101,7 +101,7 @@ pub type Program = Vec; #[derive(Clone, Debug, PartialEq)] pub enum Stmt { Let(Ident, Ident, Expr), - Func(Ident, Vec, Vec), + Func(Ident, Vec, Ident, Vec), Call(Ident, Vec), Return(Expr), } diff --git a/src/front/parser.rs b/src/front/parser.rs index 5d3dfa4..a2df2eb 100644 --- a/src/front/parser.rs +++ b/src/front/parser.rs @@ -31,6 +31,7 @@ tag_token!(tag_not, Token::Not); tag_token!(tag_assign, Token::Assign); tag_token!(tag_typehint, Token::Typehint); +tag_token!(tag_returnhint, Token::Return); tag_token!(tag_semicolon, Token::Semicolon); tag_token!(tag_lparen, Token::LParen); tag_token!(tag_rparen, Token::RParen); @@ -245,11 +246,13 @@ fn parse_func_stmt(input: Tokens) -> IResult { tag_lparen, alt((parse_params, empty_params)), tag_rparen, + tag_returnhint, + parse_ident, tag_assign, parse_block_stmt, opt(tag_semicolon), )), - |(_, ident, _, _, params, _, _, block, _)| Stmt::Func(ident, params, block), + |(_, ident, _, _, params, _, _, returntype, _, block, _)| Stmt::Func(ident, params, returntype, block), )(input) }