118 'compl': { 'symbol': '~', 'operands': 1, 'priority': 5, 'function': intf (operator.inv) }, |
118 'compl': { 'symbol': '~', 'operands': 1, 'priority': 5, 'function': intf (operator.inv) }, |
119 'neg': { 'symbol': '-', 'operands': 1, 'priority': 5, 'function': lambda x: -x }, |
119 'neg': { 'symbol': '-', 'operands': 1, 'priority': 5, 'function': lambda x: -x }, |
120 'pow': { 'symbol': '**', 'operands': 2, 'priority': 10, 'function': lambda x, y: x ** y }, |
120 'pow': { 'symbol': '**', 'operands': 2, 'priority': 10, 'function': lambda x, y: x ** y }, |
121 'mul': { 'symbol': '*', 'operands': 2, 'priority': 50, 'function': lambda x, y: x * y }, |
121 'mul': { 'symbol': '*', 'operands': 2, 'priority': 50, 'function': lambda x, y: x * y }, |
122 'div': { 'symbol': '/', 'operands': 2, 'priority': 50, 'function': lambda x, y: x / y }, |
122 'div': { 'symbol': '/', 'operands': 2, 'priority': 50, 'function': lambda x, y: x / y }, |
123 'mod': { 'symbol': '%', 'operands': 2, 'priority': 50, 'function': lambda x, y: math.fmod (x, y) }, |
123 'mod': { 'symbol': '%', 'operands': 2, 'priority': 50, 'function': realf (math.fmod) }, |
124 'add': { 'symbol': '+', 'operands': 2, 'priority': 100, 'function': lambda x, y: x + y }, |
124 'add': { 'symbol': '+', 'operands': 2, 'priority': 100, 'function': lambda x, y: x + y }, |
125 'sub': { 'symbol': '-', 'operands': 2, 'priority': 100, 'function': lambda x, y: x - y }, |
125 'sub': { 'symbol': '-', 'operands': 2, 'priority': 100, 'function': lambda x, y: x - y }, |
126 'eq': { 'symbol': '==', 'operands': 2, 'priority': 500, 'function': lambda x, y: x == y }, |
126 'eq': { 'symbol': '==', 'operands': 2, 'priority': 500, 'function': lambda x, y: x == y }, |
127 'neq': { 'symbol': '!=', 'operands': 2, 'priority': 500, 'function': lambda x, y: x != y }, |
127 'neq': { 'symbol': '!=', 'operands': 2, 'priority': 500, 'function': lambda x, y: x != y }, |
128 'lt': { 'symbol': '<', 'operands': 2, 'priority': 500, 'function': lambda x, y: x < y }, |
128 'lt': { 'symbol': '<', 'operands': 2, 'priority': 500, 'function': lambda x, y: x < y }, |
193 're': { 'function': lambda x: x.real }, |
201 're': { 'function': lambda x: x.real }, |
194 'im': { 'function': lambda x: x.imag }, |
202 'im': { 'function': lambda x: x.imag }, |
195 'conjugate':{ 'function': lambda x: x.conjugate() }, |
203 'conjugate':{ 'function': lambda x: x.conjugate() }, |
196 'rand': { 'function': random.random }, |
204 'rand': { 'function': random.random }, |
197 'sgn': { 'function': realf (sgn) }, |
205 'sgn': { 'function': realf (sgn) }, |
|
206 'byte': { 'function': intf (lambda x: integerclamp (x, bits=8, signed=False)) }, |
|
207 'sbyte': { 'function': intf (lambda x: integerclamp (x, bits=8, signed=True)) }, |
|
208 'word': { 'function': intf (lambda x: integerclamp (x, bits=16, signed=False)) }, |
|
209 'sword': { 'function': intf (lambda x: integerclamp (x, bits=16, signed=True)) }, |
|
210 'dword': { 'function': intf (lambda x: integerclamp (x, bits=32, signed=False)) }, |
|
211 'sdword': { 'function': intf (lambda x: integerclamp (x, bits=32, signed=True)) }, |
|
212 'qword': { 'function': intf (lambda x: integerclamp (x, bits=64, signed=False)) }, |
|
213 'sqword': { 'function': intf (lambda x: integerclamp (x, bits=64, signed=True)) }, |
198 } |
214 } |
199 |
215 |
200 Tokens = ['(', ')'] |
216 Tokens = ['(', ')'] |
201 |
217 |
202 # Symbol table |
218 # Symbol table |
557 return expr[0] |
576 return expr[0] |
558 |
577 |
559 def repr_number (self, x): |
578 def repr_number (self, x): |
560 """Returns a string representation for a real number""" |
579 """Returns a string representation for a real number""" |
561 base = self.preferred_base if self.preferred_base else 10 |
580 base = self.preferred_base if self.preferred_base else 10 |
562 if math.fabs (x - math.floor(x)) < epsilon and base != 10: |
581 if is_int (x) and base != 10: |
563 digits='0123456789abcdef' |
582 digits='0123456789abcdef' |
564 assert base <= len (digits), '''base %d is too large''' % base |
583 assert base <= len (digits), '''base %d is too large''' % base |
565 |
584 |
566 divisor = base |
585 divisor = base |
567 rep = '' |
586 rep = '' |
568 x = int (x) |
587 x = int (x) |
569 runaway = 0 |
588 runaway = 0 |
|
589 |
|
590 if not x: |
|
591 return '0x0' |
|
592 |
|
593 if x < 0: |
|
594 return '-' + self.repr_number (abs (x)) |
570 |
595 |
571 while x > 0: |
596 while x > 0: |
572 runaway += 1 |
597 runaway += 1 |
573 if runaway > 1000: |
598 if runaway > 1000: |
574 raise ValueError('runaway triggered') |
599 raise ValueError('runaway triggered') |