return statement

replace/7746dba3cc6b3860afe1faf69e86ed84ee46988d
Natapat Samutpong 2022-02-12 18:33:37 +07:00
parent 826092c54b
commit a3376f2492
4 changed files with 18 additions and 1 deletions

View File

@ -1,4 +1,5 @@
func join :: (a, b) = {
let result :: String = from(a, b);
return result;
};
let foo :: String = join("f", "oo");

View File

@ -94,6 +94,7 @@ fn lex_reserved_identifier(input: &Bytes) -> IResult<&Bytes, Token> {
"else" => Token::Else,
"let" => Token::Let,
"func" => Token::Func,
"return" => Token::Return,
"true" => Token::Bool(true),
"false" => Token::Bool(false),
_ => Token::Identifier(syntax.to_string()),

View File

@ -18,7 +18,7 @@ pub enum Token {
LBrace, RBrace,
Semicolon, Colon, Comma,
If, Else, Let, Func,
If, Else, Let, Func, Return,
}
/// Token struct with position information.
@ -103,6 +103,7 @@ pub enum Stmt {
Let(Ident, Ident, Expr),
Func(Ident, Vec<Ident>, Vec<Stmt>),
Call(Ident, Vec<Expr>),
Return(Expr),
}
#[derive(Clone, Debug, PartialEq)]

View File

@ -21,6 +21,8 @@ macro_rules! tag_token (
tag_token!(tag_let, Token::Let);
tag_token!(tag_func, Token::Func);
tag_token!(tag_return, Token::Return);
tag_token!(tag_assign, Token::Assign);
tag_token!(tag_typehint, Token::Typehint);
tag_token!(tag_semicolon, Token::Semicolon);
@ -148,6 +150,17 @@ fn parse_expr_lowest(input: Tokens) -> IResult<Tokens, Expr> {
parse_expr_with(input, Precedence::Lowest)
}
fn parse_return_stmt(input: Tokens) -> IResult<Tokens, Stmt> {
map(
delimited(
tag_return,
parse_expr_lowest,
tag_semicolon,
),
Stmt::Return,
)(input)
}
fn parse_call_stmt(input: Tokens) -> IResult<Tokens, Stmt> {
map(
tuple((
@ -202,6 +215,7 @@ fn parse_stmt(input: Tokens) -> IResult<Tokens, Stmt> {
parse_let_stmt,
parse_func_stmt,
parse_call_stmt,
parse_return_stmt,
))(input)
}