[Wisp-cvs] wisp/users/dig tran.py,1.28,1.29
Status: Alpha
Brought to you by:
digg
From: <di...@us...> - 2003-04-17 12:17:18
|
Update of /cvsroot/wisp/wisp/users/dig In directory sc8-pr-cvs1:/tmp/cvs-serv10060 Modified Files: tran.py Log Message: better literal arithmetics Index: tran.py =================================================================== RCS file: /cvsroot/wisp/wisp/users/dig/tran.py,v retrieving revision 1.28 retrieving revision 1.29 diff -u -d -r1.28 -r1.29 --- tran.py 17 Apr 2003 11:50:34 -0000 1.28 +++ tran.py 17 Apr 2003 12:17:14 -0000 1.29 @@ -35,6 +35,14 @@ 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 class Unique: def __init__ (this, name): @@ -51,6 +59,10 @@ class Constant_Sum (Stackable): def __init__ (this, plus = (), minus = (), scalar = 0L): + if isinstance(plus, Symbol_Literal) or plus == Dollar or \ + isinstance(plus, Integer_Literal): plus = plus, + if isinstance(minus, Symbol_Literal) or minus == Dollar or \ + isinstance(minus, Integer_Literal): minus = minus, this.scalar = long(scalar) this.plus = []; this.minus = [] for item in plus: @@ -71,13 +83,13 @@ that = Constant_Sum(plus = [that]) return Constant_Sum(plus = this.plus + that.plus, minus = this.minus + that.minus, - scalar = this.scalar + that.scalar) + 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) + scalar = this.scalar - that.scalar).simplify() def __repr__ (this): arg = [] if this.plus: arg.append('plus = ' + `this.plus`) @@ -93,6 +105,12 @@ 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): |