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 // user defined function
func foo :: (a, b) = { func foo :: (a, b) -> Bool = {
return a; return a == b;
}; };
// entry point // entry point
func main :: () = { func main :: () -> Int = {
// if else in variable definition // if else in variable definition
let cond_str :: String = if true { return "t" } else { return "f" }; let cond_str :: String = if true { return "t" } else { return "f" };

View file

@ -32,6 +32,7 @@ syntax! { not_operator , "!", Token::Not }
// Punctuations // Punctuations
syntax! { typehint_punctuation , "::", Token::Typehint } syntax! { typehint_punctuation , "::", Token::Typehint }
syntax! { returnhint_punctuation , "->", Token::Return }
syntax! { lparen_punctuation , "(", Token::LParen } syntax! { lparen_punctuation , "(", Token::LParen }
syntax! { rparen_punctuation , ")", Token::RParen } syntax! { rparen_punctuation , ")", Token::RParen }
syntax! { lbrace_punctuation , "{", Token::LBrace } syntax! { lbrace_punctuation , "{", Token::LBrace }
@ -43,6 +44,11 @@ syntax! { comma_punctuation , ",", Token::Comma }
// Operator & Punctuation // Operator & Punctuation
fn lex_operator_punctuation(input: &Bytes) -> IResult<&Bytes, Token> { fn lex_operator_punctuation(input: &Bytes) -> IResult<&Bytes, Token> {
alt(( alt((
typehint_punctuation, returnhint_punctuation,
lparen_punctuation, rparen_punctuation,
lbrace_punctuation, rbrace_punctuation,
semicolon_punctuation, colon_punctuation, comma_punctuation,
equal_operator, not_equal_operator, equal_operator, not_equal_operator,
less_than_operator, greater_than_operator, less_than_operator, greater_than_operator,
less_than_equal_operator, greater_than_equal_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, add_operator, subtract_operator, multiply_operator, divide_operator,
not_operator, not_operator,
typehint_punctuation,
lparen_punctuation, rparen_punctuation,
lbrace_punctuation, rbrace_punctuation,
semicolon_punctuation, colon_punctuation, comma_punctuation,
))(input) ))(input)
} }

View file

@ -9,7 +9,7 @@ pub enum Token {
Identifier(String), String(String), Identifier(String), String(String),
Int(i64), Bool(bool), Int(i64), Bool(bool),
Assign, Typehint, Assign, Typehint, Returnhint,
Plus, Minus, Mul, Div, Not, Plus, Minus, Mul, Div, Not,
Eq, NEq, Lt, Gt, Lte, Gte, Eq, NEq, Lt, Gt, Lte, Gte,
@ -101,7 +101,7 @@ pub type Program = Vec<Stmt>;
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum Stmt { pub enum Stmt {
Let(Ident, Ident, Expr), Let(Ident, Ident, Expr),
Func(Ident, Vec<Ident>, Vec<Stmt>), Func(Ident, Vec<Ident>, Ident, Vec<Stmt>),
Call(Ident, Vec<Expr>), Call(Ident, Vec<Expr>),
Return(Expr), Return(Expr),
} }

View file

@ -31,6 +31,7 @@ tag_token!(tag_not, Token::Not);
tag_token!(tag_assign, Token::Assign); tag_token!(tag_assign, Token::Assign);
tag_token!(tag_typehint, Token::Typehint); tag_token!(tag_typehint, Token::Typehint);
tag_token!(tag_returnhint, Token::Return);
tag_token!(tag_semicolon, Token::Semicolon); tag_token!(tag_semicolon, Token::Semicolon);
tag_token!(tag_lparen, Token::LParen); tag_token!(tag_lparen, Token::LParen);
tag_token!(tag_rparen, Token::RParen); tag_token!(tag_rparen, Token::RParen);
@ -245,11 +246,13 @@ fn parse_func_stmt(input: Tokens) -> IResult<Tokens, Stmt> {
tag_lparen, tag_lparen,
alt((parse_params, empty_params)), alt((parse_params, empty_params)),
tag_rparen, tag_rparen,
tag_returnhint,
parse_ident,
tag_assign, tag_assign,
parse_block_stmt, parse_block_stmt,
opt(tag_semicolon), opt(tag_semicolon),
)), )),
|(_, ident, _, _, params, _, _, block, _)| Stmt::Func(ident, params, block), |(_, ident, _, _, params, _, _, returntype, _, block, _)| Stmt::Func(ident, params, returntype, block),
)(input) )(input)
} }