Skip to content

Commit

Permalink
fix: handling of function names with "lambda" in them (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
dexter2206 authored Oct 9, 2024
1 parent c03e5f0 commit 63ad716
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/bartiq/symbolics/ast_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
_NAMESPACE_IDENTIFIER = rf"{_IDENTIFIER}(\.{_IDENTIFIER})*"
_PORT_PATTERN = rf"#({_NAMESPACE_IDENTIFIER})"
_WILDCARD_PATTERN = rf"(({_IDENTIFIER})?)~"
_LAMBDA_PATTERN = r"(?<![_A-Za-z])lambda(?![A-Za-z])"
_IN_PATTERN = r"(^|[^\w])in($|[^\w])"

_RESTRICTED_NAMES = {"__lambda__": "lambda", "__in__": "in"}
Expand Down Expand Up @@ -133,7 +134,7 @@ def _contains_lambda(expression):


def _replace_lambda(expression):
return expression.replace("lambda", "__lambda__")
return re.sub(_LAMBDA_PATTERN, "__lambda__", expression)


# Preprocessing stage replacing symbols named lambda with symbols named __lambda__
Expand Down
2 changes: 2 additions & 0 deletions tests/symbolics/test_sympy_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ def add_routine_path(symbol):
("a.in", Symbol("a.in")),
("a.in.b", Symbol("a.in.b")),
("in.b", Symbol("in.b")),
# Lambda as a part of function name
("calc_lambda(x)", Function("calc_lambda")(Symbol("x"))),
]


Expand Down

0 comments on commit 63ad716

Please sign in to comment.