Skip to content

Commit 23b1bd3

Browse files
committed
feat: allow binary_operators passed to parse_expression
1 parent b06d82f commit 23b1bd3

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/Parse.jl

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ using ..ExpressionModule: AbstractExpression, Expression
1616
1717
## Keyword Arguments
1818
19-
- `operators`: An instance of `OperatorEnum` specifying the available unary and binary operators.
19+
- `operators`: An instance of `AbstractOperatorEnum` specifying the available unary and binary operators.
2020
- `variable_names`: A list of variable names as strings or symbols that are allowed in the expression.
2121
- `evaluate_on`: A list of external functions to evaluate explicitly when encountered.
2222
- `node_type`: The type of the nodes in the resulting expression tree. Defaults to `Node`.
2323
- `expression_type`: The type of the resulting expression. Defaults to `Expression`.
24+
- `binary_operators`: Convenience syntax for creating an `OperatorEnum`.
25+
- `unary_operators`: Convenience syntax for creating an `OperatorEnum`.
2426
2527
## Usage
2628
@@ -103,6 +105,8 @@ function _parse_kws(kws)
103105
expression_type = Expression
104106
evaluate_on = nothing
105107
extra_metadata = ()
108+
binops = nothing
109+
unaops = nothing
106110

107111
# Iterate over keyword arguments to extract operators and variable_names
108112
for kw in kws
@@ -125,6 +129,12 @@ function _parse_kws(kws)
125129
elseif kw == :extra_metadata
126130
extra_metadata = kw
127131
continue
132+
elseif kw == :binary_operators
133+
binops = kw
134+
continue
135+
elseif kw == :unary_operators
136+
unaops = kw
137+
continue
128138
end
129139
elseif kw isa Expr && kw.head == :(=)
130140
if kw.args[1] == :operators
@@ -145,6 +155,12 @@ function _parse_kws(kws)
145155
elseif kw.args[1] == :extra_metadata
146156
extra_metadata = kw.args[2]
147157
continue
158+
elseif kw.args[1] == :binary_operators
159+
binops = kw.args[2]
160+
continue
161+
elseif kw.args[1] == :unary_operators
162+
unaops = kw.args[2]
163+
continue
148164
end
149165
end
150166
throw(
@@ -154,8 +170,19 @@ function _parse_kws(kws)
154170
)
155171
end
156172

157-
# Ensure that operators are provided
158-
@assert operators !== nothing "The 'operators' keyword argument must be provided."
173+
if operators === nothing
174+
@assert(
175+
binops !== nothing || unaops !== nothing,
176+
"You must specify the operators using either `operators`, or `binary_operators` and `unary_operators`"
177+
)
178+
operators = :($(OperatorEnum)(;
179+
binary_operators=$(binops === nothing ? :(Function[]) : binops),
180+
unary_operators=$(unaops === nothing ? :(Function[]) : unaops),
181+
))
182+
else
183+
@assert (binops === nothing && unaops === nothing)
184+
end
185+
159186
return (;
160187
operators, variable_names, node_type, expression_type, evaluate_on, extra_metadata
161188
)

0 commit comments

Comments
 (0)