diff --git a/example/hello_world.hyc b/example/hello_world.hyc index f9e2be9..b397959 100644 --- a/example/hello_world.hyc +++ b/example/hello_world.hyc @@ -1,4 +1,5 @@ func join :: (a, b) = { let result :: String = from(a, b); + return result; }; let foo :: String = join("f", "oo"); \ No newline at end of file diff --git a/src/front/lex.rs b/src/front/lex.rs index ed459c7..97aa658 100644 --- a/src/front/lex.rs +++ b/src/front/lex.rs @@ -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()), diff --git a/src/front/model.rs b/src/front/model.rs index 2aa7bc1..163e3ef 100644 --- a/src/front/model.rs +++ b/src/front/model.rs @@ -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, Vec), Call(Ident, Vec), + Return(Expr), } #[derive(Clone, Debug, PartialEq)] diff --git a/src/front/parser.rs b/src/front/parser.rs index cf866b7..8aedf5c 100644 --- a/src/front/parser.rs +++ b/src/front/parser.rs @@ -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 { parse_expr_with(input, Precedence::Lowest) } +fn parse_return_stmt(input: Tokens) -> IResult { + map( + delimited( + tag_return, + parse_expr_lowest, + tag_semicolon, + ), + Stmt::Return, + )(input) +} + fn parse_call_stmt(input: Tokens) -> IResult { map( tuple(( @@ -202,6 +215,7 @@ fn parse_stmt(input: Tokens) -> IResult { parse_let_stmt, parse_func_stmt, parse_call_stmt, + parse_return_stmt, ))(input) }