From 5ffd57a1a49b201bbe7bb416de6bb5c8d92b02bf Mon Sep 17 00:00:00 2001 From: sigmasoldi3r Date: Mon, 30 Oct 2023 19:00:05 +0100 Subject: [PATCH] fix: operator precedence & not equal code generation --- examples/loops.saturn | 3 ++- src/lua.rs | 11 ++++++++--- src/parser/mod.rs | 4 ++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/examples/loops.saturn b/examples/loops.saturn index 842214b..e46e262 100644 --- a/examples/loops.saturn +++ b/examples/loops.saturn @@ -6,11 +6,12 @@ let tbl = { c: "x" }; +// For-each for (k, v) in entries(tbl) { print(k, v); } - +// Range based for for i in 1..4 { print(i); } diff --git a/src/lua.rs b/src/lua.rs index 1361555..089019b 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -623,7 +623,6 @@ impl code::Visitor for LuaEmitter { | "<" | "<=" | "==" - | "~=" // Logic | "not" | "and" @@ -640,13 +639,19 @@ impl code::Visitor for LuaEmitter { let ctx = ctx.put(op.to_owned()); self.visit_expression(ctx.put(" "), &expr.right) } - "++"=> { + "++" => { // Native-to-native operator translation let ctx = self.visit_expression(ctx, &expr.left)?.put(" "); let ctx = ctx.put("..".to_owned()); self.visit_expression(ctx.put(" "), &expr.right) } - _=> { + "<>" => { + // Native-to-native operator translation + let ctx = self.visit_expression(ctx, &expr.left)?.put(" "); + let ctx = ctx.put("~=".to_owned()); + self.visit_expression(ctx.put(" "), &expr.right) + } + _ => { // Direct function translation let ctx = translate_operator(ctx, op.to_owned()).put("("); let ctx = self.visit_expression(ctx, &expr.left)?.put(", "); diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 915b865..3bd5aad 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -264,11 +264,11 @@ peg::parser! { { ClassField::Operator(OperatorOverload { operator, arguments, body }) } rule assign_extra() -> Operator - = value:$("+") { Operator(value.into()) } + = value:$("++") { Operator(value.into()) } + / value:$("+") { Operator(value.into()) } / value:$("-") { Operator(value.into()) } / value:$("*") { Operator(value.into()) } / value:$("/") { Operator(value.into()) } - / value:$("++") { Operator(value.into()) } rule argument_list() -> Vec = "(" _ args:argument() ** (_ "," _) _ ")" { args }