Skip to content

Commit 7c62924

Browse files
committed
feat: allow passing String to parse_expression
1 parent 0023938 commit 7c62924

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

src/Parse.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ end
242242
end
243243
end
244244

245+
@unstable parse_expression(ex::String; kws...) = parse_expression(Meta.parse(ex); kws...)
246+
245247
"""
246248
Find an operator function by its name in the OperatorEnum, considering the arity.
247249
Throws appropriate errors for ambiguous or missing matches.

test/test_parse.jl

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -354,21 +354,21 @@ end
354354

355355
# Test nested custom operators
356356
ex = parse_expression(
357-
"custom_max(custom_cos(x1), custom_mul(x2, x3), x4 + 1.5)";
357+
"custom_max(custom_cos(x1), custom_mul(x2, x3), x2 + 1.5)";
358358
operators=operators,
359+
node_type=Node{T,3} where {T},
359360
variable_names=["x1", "x2", "x3"],
360361
)
361-
@test typeof(ex) <: Expression
362-
@test string_tree(ex) == "custom_max(custom_cos(x1), custom_mul(x2, x3), x4 + 1.5)"
362+
@test typeof(ex) <: Expression{Float64}
363+
@test string_tree(ex) == "custom_max(custom_cos(x1), custom_mul(x2, x3), x2 + 1.5)"
364+
@test ex([1.0 2.0 3.0]') == [6.0]
363365

364366
# Test error cases for _find_operator_by_name
365367
@test_throws(
366368
ArgumentError(
367369
"Tried to interpolate function `unknown_func` but failed. Function not found in operators.",
368370
),
369-
parse_expression(
370-
Meta.parse("unknown_func(x1)"), operators=operators, variable_names=["x1"]
371-
)
371+
parse_expression("unknown_func(x1)", operators=operators, variable_names=["x1"])
372372
)
373373

374374
# Test ambiguous operator - same name from different modules
@@ -381,9 +381,7 @@ end
381381
same_name_ops = OperatorEnum(1 => [TestMod1.foo, TestMod2.foo])
382382
@test_throws(
383383
r"Ambiguous operator `foo` with arity 1\. Multiple matches found: Tuple\{Function, Int64\}\[.*foo.*1.*foo.*1.*\]",
384-
parse_expression(
385-
Meta.parse("foo(x1)"), operators=same_name_ops, variable_names=["x1"]
386-
)
384+
parse_expression("foo(x1)", operators=same_name_ops, variable_names=["x1"])
387385
)
388386

389387
# Test wrong arity
@@ -392,9 +390,7 @@ end
392390
"Operator `custom_cos` found but not with arity 2. Available arities: [1]"
393391
),
394392
parse_expression(
395-
Meta.parse("custom_cos(x1, x2)"),
396-
operators=operators,
397-
variable_names=["x1", "x2"],
393+
"custom_cos(x1, x2)", operators=operators, variable_names=["x1", "x2"]
398394
)
399395
)
400396
end

0 commit comments

Comments
 (0)