Skip to content

Commit

Permalink
fix: operator precedence & not equal code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmasoldi3r committed Oct 30, 2023
1 parent 40d6ba7 commit 5ffd57a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
3 changes: 2 additions & 1 deletion examples/loops.saturn
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
11 changes: 8 additions & 3 deletions src/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,6 @@ impl code::Visitor<code::Builder> for LuaEmitter {
| "<"
| "<="
| "=="
| "~="
// Logic
| "not"
| "and"
Expand All @@ -640,13 +639,19 @@ impl code::Visitor<code::Builder> 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(", ");
Expand Down
4 changes: 2 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Argument>
= "(" _ args:argument() ** (_ "," _) _ ")" { args }
Expand Down

0 comments on commit 5ffd57a

Please sign in to comment.