From 7febf91d3cee6daf28da783800d51b49c1f90729 Mon Sep 17 00:00:00 2001 From: Matthieu Jolimaitre Date: Fri, 25 Oct 2024 13:58:45 +0200 Subject: [PATCH] fixed return precedence and operator parsing --- example/data/fibo.mc | 7 +++++++ src/ast.rs | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 example/data/fibo.mc diff --git a/example/data/fibo.mc b/example/data/fibo.mc new file mode 100644 index 0000000..3086e76 --- /dev/null +++ b/example/data/fibo.mc @@ -0,0 +1,7 @@ + +fn fibo(n) { + if (n < 1) return n; + return fibo(n - 1) + fibo(n - 2); +} + +fibo(3) \ No newline at end of file diff --git a/src/ast.rs b/src/ast.rs index 663dfb6..f67e374 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -220,8 +220,8 @@ impl ShortDef { pub fn parser(expr: impl Parse) -> impl Parse { (lx("break").map(|_| Self::Break)) .or(lx("continue").map(|_| Self::Continue)) - .or(lx("return").map(|_| Self::Return(Expr::Litteral(Litteral::None)))) .or(lx("return").ignore_then(expr).map(Self::Return)) + .or(lx("return").map(|_| Self::Return(Expr::Litteral(Litteral::None)))) } pub fn eval(&self, context: &mut Context) -> Result { @@ -345,10 +345,10 @@ impl Op { .or(lx("*").map(|_| Self::Prod)) .or(lx("==").map(|_| Self::Eq)) .or(lx("!=").map(|_| Self::Neq)) - .or(lx("<").map(|_| Self::Lt)) - .or(lx(">").map(|_| Self::Gt)) .or(lx("<=").map(|_| Self::Leq)) .or(lx(">=").map(|_| Self::Geq)) + .or(lx("<").map(|_| Self::Lt)) + .or(lx(">").map(|_| Self::Gt)) } pub fn apply(&self, l: &Value, r: &Value) -> Result {