Ignore:
Timestamp:
Aug 1, 2014, 7:38:10 AM (5 years ago)
Author:
cameron
Message:

Refactor pulling out bitwise_expr module; prepare toAst method

File:
1 edited

Legend:

Unmodified
Added
Removed
  • proto/charsetcompiler/charset_compiler.py

    r3928 r3942  
    2424import EBCDIC
    2525
     26
    2627import charset_def
    2728CharDef = charset_def.CharDef
     
    3637Encoding_Type = UTF_encoding.UTF_Encoding_Type
    3738
    38 #
    39 # Symbolic Representations of Boolean Expressions
    40 #
    41 
    42 class BoolExpr:
    43    """The BoolExpr class and its subclasses provide a symbolic
    44       representation of Boolean expressions.
    45    """
    46    pass
    47 
    48 class Var(BoolExpr):
    49     def __init__(self, varname):
    50         self.varname = varname
    51     def show(self): return 'Var("' + self.varname + '")'
    52     def __str__(self): return 'Var("' + self.varname + '")'
    53 
    54 class TrueLiteral(BoolExpr):
    55     def __init__(self):
    56         self.value = True
    57     def show(self): return 'T'
    58     def __str__(self): return 'T'
    59 
    60 class FalseLiteral(BoolExpr):
    61     def __init__(self):
    62         self.value = False
    63     def show(self): return 'F'
    64 
    65 class Not(BoolExpr):
    66     def __init__(self, expr):
    67         self.operand = expr
    68     def show(self): return 'Not(%s)' % (self.operand.show())
    69     def __str__(self): return 'Not(%s)' % (self.operand.show())
    70 
    71 class And(BoolExpr):
    72     def __init__(self, expr1, expr2):
    73         self.operand1 = expr1
    74         self.operand2 = expr2
    75     def show(self): return 'And(%s, %s)' % (self.operand1.show(), self.operand2.show())
    76     def __str__(self): return 'And(%s, %s)' % (self.operand1.show(), self.operand2.show())
    77 
    78 class Or(BoolExpr):
    79     def __init__(self, expr1, expr2):
    80         self.operand1 = expr1
    81         self.operand2 = expr2
    82     def show(self): return 'Or(%s, %s)' % (self.operand1.show(), self.operand2.show())
    83 
    84 class Xor(BoolExpr):
    85     def __init__(self, expr1, expr2):
    86         self.operand1 = expr1
    87         self.operand2 = expr2
    88     def show(self): return 'Xor(%s, %s)' % (self.operand1.show(), self.operand2.show())
    89 
    90 class Sel(BoolExpr):
    91     def __init__(self, expr1, expr2, expr3):
    92         self.sel = expr1
    93         self.true_branch = expr2
    94         self.false_branch = expr3
    95     def show(self): return 'Sel(%s, %s, %s)' % (self.sel.show(), self.true_branch.show(), self.false_branch.show())
    96     def __str__(self): return 'Sel(%s, %s, %s)' % (self.sel.show(), self.true_branch.show(), self.false_branch.show())
    97 
    98 
    99 #
    100 # Optimizing Constructors for Boolean Expressions
    101 # - Maintaining Assembler Instruction Form:
    102 #   - All boolean algebraic rules involving true/false applied.
    103 #   - Negations restricted:
    104 #       - no negations within or (DeMorgan's to nand)
    105 #       - at most one negation within and
    106 #
    107 
    108 def make_not(expr):
    109     if isinstance(expr, TrueLiteral):
    110         return FalseLiteral()
    111     elif isinstance(expr, FalseLiteral):
    112         return TrueLiteral()
    113     elif isinstance(expr, Not):
    114         return expr.operand
    115     else: return Not(expr)
    116 
    117 def make_and(expr1, expr2):
    118     if isinstance(expr1, TrueLiteral):
    119         return expr2
    120     elif isinstance(expr2, TrueLiteral):
    121         return expr1
    122     elif isinstance(expr1, FalseLiteral):
    123         return FalseLiteral()
    124     elif isinstance(expr2, FalseLiteral):
    125         return FalseLiteral()
    126     elif equal_exprs(expr1, expr2): return expr1
    127     elif isinstance(expr1, Not):
    128         if isinstance(expr2, Not):
    129             return make_not(make_or(expr1.operand, expr2.operand))
    130         elif equal_exprs(expr1.operand, expr2): return FalseLiteral()
    131         else: return And(expr1, expr2)
    132     elif isinstance(expr2, Not):
    133         if equal_exprs(expr1, expr2.operand): return FalseLiteral()
    134         else: return And(expr1, expr2)
    135     else: return And(expr1, expr2)
    136 
    137 def make_or(expr1, expr2):
    138     if isinstance(expr1, FalseLiteral):
    139         return expr2
    140     elif isinstance(expr2, FalseLiteral):
    141         return expr1
    142     elif isinstance(expr1, TrueLiteral):
    143         return TrueLiteral()
    144     elif isinstance(expr2, TrueLiteral):
    145         return TrueLiteral()
    146     elif isinstance(expr1, Not):
    147         return make_not(make_and(expr1.operand, make_not(expr2)))
    148     elif isinstance(expr2, Not):
    149         return make_not(make_and(make_not(expr1), expr2.operand))
    150     elif equal_exprs(expr1, expr2): return expr1
    151     elif isinstance(expr1, And) and isinstance(expr2, And):
    152         # These optimizations factor out common components that
    153         # can occur when sets are formed by union (e.g., union of
    154         # [a-z] and [A-Z].
    155         if equal_exprs(expr1.operand1, expr2.operand1):
    156             return make_and(expr1.operand1, make_or(expr1.operand2, expr2.operand2))
    157         elif equal_exprs(expr1.operand2, expr2.operand2):
    158             return make_and(expr1.operand2, make_or(expr1.operand1, expr2.operand1))
    159         elif equal_exprs(expr1.operand1, expr2.operand2):
    160             return make_and(expr1.operand1, make_or(expr1.operand2, expr2.operand1))
    161         elif equal_exprs(expr1.operand2, expr2.operand1):
    162             return make_and(expr1.operand2, make_or(expr1.operand1, expr2.operand2))
    163         else: return Or(expr1, expr2)
    164     else: return Or(expr1, expr2)
    165 
    166 def make_sel(if_expr, T_expr, F_expr):
    167     if isinstance(if_expr, TrueLiteral):
    168         return T_expr
    169     elif isinstance(if_expr, FalseLiteral):
    170         return F_expr
    171     elif isinstance(T_expr, TrueLiteral):
    172         # if x then T else y = x or y
    173         return make_or(if_expr, F_expr) 
    174     elif isinstance(T_expr, FalseLiteral):
    175         # if x then F else y = not(x) and y
    176         return make_and(make_not(if_expr), F_expr) 
    177     elif isinstance(F_expr, FalseLiteral):
    178         # if x then y else F = x and y
    179         return make_and(if_expr, T_expr) 
    180     elif isinstance(F_expr, TrueLiteral):
    181         # if x then y else T = not(x) or y
    182         return make_or(make_not(if_expr), T_expr)
    183     elif equal_exprs(T_expr, F_expr):
    184         return T_expr
    185     else: return Sel(if_expr, T_expr, F_expr)
    186 
    187 
    188 def make_xor(expr1, expr2):
    189     if isinstance(expr1, FalseLiteral):
    190         return expr2
    191     elif isinstance(expr2, FalseLiteral):
    192         return expr1
    193     elif isinstance(expr1, TrueLiteral):
    194         return make_not(expr2)
    195     elif isinstance(expr2, TrueLiteral):
    196         return make_not(expr1)
    197     elif isinstance(expr1, Not) and isinstance(expr2, Not):
    198         return make_xor(expr1.operand, expr2.operand)
    199     else: return Xor(expr1, expr2)
    200 
    201 # Return True if e1 and e2 can be proven equivalent according
    202 # to some rules, False otherwise.   Note that False may
    203 # be returned in some cases when the exprs are equivalent.
    204 def equal_exprs(e1, e2):
    205     if isinstance(e1, FalseLiteral): return isinstance(e2, FalseLiteral)
    206     elif isinstance(e1, TrueLiteral): return isinstance(e2, TrueLiteral)
    207     elif isinstance(e1, Var) and isinstance(e2, Var):
    208         return e1.varname == e2.varname
    209     elif isinstance(e1, Not) and isinstance(e2, Not):
    210         return equal_exprs(e1.operand, e2.operand)
    211     elif isinstance(e1, And) and isinstance(e2, And):
    212         if equal_exprs(e1.operand1, e2.operand1):
    213             return equal_exprs(e1.operand2, e2.operand2)
    214         elif equal_exprs(e1.operand1, e2.operand2):
    215             return equal_exprs(e1.operand2, e2.operand1)
    216         else: return False
    217     elif isinstance(e1, Or) and isinstance(e2, Or):
    218         if equal_exprs(e1.operand1, e2.operand1):
    219             return equal_exprs(e1.operand2, e2.operand2)
    220         elif equal_exprs(e1.operand1, e2.operand2):
    221             return equal_exprs(e1.operand2, e2.operand1)
    222         else: return False
    223     elif isinstance(e1, Xor) and isinstance(e2, Xor):
    224         if equal_exprs(e1.operand1, e2.operand1):
    225             return equal_exprs(e1.operand2, e2.operand2)
    226         elif equal_exprs(e1.operand1, e2.operand2):
    227             return equal_exprs(e1.operand2, e2.operand1)
    228         else: return False
    229     elif isinstance(e1, Sel) and isinstance(e2, Sel):
    230         if equal_exprs(e1.sel, e2.sel):
    231              if equal_exprs(e1.true_branch, e2.true_branch):
    232                  return equal_exprs(e1.false_branch, e2.false_branch)
    233              else: return False
    234         else: return False
    235 
    236 #
    237 #
    238 #  Boolean Expressions for Character Class Definitions
    239 #
     39from bitwise_expr import *
     40
    24041
    24142def bit_var(n):
     
    28081            elif n > 23:
    28182                return UTF_encoding.Encoding.basis_pattern[2] % (n - 24)
    282 
    28383
    28484def make_bitv(n):
Note: See TracChangeset for help on using the changeset viewer.