nope lmao

main
annieversary 2021-09-26 15:58:55 +02:00
parent b4cb20b968
commit 200bd1d702
2 changed files with 64 additions and 18 deletions

View File

@ -2,9 +2,10 @@ mod ast;
mod parser; mod parser;
fn main() { fn main() {
let a = "hello( )"; let a = r#"funFun("ss" + 12, true)"#;
let (_rest, res) = parser::expr(a).unwrap(); let (rest, res) = parser::expr(a).unwrap();
dbg!(res); dbg!(res);
dbg!(rest);
println!("Hello, world!"); println!("Hello, world!");
} }

View File

@ -72,13 +72,19 @@ fn parse_str<'a>(i: &'a str) -> IResult<&'a str, &'a str> {
escaped(alphanumeric1, '\\', one_of("\"n\\"))(i) escaped(alphanumeric1, '\\', one_of("\"n\\"))(i)
} }
fn string<'a>(i: &'a str) -> IResult<&'a str, &'a str> { fn string<'a>(i: &'a str) -> IResult<&'a str, Expr> {
preceded(char('\"'), cut(terminated(parse_str, char('\"'))))(i) map(
preceded(char('\"'), cut(terminated(parse_str, char('\"')))),
|s| Expr::LStr(String::from(s)),
)(i)
} }
fn boolean<'a>(i: &'a str) -> IResult<&'a str, bool> { fn boolean<'a>(i: &'a str) -> IResult<&'a str, Expr> {
println!("boolean: {}", i); println!("boolean: {}", i);
alt((map(tag("true"), |_| true), map(tag("false"), |_| false)))(i) alt((
map(tag("true"), |_| Expr::LBool(true)),
map(tag("false"), |_| Expr::LBool(false)),
))(i)
} }
fn call<'a>(i: &'a str) -> IResult<&'a str, (Identifier, Vec<Expr>)> { fn call<'a>(i: &'a str) -> IResult<&'a str, (Identifier, Vec<Expr>)> {
@ -100,13 +106,13 @@ fn num(i: &str) -> IResult<&str, Expr> {
println!("num: {}", i); println!("num: {}", i);
map(double, Expr::LNum)(i) map(double, Expr::LNum)(i)
} }
fn literal(i: &str) -> IResult<&str, Expr> {
fn parens_bin(i: &str) -> IResult<&str, Expr> { println!("literal: {}", i);
delimited(space0, delimited(tag("("), expr, tag(")")), space0)(i) alt((num, string, map(tag("null"), |_| Expr::LNull), boolean))(i)
} }
fn factor_bin(i: &str) -> IResult<&str, Expr> { fn factor_bin(i: &str) -> IResult<&str, Expr> {
alt((delimited(space0, num, space0), parens_bin))(i) alt((delimited(space0, literal, space0), expr))(i)
} }
fn bin_less_greater(i: &str) -> IResult<&str, Expr> { fn bin_less_greater(i: &str) -> IResult<&str, Expr> {
@ -178,18 +184,17 @@ fn receive<'a>(i: &'a str) -> IResult<&'a str, Expr> {
pub fn expr<'a>(i: &'a str) -> IResult<&'a str, Expr> { pub fn expr<'a>(i: &'a str) -> IResult<&'a str, Expr> {
println!("expr: {}", i); println!("expr: {}", i);
preceded( delimited(
sc, sc,
alt(( alt((
parens(expr), parens(expr),
map(call, |(i, v)| Expr::Call(i, v)), map(call, |(i, v)| Expr::Call(i, v)),
map(identifier, |i| Expr::Var(i.to_string())),
receive, receive,
binary, binary,
map(tag("null"), |_| Expr::LNull), literal,
map(string, |s| Expr::LStr(String::from(s))), map(identifier, |i| Expr::Var(i.to_string())),
num,
)), )),
sc,
)(i) )(i)
} }
@ -202,7 +207,7 @@ mod tests {
let a = r#""hey" "#; let a = r#""hey" "#;
let (rest, res) = string(a).unwrap(); let (rest, res) = string(a).unwrap();
assert_eq!(res, "hey"); assert_eq!(res, Expr::LStr("hey".to_string()));
assert_eq!(rest, " "); assert_eq!(rest, " ");
} }
@ -225,6 +230,29 @@ mod tests {
); );
} }
#[test]
fn can_parse_binary_op_with_str() {
let a = r#"3 +3 + 6 + "hey""#;
let (_rest, res) = binary(a).unwrap();
assert_eq!(
res,
Expr::Binary(
BinOp::Plus,
Box::new(Expr::Binary(
BinOp::Plus,
Box::new(Expr::Binary(
BinOp::Plus,
Box::new(Expr::LNum(3.0)),
Box::new(Expr::LNum(3.0)),
)),
Box::new(Expr::LNum(6.0))
)),
Box::new(Expr::LStr("hey".to_string()))
)
);
}
#[test] #[test]
fn can_parse_empty_call() { fn can_parse_empty_call() {
let a = "hello()"; let a = "hello()";
@ -277,7 +305,24 @@ mod tests {
} }
#[test] #[test]
fn can_parse() { fn can_parse_call_with_string_binop() {
let a = r#"let hello = ("hey" + 0.5)"#; let a = r#"funFun("ss" + 12, true)"#;
let (rest, res) = expr(a).unwrap();
assert_eq!(
res,
Expr::Call(
"funFun".to_string(),
vec![
Expr::Binary(
BinOp::Plus,
Box::new(Expr::LStr("ss".to_string())),
Box::new(Expr::LNum(12.0))
),
Expr::LBool(true)
]
)
);
assert_eq!(rest, "");
} }
} }