[Wisp-cvs] wisp/users/dig tran.py,1.58,1.59
Status: Alpha
Brought to you by:
digg
From: <di...@us...> - 2003-04-22 15:40:24
|
Update of /cvsroot/wisp/wisp/users/dig In directory sc8-pr-cvs1:/tmp/cvs-serv28272 Modified Files: tran.py Log Message: major cleanup Index: tran.py =================================================================== RCS file: /cvsroot/wisp/wisp/users/dig/tran.py,v retrieving revision 1.58 retrieving revision 1.59 diff -u -d -r1.58 -r1.59 --- tran.py 22 Apr 2003 15:34:53 -0000 1.58 +++ tran.py 22 Apr 2003 15:40:19 -0000 1.59 @@ -46,14 +46,6 @@ return str.__new__(cls, s) def __repr__ (this): return 'Symbol_Literal(%r)' % str(this) - def __add__ (this, that): - if not isinstance(this, Constant_Sum): this = Constant_Sum(this) - if not isinstance(that, Constant_Sum): that = Constant_Sum(that) - return this + that - def __sub__ (this, that): - if not isinstance(this, Constant_Sum): this = Constant_Sum(this) - if not isinstance(that, Constant_Sum): that = Constant_Sum(that) - return this - that def get_symbols (this): return this, @@ -73,65 +65,6 @@ Semicolon = Unique_Token(';') Dollar = Dollar_Type() -class Constant_Sum (Stackable): - def __init__ (this, plus = (), minus = (), scalar = 0L): - this.scalar = long(scalar) - if isinstance(plus, Symbol_Literal) or plus == Dollar: - plus = plus, - if isinstance(minus, Symbol_Literal) or minus == Dollar: - minus = minus, - if isinstance(plus, Integer_Literal): - this.scalar += long(plus); plus = () - if isinstance(minus, Integer_Literal): - this.scalar -= long(minus); minus = () - this.plus = []; this.minus = [] - for item in plus: - if isinstance(item, Symbol_Literal) or item == Dollar: - this.plus.append(item) - else: raise 'bad addee', item - for item in minus: - if isinstance(item, Symbol_Literal) or item == Dollar: - try: - i = this.plus.index(item) - this.plus.pop(i) - except ValueError: - this.minus.append(item) - else: raise 'bad subtractee', item - this.plus = tuple(this.plus); this.minus = tuple(this.minus) - def __add__ (this, that): - if isinstance(that, Symbol_Literal) or that == Dollar: - that = Constant_Sum(plus = [that]) - return Constant_Sum(plus = this.plus + that.plus, - minus = this.minus + that.minus, - scalar = this.scalar + that.scalar).simplify() - def __sub__ (this, that): - if isinstance(that, Symbol_Literal) or that == Dollar: - that = Constant_Sum(plus = [that]) - return Constant_Sum(plus = this.plus + that.minus, - minus = this.minus + that.plus, - scalar = this.scalar - that.scalar).simplify() - def __repr__ (this): - arg = [] - if this.plus: arg.append('plus = ' + `this.plus`) - if this.minus: arg.append('minus = ' + `this.minus`) - if this.scalar: arg.append('scalar = ' + `this.scalar`) - return 'Constant_Sum(%s)' % ', '.join(arg) - def __str__ (this): - res = '' - for item in this.plus: res += ' + ' + str(item) - for item in this.minus: res += ' - ' + str(item) - if this.scalar > 0: res += ' + ' + str(this.scalar) - else: res += ' - ' + str(-this.scalar) - if res[0] == ' ': res = res[1:] - if res[:2] == '+ ': res = res[2:] - return '(' + res + ')' - def simplify (this): - if len(this.plus) == 0 and len(this.minus) == 0: - return Integer_Literal(this.scalar) - if len(this.plus) == 1 and len(this.minus) == 0 and this.scalar == 0: - return this.plus[0] - else: return this - class Lexer (shlex): def __init__ (this, filename): shlex.__init__(this, instream = open(filename, 'r'), infile = filename) @@ -195,51 +128,6 @@ def __init__ (this, id): this.id = id -class Sum (list): - def __init__ (this, addends, scalar = 0, dollars = 0): - list.__init__(this, []) - this.scalar = long(scalar) - this.dollars = long(dollars) - for a in addends: - if isinstance(a, Sum): - this.extend(a) - this.scalar += a.scalar - this.dollars += a.dollars - elif isinstance(a, Integer_Literal) or isinstance(a, IntType): - this.scalar += long(a) - else: this.append(a) - def __add__ (this, that): - return Sum(this, that) - def __repr__ (this): - res = `list(this)` - if this.scalar: res += ', scalar = ' + `this.scalar` - if this.dollars: res += ', dollars = ' + `this.dollars` - return 'Sum(' + res + ')' - def __str__ (this): - res = ' + '.join(map(str, this)) - if this.dollars: - if res <> '': res += ' ' - res += '- ' - if this.dollars <> 1: res += `this.dollars` - res += '$' - if this.scalar: - if this.scalar > 0: - if res <> '': res += ' + ' - res += `this.scalar` - else: - if res <> '': res += ' ' - res += '- ' + `-this.scalar` - return res - -def plus (a, b): - if not isinstance(a, Constant_Sum): a = Constant_Sum(a) - if not isinstance(b, Constant_Sum): b = Constant_Sum(b) - Regstack.append(a + b) -def minus (a, b): - if not isinstance(a, Constant_Sum): a = Constant_Sum(a) - if not isinstance(b, Constant_Sum): b = Constant_Sum(b) - Regstack.append(a - b) - def matchers (object): if isinstance(object, Class_Marker): yield object.id @@ -251,9 +139,6 @@ yield 'sym' yield 'lit' yield 'const' - elif isinstance(object, Constant_Sum): - yield 'sum' - yield 'const' elif object == Dollar: yield '$' yield 'const' @@ -301,17 +186,12 @@ m = Meaning[tok] mtype = m[0] if mtype == 'builtin': - argc = m[2] & MA_ARGC - if len(Regstack) < argc: raise 'Stack too empty', tok - if argc: - arg = Regstack[-argc:]; Regstack = Regstack[:-argc] - else: - arg = [] if m[2] & MA_PREFIX: tok = prep.get_token() if not isinstance(tok, str): raise 'word expected', tok - arg.append(tok) - apply(m[1], arg) + m[1](tok) + else: + m[1]() elif mtype == 'macro': prep.push_macro(m[1]) elif mtype == 'simple': @@ -343,11 +223,8 @@ Generic_Register = Register() reggen = Generic_Register.child_generator() -MA_ARGC = 007 -MA_PREFIX = 010 +MA_PREFIX = 1 Meaning = { - '+ const const': ('builtin', plus, 2), - '- const const': ('builtin', minus, 2), 'any': ('simple', Class_Marker('any')), 'const': ('simple', Class_Marker('const')), 'include': ('include',), @@ -362,6 +239,10 @@ $-t, sym cursect.emit_tetra_sum([str(Regstack.pop())], relative = 1) + ++ int int + n = Regstack.pop(); m = Regstack.pop() + Regstack.append(m + n) .bss cursect = Bss |