From 23f1edfea41c81196ec4a2352cca38bb406ea1e4 Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Mon, 7 Mar 2022 00:20:18 +0700 Subject: [PATCH] return keyword --- crates/lexer/src/lib.rs | 3 +++ crates/main/src/main.rs | 5 ++++- crates/parser/src/lib.rs | 17 +++++++++++++++-- example/{ex.hyc => ex.hades} | 7 +++++++ 4 files changed, 29 insertions(+), 3 deletions(-) rename example/{ex.hyc => ex.hades} (68%) diff --git a/crates/lexer/src/lib.rs b/crates/lexer/src/lib.rs index bad50cd..3da34c9 100644 --- a/crates/lexer/src/lib.rs +++ b/crates/lexer/src/lib.rs @@ -6,6 +6,7 @@ pub enum Token { KwLet, KwFun, KwDo, KwEnd, KwIf, KwThen, KwElse, + KwReturn, // Literals Int(i64), Float(String), Boolean(bool), @@ -32,6 +33,7 @@ impl std::fmt::Display for Token { Token::KwIf => write!(f, "if"), Token::KwThen => write!(f, "then"), Token::KwElse => write!(f, "else"), + Token::KwReturn => write!(f, "return"), Token::Int(i) => write!(f, "{}", i), Token::Float(s) => write!(f, "{}", s), @@ -105,6 +107,7 @@ pub fn lexer() -> impl Parser, Error = Simple> { "if" => Token::KwIf, "then" => Token::KwThen, "else" => Token::KwElse, + "return" => Token::KwReturn, _ => Token::Identifier(s), }); diff --git a/crates/main/src/main.rs b/crates/main/src/main.rs index 0d24dc4..14f5731 100644 --- a/crates/main/src/main.rs +++ b/crates/main/src/main.rs @@ -60,7 +60,7 @@ fn main() { .with_message(format!( "{}, expected {}", - if e.found().is_some() {"Unexpected token in input" } + if e.found().is_some() { "Unexpected token in input" } else { "Unexpected end of input" }, if e.expected().len() == 0 { "something else".to_string().fg(Color::Green) } @@ -84,6 +84,9 @@ fn main() { .fg(Color::Red) )) .with_color(Color::Red) + ) + .with_help( + "You might have forgotten to end a previous line with semicolon" ), _ => { println!("{:?}", e); diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index e87f08d..ce31520 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -11,7 +11,7 @@ pub enum Expr { Unary { op: String, rhs: Box> }, Binary { lhs: Box>, op: String, rhs: Box> }, Call { name: Box>, args: Spanned>> }, - + Let { name: String, type_hint: String, @@ -23,7 +23,8 @@ pub enum Expr { args: Spanned, Spanned)>>, body: Box> }, - + Return { expr: Box> }, + If { cond: Box>, then: Box>, @@ -193,6 +194,17 @@ fn expr_parser() -> impl Parser>, Error = Simple ) }); + let return_ = just(Token::KwReturn) + .ignore_then(expr.clone()) + .map(|(expr, span)| { + ( + Expr::Return { + expr: Box::new((expr, span.clone())), + }, + span.start..span.end, + ) + }); + let do_block = just(Token::KwDo) .ignore_then( expr.clone() @@ -211,6 +223,7 @@ fn expr_parser() -> impl Parser>, Error = Simple let_ .or(fun) + .or(return_) .or(do_block) .or(compare) }).labelled("expression"); diff --git a/example/ex.hyc b/example/ex.hades similarity index 68% rename from example/ex.hyc rename to example/ex.hades index 4292411..a403401 100644 --- a/example/ex.hyc +++ b/example/ex.hades @@ -7,4 +7,11 @@ fun add (lhs: Int) (rhs: Int): Int = lhs + rhs; fun add_2 (lhs: Int) (rhs: Int): Int = do let a: Int = lhs + rhs; let b: Int = a + lhs; + return b; +end; + +-- Entry point (maybe) +fun main: Void = do + print(add(34, 35)); + return 0; end; \ No newline at end of file