mirror of
https://github.com/azur1s/bobbylisp.git
synced 2024-10-16 02:37:40 -05:00
infix expr
This commit is contained in:
parent
d841f5d2c1
commit
49716a8192
|
@ -7,4 +7,7 @@ func foo :: (a, b) = {
|
||||||
func main :: () = {
|
func main :: () = {
|
||||||
// if else in variable definition
|
// if else in variable definition
|
||||||
let cond_str :: String = if true { return "t" } else { return "f" };
|
let cond_str :: String = if true { return "t" } else { return "f" };
|
||||||
|
|
||||||
|
// Infix operator
|
||||||
|
let n :: Bool = 2 == 2;
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,7 +6,7 @@ use nom::{
|
||||||
error::{Error, ErrorKind},
|
error::{Error, ErrorKind},
|
||||||
IResult,
|
IResult,
|
||||||
multi::many0,
|
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};
|
use super::model::{Token, Tokens, Precedence, Infix, Program, Stmt, Expr, Ident, Literal};
|
||||||
|
@ -112,6 +112,22 @@ fn parse_call_expr(input: Tokens, func_handle: Expr) -> IResult<Tokens, Expr> {
|
||||||
)(input)
|
)(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> {
|
fn parse_expr(input: Tokens, precedence: Precedence, left: Expr) -> IResult<Tokens, Expr> {
|
||||||
let (i1, t1) = take(1usize)(input)?;
|
let (i1, t1) = take(1usize)(input)?;
|
||||||
|
|
||||||
|
@ -124,9 +140,8 @@ fn parse_expr(input: Tokens, precedence: Precedence, left: Expr) -> IResult<Toke
|
||||||
parse_expr(i2, precedence, left2)
|
parse_expr(i2, precedence, left2)
|
||||||
},
|
},
|
||||||
(ref peek, _) if precedence < *peek => {
|
(ref peek, _) if precedence < *peek => {
|
||||||
// let (i2, left2) = parse_infix_expr(input, left)?;
|
let (i2, left2) = parse_infix_expr(input, left)?;
|
||||||
// parse_expr(i2, precedence, left2)
|
parse_expr(i2, precedence, left2)
|
||||||
todo!()
|
|
||||||
},
|
},
|
||||||
_ => Ok((input, left)),
|
_ => Ok((input, left)),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue