diff --git a/axc/foo.axs b/axc/foo.axs index 2f6e30a..24d6fba 100644 --- a/axc/foo.axs +++ b/axc/foo.axs @@ -1,4 +1,8 @@ // AlexScript def double x = x * 2; def tuple = (atan2 x y + z, thing); -def negatives = 2 + ~3; + +def unary = -2; +def minus = 2 - 2; +def unary_app = 2 (- 2); +def unary_double = 2 - - 2; diff --git a/axc/src/parser.rs b/axc/src/parser.rs index 834600c..b28693a 100644 --- a/axc/src/parser.rs +++ b/axc/src/parser.rs @@ -334,10 +334,7 @@ fn parse_expression<'a>( let subscript = parse_subscript_expr(m, base); let term = choice((lambda, let_, match_, record, subscript, tuple)); - // let unary = parse_unary(m, term); - let unary = term; - - let application = unary.repeated().at_least(1).map(|exprs| { + let application = term.repeated().at_least(1).map(|exprs| { exprs .into_iter() .reduce(|l, r| Expr::Application { @@ -347,7 +344,11 @@ fn parse_expression<'a>( .unwrap() }); - let binary = (0..=10).rev().fold(application.boxed(), |p, precedence| { + // let unary = parse_unary(m, term); + // let unary = term; + let unary = parse_unary(m, application); + + let binary = (0..=10).rev().fold(unary.boxed(), |p, precedence| { parse_binary(m, precedence, p).boxed() }); @@ -355,20 +356,20 @@ fn parse_expression<'a>( }) } -// fn parse_unary( -// _m: &ParserMeta, -// base: impl Parser> + Clone, -// ) -> impl Parser> + Clone { -// pad(just("-").to("-")) -// .repeated() -// .then(base.clone()) -// .map(|(ops, exp)| { -// ops.into_iter().fold(exp, |exp, op| Expr::UnaryOp { -// kind: op.to_string(), -// val: Box::new(exp), -// }) -// }) -// } +fn parse_unary( + _m: &ParserMeta, + base: impl Parser> + Clone, +) -> impl Parser> + Clone { + pad(just("-").to("-")) + .repeated() + .then(base.clone()) + .map(|(ops, exp)| { + ops.into_iter().fold(exp, |exp, op| Expr::UnaryOp { + kind: op.to_string(), + val: Box::new(exp), + }) + }) +} fn parse_binary<'a>( m: &'a ParserMeta, @@ -565,13 +566,10 @@ fn parse_tuple_expr( } fn parse_var_ref_expr(_m: &ParserMeta) -> impl Parser> + Clone { - choice(( - pad(just("~")).to(Expr::VariableReference(vec!["~".to_string()])), - pad(ident()) - .separated_by(pad(just("::"))) - .at_least(1) - .map(Expr::VariableReference), - )) + pad(ident()) + .separated_by(pad(just("::"))) + .at_least(1) + .map(Expr::VariableReference) } fn parse_literal(_m: &ParserMeta) -> impl Parser> + Clone {