Changeset 322 for proto/parabix2


Ignore:
Timestamp:
Oct 25, 2009, 7:26:36 AM (10 years ago)
Author:
cameron
Message:

Extend PyBit? to include loops of form while B: S

Location:
proto/parabix2
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • proto/parabix2/Compiler/bitexpr.py

    r319 r322  
    11#
    22# bitexpr.py - bitstream expressions in Python
     3#
     4# (c) 2009 Robert D. Cameron with modifications by Ehsan Amiri
     5# All rights reserved.
     6# Licensed to International Characters, Inc. under Academic Free License 3.0
    37#
    48# Class BitExpr provides a model for symbolic expressions
     
    1721
    1822class BitExpr:
    19    """The BitExpr class and its Sbbclasses provide a symbolic
     23   """The BitExpr class and its subclasses provide a symbolic
    2024      representation of Bitwise logical expressions.
    2125   """
     
    9397    def show(self): return 'Sel(%s, %s, %s)' % (self.sel.show(), self.true_branch.show(), self.false_branch.show())
    9498    def show_C(self): return 'Sel(%s, %s, %s)' % (self.sel.show_C(), self.true_branch.show_C(), self.false_branch.show_C())
     99
    95100class Add(BitExpr):
    96101    def __init__(self, expr1, expr2, carry = None):
     
    119124    pass
    120125
    121 class BitAssign:
     126class BitAssign(BitStmt):
    122127    def __init__(self, var, expr):
    123128        self.LHS = var
     
    125130    def show(self):
    126131            if self.LHS is None:
    127                 return "%s"%self.RHS.show_C()
     132                return "%s"%self.RHS.show()
    128133            else:
    129                 return '%s = %s' % (self.LHS.show_C(), self.RHS.show_C())
     134                return '%s = %s' % (self.LHS.show(), self.RHS.show())
     135
     136class StmtList(BitStmt):
     137    def __init__(self, stmts):
     138        self.stmts = stmts
     139    def show(self):
     140        rslt = ''
     141        for s in self.stmts: rslt += s.show() + '\n'
     142        return rslt
     143
     144
     145#
     146# A PyBit WhileLoop of the form while B: S
     147# will execute statements S repeatedly while bitstream expression
     148# B evaluates to a nonzero bitstream (i.e., has any bits)
     149#
     150
     151class WhileLoop(BitStmt):
     152    def __init__(self, expr, stmts):
     153        self.control_expr = expr
     154        self.loop_body = stmts
     155    def show(self):
     156        rslt = ''
     157        for s in self.loop_body: rslt += s.show() + '\n'
     158        return 'while (%s) {%s}' % (self.control_expr.show(), rslt)
     159
    130160
    131161#
  • proto/parabix2/Compiler/bitstream_compiler.py

    r320 r322  
    399399        s=r"""def u8u16(u8, u8bit):
    400400        u8.unibyte = (~u8bit[0]);
    401         optimize(u8.unibyte, allone)
     401        while x:
     402                x = bitutil.Advance(x)
     403                y |= x
     404        optimize(u8bit[0], allzero)
    402405        u8.prefix = (u8bit[0] & u8bit[1]);
    403406        u8.prefix2 = (u8.prefix &~ u8bit[2]);
  • proto/parabix2/Compiler/py2bitexpr.py

    r321 r322  
    11#
    22# py2bitexpr.py
     3#
     4# (c) 2009 Robert D. Cameron with modifications by Ehsan Amiri
     5# All rights reserved.
     6# Licensed to International Characters, Inc. under Academic Free License 3.0
    37#
    48# Translate unbounded bitstream code in "PyBit" form into our internal
     
    111115                        v = bitexpr.Var(translate_var(s.target))
    112116                        translated.append(bitexpr.BitAssign(v, translate(ast.BinOp(s.target, s.op, s.value))))
     117                elif isinstance(s, ast.While):
     118                        e = translate(s.test)
     119                        body = translate_stmts(s.body)
     120                        translated.append(bitexpr.WhileLoop(e, body))
    113121                else: raise PyBitError("Unknown PyBit statement type %s\n" % ast.dump(s))
    114122        return translated
  • proto/parabix2/parabix2.py

    r315 r322  
    166166        CtCDPI_Cursor = bitutil.ScanThru(CtCDPI_Cursor, CtCDPI_scan)
    167167        CtCDPI_Cursor &= EOF_mask
    168         while CtCDPI_Cursor > 0:
     168        while CtCDPI_Cursor:
    169169                CtCDPI_starts |= CtCDPI_Cursor
    170170                PI_Cursor = CtCDPI_Cursor & lex.PI_start
     
    456456        # iterations is the maximum number of attributes found in any one
    457457        # start tag, plus one.
    458         while AttNameStart > 0:
     458        while AttNameStart:
    459459                AttNameStarts |= AttNameStart
    460460                AttNameFollow = bitutil.ScanThru(AttNameStart, lex.NameScan)
Note: See TracChangeset for help on using the changeset viewer.