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

Compare commits

..

No commits in common. "49716a8192ca05125fd9160380a2ebab792b56af" and "20decd8677debaed05f28cbf950eb25d3be6422e" have entirely different histories.

3 changed files with 14 additions and 63 deletions

View file

@ -1,13 +1,10 @@
// user defined function
func foo :: (a, b) = {
return a;
func join :: (a, b) = {
let result :: String = from(a, b);
return result
};
// entry point
func main :: () = {
// if else in variable definition
let cond_str :: String = if true { return "t" } else { return "f" };
// Infix operator
let n :: Bool = 2 == 2;
};
let foo :: String = join("f", "oo");
print(foo);
return 0
};

View file

@ -1,6 +1,6 @@
use nom::{
branch::alt,
bytes::complete::{tag, take, take_until},
bytes::complete::{tag, take},
character::complete::{multispace0, alphanumeric1, alpha1, digit1},
combinator::{map, map_res, recognize},
IResult,
@ -119,27 +119,15 @@ 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_reserved_identifier,
lex_string,
lex_reserved_identifier,
lex_integer,
lex_illegal,
))(i1)
))(input)
}
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}, error_position,
sequence::{terminated, tuple, pair, preceded, delimited},
};
use super::model::{Token, Tokens, Precedence, Infix, Program, Stmt, Expr, Ident, Literal};
@ -22,8 +22,6 @@ 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);
@ -73,7 +71,6 @@ fn parse_atom_expr(input: Tokens) -> IResult<Tokens, Expr> {
alt((
parse_literal_expr,
parse_ident_expr,
parse_if_expr,
))(input)
}
@ -112,22 +109,6 @@ 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)?;
@ -140,30 +121,15 @@ 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)
// let (i2, left2) = parse_infix_expr(input, left)?;
// parse_expr(i2, precedence, left2)
todo!()
},
_ => 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)
}