Skip to content

Commit

Permalink
Support dot-broadcasting in ljl_propfunc
Browse files Browse the repository at this point in the history
  • Loading branch information
oschulz committed Dec 4, 2024
1 parent 028c9dc commit 3e4b1db
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/ljl_expressions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,36 @@ function _process_ljlexpr_impl(@nospecialize(expr::Expr), @nospecialize(f_varsub
_process_inner = Base.Fix2(_process_ljlexpr_impl, f_varsubst)
if expr.head in ljl_expr_allowed_heads
if expr.head == :.
obj = expr.args[begin]
propname = expr.args[begin+1]
return Expr(expr.head, _process_ljlexpr_impl(obj, f_varsubst), propname)
if length(expr.args) == 1
arg1 = only(expr.args)
if arg1 isa Symbol

Check warning on line 102 in src/ljl_expressions.jl

View check run for this annotation

Codecov / codecov/patch

src/ljl_expressions.jl#L101-L102

Added lines #L101 - L102 were not covered by tests
# Standalone dot-operator syntax:
expr

Check warning on line 104 in src/ljl_expressions.jl

View check run for this annotation

Codecov / codecov/patch

src/ljl_expressions.jl#L104

Added line #L104 was not covered by tests
else
throw(ArgumentError("LEGEND Julia expressions don't support `$expr`"))

Check warning on line 106 in src/ljl_expressions.jl

View check run for this annotation

Codecov / codecov/patch

src/ljl_expressions.jl#L106

Added line #L106 was not covered by tests
end
elseif length(expr.args) == 2
arg1 = expr.args[begin]
arg2 = expr.args[begin+1]
if arg2 isa Union{Symbol,QuoteNode}
# Property access
return Expr(expr.head, _process_ljlexpr_impl(arg1, f_varsubst), arg2)
elseif arg2 isa Expr && arg2.head == :tuple
# Broadcast syntax
return Expr(expr.head, arg1, Expr(:tuple, map(_process_inner, arg2.args)...))
else
throw(ArgumentError("LEGEND Julia expressions don't support `$expr`"))

Check warning on line 118 in src/ljl_expressions.jl

View check run for this annotation

Codecov / codecov/patch

src/ljl_expressions.jl#L118

Added line #L118 was not covered by tests
end
else
throw(ArgumentError("LEGEND Julia expressions don't support `$expr`"))

Check warning on line 121 in src/ljl_expressions.jl

View check run for this annotation

Codecov / codecov/patch

src/ljl_expressions.jl#L121

Added line #L121 was not covered by tests
end
elseif expr.head == :call
funcname = expr.args[begin]
funcname_str = string(funcname)
# Handle constructs like `a .+ b`:
base_funcname = funcname_str[begin] == '.' ? Symbol(funcname_str[begin+1:end]) : funcname
funcargs = expr.args[begin+1:end]
if funcname in ljl_expr_allowed_funcs
if base_funcname in ljl_expr_allowed_funcs
return Expr(expr.head, funcname, map(_process_inner, funcargs)...)
else
throw(ArgumentError("Function \"$(funcname)\" not allowed in LEGEND Julia expression."))
Expand Down
3 changes: 3 additions & 0 deletions test/test_ljl_expressions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,7 @@ include("testing_utils.jl")
multi_pf = ljl_propfunc(expr_map)
@test multi_pf isa PropertyFunctions.PropertyFunction
@test @inferred(broadcast(multi_pf, data)) == StructArray(e_cal = ref_numfunc.(data), e_flag = ref_boolfunc.(data))

@test @inferred(ljl_propfunc("a.+b")((a = [1,2,3], b = [5, 6, 7]))) == [6, 8, 10]
@test @inferred(ljl_propfunc("log.(a)")((a = [1,2,3],))) [0.0, 0.6931471805599453, 1.0986122886681098]
end

0 comments on commit 3e4b1db

Please sign in to comment.