<s> """<STR_LIT>""" <EOL> from . import ast <EOL> from . import util <EOL> from . tiler import ASTPattern , SimplePattern , tile <EOL> EQUALITY = ( "<STR_LIT:=>" , "<STR_LIT>" ) <EOL> INEQUALITY = ( "<STR_LIT>" , ) <EOL> def canonicalize ( node ) : <EOL> """<STR_LIT>""" <EOL> def replace_func ( pattern , n ) : <EOL> l_literal = isinstance ( n . left , ast . Literal ) <EOL> r_literal = isinstance ( n . right , ast . Literal ) <EOL> if not l_literal and r_literal : <EOL> n . reverse ( ) <EOL> elif l_literal and r_literal : <EOL> if n . left . static and not n . right . static : <EOL> n . reverse ( ) <EOL> elif not n . left . static and not n . right . static and n . left . value > n . right . value : <EOL> n . reverse ( ) <EOL> elif n . left . static and n . right . static and n . left . value > n . right . value : <EOL> n . reverse ( ) <EOL> p = SimplePattern ( "<STR_LIT>" ) <EOL> return tile ( node , [ p ] , replace_func ) <EOL> def select_rewrite_expression ( name , exprs ) : <EOL> """<STR_LIT>""" <EOL> if name [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> values = [ e . right . value for e in exprs ] <EOL> filter_using = util . mode ( values ) <EOL> for e in exprs : <EOL> if e . right . value == filter_using : <EOL> return e <EOL> elif name [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> is_static = name [ <NUM_LIT:3> ] [ <NUM_LIT:1> ] == "<STR_LIT>" <EOL> values = [ e . right . value for e in exprs ] <EOL> if is_static : <EOL> filter_using = util . median ( values ) <EOL> else : <EOL> filter_using = util . mode ( values ) <EOL> for e in exprs : <EOL> if e . right . value == filter_using : <EOL> return e <EOL> else : <EOL> return exprs [ <NUM_LIT:0> ] <EOL> def compare_rewrite ( node , name , expr , assumed_result ) : <EOL> """<STR_LIT>""" <EOL> if name [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> return equality_rewrite ( node , name , expr , assumed_result ) <EOL> elif name [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> return order_rewrite ( node , name , expr , assumed_result ) <EOL> else : <EOL> assert False , "<STR_LIT>" <EOL> def equality_rewrite ( node , name , expr , assumed_result ) : <EOL> static_value = expr . right . value <EOL> is_static = expr . right . static <EOL> if expr . type in EQUALITY : <EOL> known = assumed_result <EOL> else : <EOL> known = not assumed_result <EOL> def replace_func ( pattern , node ) : <EOL> static_match = node . right . value == static_value <EOL> is_static_node = node . right . static <EOL> const = None <EOL> if known and is_static and is_static_node : <EOL> if node . type in EQUALITY : <EOL> const = static_match <EOL> else : <EOL> const = not static_match <EOL> elif static_match : <EOL> if node . type in EQUALITY : <EOL> const = known <EOL> else : <EOL> const = not known <EOL> return ast . Constant ( const ) if const is not None else None <EOL> pattern = SimplePattern ( "<STR_LIT>" , ASTPattern ( expr . left ) ) <EOL> return tile ( node , [ pattern ] , replace_func ) <EOL> def order_rewrite ( node , name , expr , assumed_result ) : <EOL> static_value = expr . right . value <EOL> numeric = isinstance ( expr . right , ast . Number ) <EOL> less_than = "<STR_LIT:<>" in expr . type <EOL> maybe_equals = "<STR_LIT:=>" in expr . type <EOL> if less_than : <EOL> if assumed_result : <EOL> min_bound = float ( "<STR_LIT>" ) <EOL> min_incl = False <EOL> max_bound = static_value <EOL> max_incl = maybe_equals <EOL> else : <EOL> min_bound = static_value <EOL> min_incl = not maybe_equals <EOL> max_bound = float ( "<STR_LIT>" ) <EOL> max_incl = False <EOL> else : <EOL> if assumed_result : <EOL> min_bound = static_value <EOL> min_incl = maybe_equals <EOL> max_bound = float ( "<STR_LIT>" ) <EOL> max_incl = False <EOL> else : <EOL> min_bound = float ( "<STR_LIT>" ) <EOL> min_incl = False <EOL> max_bound = static_value <EOL> max_incl = not maybe_equals <EOL> if not assumed_result : <EOL> less_than = not less_than <EOL> maybe_equals = not maybe_equals <EOL> def replace_func ( pattern , node ) : <EOL> node_val = node . right . value <EOL> if not numeric and node_val != static_value : <EOL> return None <EOL> assert_less = "<STR_LIT:<>" in node . type <EOL> assert_equals = "<STR_LIT:=>" in node . type <EOL> if not numeric : <EOL> const = ( less_than == assert_less ) <EOL> if maybe_equals : <EOL> const = const and assert_equals <EOL> else : <EOL> const = None <EOL> if assert_less : <EOL> if node_val > max_bound : <EOL> const = True <EOL> elif node_val == max_bound and max_incl and assert_equals : <EOL> const = True <EOL> elif node_val == max_bound and not max_incl : <EOL> const = True <EOL> elif node_val < min_bound : <EOL> const = False <EOL> elif node_val == min_bound and not assert_equals : <EOL> const = False <EOL> elif node_val == min_bound and assert_equals and not min_incl : <EOL> const = False <EOL> else : <EOL> if node_val < min_bound : <EOL> const = True <EOL> elif node_val == min_bound and not min_incl : <EOL> const = True <EOL> elif node_val == min_bound and min_incl and assert_equals : <EOL> const = True <EOL> elif node_val > max_bound : <EOL> const = False <EOL> elif node_val == max_bound and not assert_equals : <EOL> const = False <EOL> elif node_val == max_bound and assert_equals and not max_incl : <EOL> const = False <EOL> if const is None : <EOL> return None <EOL> return ast . Constant ( const ) <EOL> pattern = SimplePattern ( "<STR_LIT>" , <EOL> ASTPattern ( expr . left ) , "<STR_LIT>" if numeric else "<STR_LIT>" ) <EOL> return tile ( node , [ pattern ] , replace_func ) </s>
