@@ -4,25 +4,27 @@ using DispatchDoctor: @unstable
44
55using .. NodeModule: AbstractExpressionNode, Node, constructorof
66using .. OperatorEnumModule: AbstractOperatorEnum
7- using .. OperatorEnumConstructionModule: empty_all_globals!
7+ using .. OperatorEnumConstructionModule: OperatorEnum, empty_all_globals!
88using .. ExpressionModule: AbstractExpression, Expression
99
1010"""
1111 @parse_expression(expr; operators, variable_names, node_type=Node, evaluate_on=[])
1212
13- Parse a symbolic expression `expr` into a computational graph where nodes represent operations or variables.
13+ (Experimental) Parse a symbolic expression `expr` into a computational graph where nodes represent operations or variables.
1414
1515## Arguments
1616
1717- `expr`: An expression to parse into an `AbstractExpression`.
1818
1919## Keyword Arguments
2020
21- - `operators`: An instance of `OperatorEnum ` specifying the available unary and binary operators.
21+ - `operators`: An instance of `AbstractOperatorEnum ` specifying the available unary and binary operators.
2222- `variable_names`: A list of variable names as strings or symbols that are allowed in the expression.
2323- `evaluate_on`: A list of external functions to evaluate explicitly when encountered.
2424- `node_type`: The type of the nodes in the resulting expression tree. Defaults to `Node`.
2525- `expression_type`: The type of the resulting expression. Defaults to `Expression`.
26+ - `binary_operators`: Convenience syntax for creating an `OperatorEnum`.
27+ - `unary_operators`: Convenience syntax for creating an `OperatorEnum`.
2628
2729## Usage
2830
105107 expression_type = Expression
106108 evaluate_on = nothing
107109 extra_metadata = ()
110+ binops = nothing
111+ unaops = nothing
108112
109113 # Iterate over keyword arguments to extract operators and variable_names
110114 for kw in kws
127131 elseif kw == :extra_metadata
128132 extra_metadata = kw
129133 continue
134+ elseif kw == :binary_operators
135+ binops = kw
136+ continue
137+ elseif kw == :unary_operators
138+ unaops = kw
139+ continue
130140 end
131141 elseif kw isa Expr && kw. head == :(= )
132142 if kw. args[1 ] == :operators
147157 elseif kw. args[1 ] == :extra_metadata
148158 extra_metadata = kw. args[2 ]
149159 continue
160+ elseif kw. args[1 ] == :binary_operators
161+ binops = kw. args[2 ]
162+ continue
163+ elseif kw. args[1 ] == :unary_operators
164+ unaops = kw. args[2 ]
165+ continue
150166 end
151167 end
152168 throw (
156172 )
157173 end
158174
159- # Ensure that operators are provided
160- @assert operators != = nothing " The 'operators' keyword argument must be provided."
175+ if operators === nothing
176+ @assert (
177+ binops != = nothing || unaops != = nothing ,
178+ " You must specify the operators using either `operators`, or `binary_operators` and `unary_operators`"
179+ )
180+ operators = :($ (OperatorEnum)(;
181+ binary_operators= $ (binops === nothing ? :(Function[]) : binops),
182+ unary_operators= $ (unaops === nothing ? :(Function[]) : unaops),
183+ ))
184+ else
185+ @assert (binops === nothing && unaops === nothing )
186+ end
187+
161188 return (;
162189 operators, variable_names, node_type, expression_type, evaluate_on, extra_metadata
163190 )
0 commit comments