1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

return type hint

This commit is contained in:
Natapat Samutpong 2022-02-12 20:37:28 +07:00
parent b4d3399a32
commit 0b82cf058f
4 changed files with 23 additions and 18 deletions

View file

@ -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" };

View file

@ -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)
}

View file

@ -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<Stmt>;
#[derive(Clone, Debug, PartialEq)]
pub enum Stmt {
Let(Ident, Ident, Expr),
Func(Ident, Vec<Ident>, Vec<Stmt>),
Func(Ident, Vec<Ident>, Ident, Vec<Stmt>),
Call(Ident, Vec<Expr>),
Return(Expr),
}

View file

@ -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<Tokens, Stmt> {
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)
}