1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

Compare commits

..

2 commits

Author SHA1 Message Date
Natapat Samutpong 49716a8192 infix expr 2022-02-12 20:21:42 +07:00
Natapat Samutpong d841f5d2c1 if else and commenting 2022-02-12 20:17:13 +07:00
3 changed files with 63 additions and 14 deletions

View file

@ -1,10 +1,13 @@
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" };
// Infix operator
let n :: Bool = 2 == 2;
};

View file

@ -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>> {

View file

@ -6,7 +6,7 @@ use nom::{
error::{Error, ErrorKind},
IResult,
multi::many0,
sequence::{terminated, tuple, pair, preceded, delimited},
sequence::{terminated, tuple, pair, preceded, delimited}, error_position,
};
use super::model::{Token, Tokens, Precedence, Infix, Program, Stmt, Expr, Ident, Literal};
@ -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)
}
@ -109,6 +112,22 @@ fn parse_call_expr(input: Tokens, func_handle: Expr) -> IResult<Tokens, Expr> {
)(input)
}
fn parse_infix_expr(input: Tokens, left: Expr) -> IResult<Tokens, Expr> {
let (i1, t1) = take(1usize)(input)?;
if t1.tokens.is_empty() { Err(Err::Error(error_position!(input, ErrorKind::Tag))) }
else {
let next = &t1.tokens[0];
let (prec, op) = infix_operator(next);
match op {
None => Err(Err::Error(error_position!(input, ErrorKind::Tag))),
Some(op) => {
let (i2, right) = parse_expr_with(i1, prec)?;
Ok((i2, Expr::Infix(op, Box::new(left), Box::new(right))))
}
}
}
}
fn parse_expr(input: Tokens, precedence: Precedence, left: Expr) -> IResult<Tokens, Expr> {
let (i1, t1) = take(1usize)(input)?;
@ -121,15 +140,30 @@ fn parse_expr(input: Tokens, precedence: Precedence, left: Expr) -> IResult<Toke
parse_expr(i2, precedence, left2)
},
(ref peek, _) if precedence < *peek => {
// let (i2, left2) = parse_infix_expr(input, left)?;
// parse_expr(i2, precedence, left2)
todo!()
let (i2, left2) = parse_infix_expr(input, left)?;
parse_expr(i2, precedence, left2)
},
_ => Ok((input, left)),
}
}
}
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)
}