fixed return precedence and operator parsing

This commit is contained in:
Matthieu Jolimaitre 2024-10-25 13:58:45 +02:00
parent 3868298f00
commit 7febf91d3c
2 changed files with 10 additions and 3 deletions

7
example/data/fibo.mc Normal file
View file

@ -0,0 +1,7 @@
fn fibo(n) {
if (n < 1) return n;
return fibo(n - 1) + fibo(n - 2);
}
fibo(3)

View file

@ -220,8 +220,8 @@ impl ShortDef {
pub fn parser(expr: impl Parse<Expr>) -> impl Parse<Self> { pub fn parser(expr: impl Parse<Expr>) -> impl Parse<Self> {
(lx("break").map(|_| Self::Break)) (lx("break").map(|_| Self::Break))
.or(lx("continue").map(|_| Self::Continue)) .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").ignore_then(expr).map(Self::Return))
.or(lx("return").map(|_| Self::Return(Expr::Litteral(Litteral::None))))
} }
pub fn eval(&self, context: &mut Context) -> Result<Short, EvalError> { pub fn eval(&self, context: &mut Context) -> Result<Short, EvalError> {
@ -345,10 +345,10 @@ impl Op {
.or(lx("*").map(|_| Self::Prod)) .or(lx("*").map(|_| Self::Prod))
.or(lx("==").map(|_| Self::Eq)) .or(lx("==").map(|_| Self::Eq))
.or(lx("!=").map(|_| Self::Neq)) .or(lx("!=").map(|_| Self::Neq))
.or(lx("<").map(|_| Self::Lt))
.or(lx(">").map(|_| Self::Gt))
.or(lx("<=").map(|_| Self::Leq)) .or(lx("<=").map(|_| Self::Leq))
.or(lx(">=").map(|_| Self::Geq)) .or(lx(">=").map(|_| Self::Geq))
.or(lx("<").map(|_| Self::Lt))
.or(lx(">").map(|_| Self::Gt))
} }
pub fn apply(&self, l: &Value, r: &Value) -> Result<Value, EvalError> { pub fn apply(&self, l: &Value, r: &Value) -> Result<Value, EvalError> {