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,
|
KwLet, KwFun,
|
||||||
KwDo, KwEnd,
|
KwDo, KwEnd,
|
||||||
KwIf, KwThen, KwElse,
|
KwIf, KwThen, KwElse,
|
||||||
|
KwReturn,
|
||||||
|
|
||||||
// Literals
|
// Literals
|
||||||
Int(i64), Float(String), Boolean(bool),
|
Int(i64), Float(String), Boolean(bool),
|
||||||
|
@ -32,6 +33,7 @@ impl std::fmt::Display for Token {
|
||||||
Token::KwIf => write!(f, "if"),
|
Token::KwIf => write!(f, "if"),
|
||||||
Token::KwThen => write!(f, "then"),
|
Token::KwThen => write!(f, "then"),
|
||||||
Token::KwElse => write!(f, "else"),
|
Token::KwElse => write!(f, "else"),
|
||||||
|
Token::KwReturn => write!(f, "return"),
|
||||||
|
|
||||||
Token::Int(i) => write!(f, "{}", i),
|
Token::Int(i) => write!(f, "{}", i),
|
||||||
Token::Float(s) => write!(f, "{}", s),
|
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,
|
"if" => Token::KwIf,
|
||||||
"then" => Token::KwThen,
|
"then" => Token::KwThen,
|
||||||
"else" => Token::KwElse,
|
"else" => Token::KwElse,
|
||||||
|
"return" => Token::KwReturn,
|
||||||
_ => Token::Identifier(s),
|
_ => Token::Identifier(s),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,7 @@ fn main() {
|
||||||
.with_message(format!(
|
.with_message(format!(
|
||||||
"{}, expected {}",
|
"{}, expected {}",
|
||||||
|
|
||||||
if e.found().is_some() {"Unexpected token in input" }
|
if e.found().is_some() { "Unexpected token in input" }
|
||||||
else { "Unexpected end of input" },
|
else { "Unexpected end of input" },
|
||||||
|
|
||||||
if e.expected().len() == 0 { "something else".to_string().fg(Color::Green) }
|
if e.expected().len() == 0 { "something else".to_string().fg(Color::Green) }
|
||||||
|
@ -84,6 +84,9 @@ fn main() {
|
||||||
.fg(Color::Red)
|
.fg(Color::Red)
|
||||||
))
|
))
|
||||||
.with_color(Color::Red)
|
.with_color(Color::Red)
|
||||||
|
)
|
||||||
|
.with_help(
|
||||||
|
"You might have forgotten to end a previous line with semicolon"
|
||||||
),
|
),
|
||||||
_ => {
|
_ => {
|
||||||
println!("{:?}", e);
|
println!("{:?}", e);
|
||||||
|
|
|
@ -11,7 +11,7 @@ pub enum Expr {
|
||||||
Unary { op: String, rhs: Box<Spanned<Self>> },
|
Unary { op: String, rhs: Box<Spanned<Self>> },
|
||||||
Binary { lhs: Box<Spanned<Self>>, 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>>> },
|
Call { name: Box<Spanned<Self>>, args: Spanned<Vec<Spanned<Self>>> },
|
||||||
|
|
||||||
Let {
|
Let {
|
||||||
name: String,
|
name: String,
|
||||||
type_hint: String,
|
type_hint: String,
|
||||||
|
@ -23,7 +23,8 @@ pub enum Expr {
|
||||||
args: Spanned<Vec<(Spanned<String>, Spanned<String>)>>,
|
args: Spanned<Vec<(Spanned<String>, Spanned<String>)>>,
|
||||||
body: Box<Spanned<Self>>
|
body: Box<Spanned<Self>>
|
||||||
},
|
},
|
||||||
|
Return { expr: Box<Spanned<Self>> },
|
||||||
|
|
||||||
If {
|
If {
|
||||||
cond: Box<Spanned<Self>>,
|
cond: Box<Spanned<Self>>,
|
||||||
then: 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)
|
let do_block = just(Token::KwDo)
|
||||||
.ignore_then(
|
.ignore_then(
|
||||||
expr.clone()
|
expr.clone()
|
||||||
|
@ -211,6 +223,7 @@ fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
|
||||||
|
|
||||||
let_
|
let_
|
||||||
.or(fun)
|
.or(fun)
|
||||||
|
.or(return_)
|
||||||
.or(do_block)
|
.or(do_block)
|
||||||
.or(compare)
|
.or(compare)
|
||||||
}).labelled("expression");
|
}).labelled("expression");
|
||||||
|
|
|
@ -7,4 +7,11 @@ fun add (lhs: Int) (rhs: Int): Int = lhs + rhs;
|
||||||
fun add_2 (lhs: Int) (rhs: Int): Int = do
|
fun add_2 (lhs: Int) (rhs: Int): Int = do
|
||||||
let a: Int = lhs + rhs;
|
let a: Int = lhs + rhs;
|
||||||
let b: Int = a + lhs;
|
let b: Int = a + lhs;
|
||||||
|
return b;
|
||||||
|
end;
|
||||||
|
|
||||||
|
-- Entry point (maybe)
|
||||||
|
fun main: Void = do
|
||||||
|
print(add(34, 35));
|
||||||
|
return 0;
|
||||||
end;
|
end;
|
Loading…
Reference in a new issue