mirror of
https://github.com/azur1s/bobbylisp.git
synced 2024-10-16 02:37:40 -05:00
return keyword
This commit is contained in:
parent
b19769e1ee
commit
23f1edfea4
|
@ -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<char, Vec<(Token, Span)>, Error = Simple<char>> {
|
|||
"if" => Token::KwIf,
|
||||
"then" => Token::KwThen,
|
||||
"else" => Token::KwElse,
|
||||
"return" => Token::KwReturn,
|
||||
_ => Token::Identifier(s),
|
||||
});
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -11,7 +11,7 @@ pub enum Expr {
|
|||
Unary { op: String, rhs: Box<Spanned<Self>> },
|
||||
Binary { lhs: Box<Spanned<Self>>, op: String, rhs: Box<Spanned<Self>> },
|
||||
Call { name: Box<Spanned<Self>>, args: Spanned<Vec<Spanned<Self>>> },
|
||||
|
||||
|
||||
Let {
|
||||
name: String,
|
||||
type_hint: String,
|
||||
|
@ -23,7 +23,8 @@ pub enum Expr {
|
|||
args: Spanned<Vec<(Spanned<String>, Spanned<String>)>>,
|
||||
body: Box<Spanned<Self>>
|
||||
},
|
||||
|
||||
Return { expr: Box<Spanned<Self>> },
|
||||
|
||||
If {
|
||||
cond: Box<Spanned<Self>>,
|
||||
then: Box<Spanned<Self>>,
|
||||
|
@ -193,6 +194,17 @@ fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
|
|||
)
|
||||
});
|
||||
|
||||
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<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
|
|||
|
||||
let_
|
||||
.or(fun)
|
||||
.or(return_)
|
||||
.or(do_block)
|
||||
.or(compare)
|
||||
}).labelled("expression");
|
||||
|
|
|
@ -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;
|
Loading…
Reference in a new issue