mirror of
https://github.com/azur1s/bobbylisp.git
synced 2024-10-16 02:37:40 -05:00
if else and commenting
This commit is contained in:
parent
20decd8677
commit
d841f5d2c1
|
@ -1,10 +1,10 @@
|
|||
func join :: (a, b) = {
|
||||
let result :: String = from(a, b);
|
||||
return result
|
||||
// user defined function
|
||||
func foo :: (a, b) = {
|
||||
return a;
|
||||
};
|
||||
|
||||
// entry point
|
||||
func main :: () = {
|
||||
let foo :: String = join("f", "oo");
|
||||
print(foo);
|
||||
return 0
|
||||
// if else in variable definition
|
||||
let cond_str :: String = if true { return "t" } else { return "f" };
|
||||
};
|
|
@ -1,6 +1,6 @@
|
|||
use nom::{
|
||||
branch::alt,
|
||||
bytes::complete::{tag, take},
|
||||
bytes::complete::{tag, take, take_until},
|
||||
character::complete::{multispace0, alphanumeric1, alpha1, digit1},
|
||||
combinator::{map, map_res, recognize},
|
||||
IResult,
|
||||
|
@ -119,15 +119,27 @@ fn lex_illegal(input: &Bytes) -> IResult<&Bytes, Token> {
|
|||
map(take(1usize), |_| Token::Illegal)(input)
|
||||
}
|
||||
|
||||
fn lex_comment(input: &Bytes) -> IResult<&Bytes, ()> {
|
||||
let (i1, c1) = take(2usize)(input)?;
|
||||
if c1.as_bytes() == b"//" {
|
||||
let (i2, _) = take_until("\n")(i1)?;
|
||||
let (i3, _) = take(1usize)(i2)?;
|
||||
let (i4, _) = multispace0(i3)?;
|
||||
let (i5, _) = lex_comment(i4)?;
|
||||
Ok((i5, ()))
|
||||
} else { Ok((input, ())) }
|
||||
}
|
||||
|
||||
// Tokens
|
||||
fn lex_token(input: &Bytes) -> IResult<&Bytes, Token> {
|
||||
let (i1, _) = lex_comment(input)?;
|
||||
alt((
|
||||
lex_operator_punctuation,
|
||||
lex_string,
|
||||
lex_reserved_identifier,
|
||||
lex_string,
|
||||
lex_integer,
|
||||
lex_illegal,
|
||||
))(input)
|
||||
))(i1)
|
||||
}
|
||||
|
||||
fn lex_tokens(input: &Bytes) -> IResult<&Bytes, Vec<Token>> {
|
||||
|
|
|
@ -22,6 +22,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_if, Token::If);
|
||||
tag_token!(tag_else, Token::Else);
|
||||
|
||||
tag_token!(tag_assign, Token::Assign);
|
||||
tag_token!(tag_typehint, Token::Typehint);
|
||||
|
@ -71,6 +73,7 @@ fn parse_atom_expr(input: Tokens) -> IResult<Tokens, Expr> {
|
|||
alt((
|
||||
parse_literal_expr,
|
||||
parse_ident_expr,
|
||||
parse_if_expr,
|
||||
))(input)
|
||||
}
|
||||
|
||||
|
@ -130,6 +133,22 @@ fn parse_expr(input: Tokens, precedence: Precedence, left: Expr) -> IResult<Toke
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_if_expr(input: Tokens) -> IResult<Tokens, Expr> {
|
||||
map(
|
||||
tuple((
|
||||
tag_if,
|
||||
parse_expr_lowest,
|
||||
parse_block_stmt,
|
||||
parse_else_expr,
|
||||
)),
|
||||
|(_, cond, then, else_)| Expr::If { cond: Box::new(cond), then, else_ },
|
||||
)(input)
|
||||
}
|
||||
|
||||
fn parse_else_expr(input: Tokens) -> IResult<Tokens, Option<Program>> {
|
||||
opt(preceded(tag_else, parse_block_stmt))(input)
|
||||
}
|
||||
|
||||
fn parse_comma_exprs(input: Tokens) -> IResult<Tokens, Expr> {
|
||||
preceded(tag_comma, parse_expr_lowest)(input)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue