Skip to content

Commit

Permalink
Add support for lambdas and "sortedBy"
Browse files Browse the repository at this point in the history
  • Loading branch information
aranega committed Sep 18, 2024
1 parent 2a5382e commit 5cb44c3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
16 changes: 15 additions & 1 deletion pyecoreocl/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ def visitArgumentsExp(self, ctx):
self.inline(", ")

def visitLambdaExp(self, ctx):
return self.visitChildren(ctx)
# return self.visitChildren(ctx)
variables = [arg.text for arg in ctx.varnames]
varnames = ", ".join(variables)
self.inline(f"(lambda {varnames}: ")
self.visit(ctx.oclExp())
self.inline(")")

def visitNestedExp(self, ctx):
self.inline("(")
Expand Down Expand Up @@ -496,6 +501,15 @@ def rule_any(emitter, ctx):
emitter.inline("), None)")


@call_rule
def rule_sorted_by(emitter, ctx):
emitter.inline("sorted(")
emitter.visit(ctx.expression)
emitter.inline(", key=")
emitter.visit(ctx.argExp())
emitter.inline(")")


def default_collection_call(emitter, ctx):
operation = ctx.attname.text
emitter.visit(ctx.expression)
Expand Down
7 changes: 6 additions & 1 deletion tests/test_collection_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ def test__one():
l = [3, 4, 5]
assert !l->one(e | e = 3)! is True

assert !Bag{3, 4, 5}->collect(e | e + 1)->one(e | e = 3)! is False
assert !Bag{3, 4, 5}->collect(e | e + 1)->one(e | e = 3)! is False


def test__sorted_by():
l = [3, 4, 5]
assert !l->sortedBy(e | -e)! == [5, 4, 3]

0 comments on commit 5cb44c3

Please sign in to comment.