PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
Public Member Functions | Static Protected Member Functions
picurv_cli.source_expressions._LogicalRewriter Class Reference

Rewrite logical operators and chained comparisons into array operations. More...

Inheritance diagram for picurv_cli.source_expressions._LogicalRewriter:
[legend]
Collaboration diagram for picurv_cli.source_expressions._LogicalRewriter:
[legend]

Public Member Functions

 visit_Call (self, node)
 Rewrite sum(k, lo, hi, body) as a loop over lambda k: body.
 
 visit_BoolOp (self, node)
 Fold a and b and c into nested logical calls.
 
 visit_UnaryOp (self, node)
 Rewrite not a as logical_not(a).
 
 visit_Compare (self, node)
 Rewrite a < b < c as logical_and(a < b, b < c).
 

Static Protected Member Functions

 _call (name, args)
 Build a call to one of the evaluation-only logical helpers.
 

Detailed Description

Rewrite logical operators and chained comparisons into array operations.

and, or, not and a chain a < b < c have no elementwise meaning for arrays, so they become logical_and, logical_or, logical_not calls, the same 1-or-0 semantics the runtime evaluator gives them.

Definition at line 197 of file source_expressions.py.

Member Function Documentation

◆ _call()

picurv_cli.source_expressions._LogicalRewriter._call (   name,
  args 
)
staticprotected

Build a call to one of the evaluation-only logical helpers.

Parameters
[in]nameHelper name.
[in]argsArgument nodes.
Returns
Call node.

Definition at line 206 of file source_expressions.py.

206 def _call(name, args):
207 """!
208 @brief Build a call to one of the evaluation-only logical helpers.
209 @param[in] name Helper name.
210 @param[in] args Argument nodes.
211 @return Call node.
212 """
213 return ast.Call(func=ast.Name(id=name, ctx=ast.Load()), args=list(args), keywords=[])
214
Head of a generic C-style linked list.
Definition variables.h:478
Here is the caller graph for this function:

◆ visit_Call()

picurv_cli.source_expressions._LogicalRewriter.visit_Call (   self,
  node 
)

Rewrite sum(k, lo, hi, body) as a loop over lambda k: body.

Parameters
[in]nodeCall node.
Returns
Rewritten node.

Definition at line 215 of file source_expressions.py.

215 def visit_Call(self, node):
216 """!
217 @brief Rewrite `sum(k, lo, hi, body)` as a loop over `lambda k: body`.
218 @param[in] node Call node.
219 @return Rewritten node.
220 """
221 self.generic_visit(node)
222 if not (isinstance(node.func, ast.Name) and node.func.id == "sum"):
223 return node
224 index, lower, upper, body = node.args
225 loop = ast.Lambda(args=ast.arguments(posonlyargs=[], args=[ast.arg(arg=index.id)], vararg=None,
226 kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]),
227 body=body)
228 return self._call("series", [loop, lower, upper])
229
Here is the call graph for this function:

◆ visit_BoolOp()

picurv_cli.source_expressions._LogicalRewriter.visit_BoolOp (   self,
  node 
)

Fold a and b and c into nested logical calls.

Parameters
[in]nodeBoolOp node.
Returns
Rewritten node.

Definition at line 230 of file source_expressions.py.

230 def visit_BoolOp(self, node):
231 """!
232 @brief Fold `a and b and c` into nested logical calls.
233 @param[in] node BoolOp node.
234 @return Rewritten node.
235 """
236 self.generic_visit(node)
237 name = "logical_and" if isinstance(node.op, ast.And) else "logical_or"
238 result = node.values[0]
239 for value in node.values[1:]:
240 result = self._call(name, [result, value])
241 return result
242
Here is the call graph for this function:

◆ visit_UnaryOp()

picurv_cli.source_expressions._LogicalRewriter.visit_UnaryOp (   self,
  node 
)

Rewrite not a as logical_not(a).

Parameters
[in]nodeUnaryOp node.
Returns
Rewritten node.

Definition at line 243 of file source_expressions.py.

243 def visit_UnaryOp(self, node):
244 """!
245 @brief Rewrite `not a` as `logical_not(a)`.
246 @param[in] node UnaryOp node.
247 @return Rewritten node.
248 """
249 self.generic_visit(node)
250 if isinstance(node.op, ast.Not):
251 return self._call("logical_not", [node.operand])
252 return node
253
Here is the call graph for this function:

◆ visit_Compare()

picurv_cli.source_expressions._LogicalRewriter.visit_Compare (   self,
  node 
)

Rewrite a < b < c as logical_and(a < b, b < c).

Parameters
[in]nodeCompare node.
Returns
Rewritten node.

Definition at line 254 of file source_expressions.py.

254 def visit_Compare(self, node):
255 """!
256 @brief Rewrite `a < b < c` as `logical_and(a < b, b < c)`.
257 @param[in] node Compare node.
258 @return Rewritten node.
259 """
260 self.generic_visit(node)
261 if len(node.ops) == 1:
262 return node
263 operands = [node.left] + node.comparators
264 pairs = [ast.Compare(left=copy.deepcopy(operands[i]), ops=[op], comparators=[copy.deepcopy(operands[i + 1])])
265 for i, op in enumerate(node.ops)]
266 result = pairs[0]
267 for pair in pairs[1:]:
268 result = self._call("logical_and", [result, pair])
269 return result
270
271
Here is the call graph for this function:

The documentation for this class was generated from the following file: