calc.py

Tue, 11 Aug 2015 19:12:30 +0300

author
Teemu Piippo <tsapii@utu.fi>
date
Tue, 11 Aug 2015 19:12:30 +0300
changeset 151
e24793fae424
parent 146
c17b82b1f573
child 152
1b734faab67a
permissions
-rw-r--r--

Yet more fixes

124
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
1 '''
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
2 Copyright 2015 Teemu Piippo
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
3 All rights reserved.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
4
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
5 Redistribution and use in source and binary forms, with or without
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
6 modification, are permitted provided that the following conditions
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
7 are met:
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
8
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
9 1. Redistributions of source code must retain the above copyright
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
10 notice, this list of conditions and the following disclaimer.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
12 notice, this list of conditions and the following disclaimer in the
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
13 documentation and/or other materials provided with the distribution.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
14 3. The name of the author may not be used to endorse or promote products
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
15 derived from this software without specific prior written permission.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
16
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
27 '''
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
28
146
c17b82b1f573 Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents: 137
diff changeset
29 from __future__ import print_function
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 import re
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 import cmath
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 import math
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 import random
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 import time
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 import operator
125
c44b1aa85257 - added hex support to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
36 import string
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 from copy import deepcopy
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 epsilon = 1e-10
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 # http://stackoverflow.com/a/2182437
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 class Enum (set):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 def __init__ (self, *args):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 super (Enum, self).__init__ (args)
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 def __getattr__ (self, name):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 if name in self:
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 return name
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 raise AttributeError
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 def __setattr__ (self, name, value):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 raise AttributeError
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 class Operator (object):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 def __init__ (self, name, symbol, operands, priority, function):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 self.name = name
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 self.symbol = symbol
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 self.operands = operands
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 self.priority = priority
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 self.function = function
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 def __str__ (self):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 return '''<operator %s>''' % self.name
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 def __repr__ (self):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 return self.__str__()
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 class FunctionCall (object):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 def __init__ (self, funcname, args):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 assert (type(args) is list)
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 self.funcname = funcname
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 self.args = args
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 def __str__ (self):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 return '''<function %s (%s)>''' % (self.funcname, self.args)
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 def __repr__ (self):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 return self.__str__()
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79
136
3812bfce21e0 Use a value class
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
80 class Value (object):
137
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
81 def __init__ (self, value):
136
3812bfce21e0 Use a value class
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
82 self.value = value
3812bfce21e0 Use a value class
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
83
3812bfce21e0 Use a value class
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
84 def __str__ (self):
3812bfce21e0 Use a value class
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
85 return '''<value %s>''' % (self.value)
3812bfce21e0 Use a value class
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
86
3812bfce21e0 Use a value class
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
87 def __repr__ (self):
3812bfce21e0 Use a value class
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
88 return self.__str__()
3812bfce21e0 Use a value class
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
89
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 def do_realf (func, *args):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 for x in args:
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 if x.imag:
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 raise ValueError ('%s called with a complex number' % func.__name__)
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 return func (*[x.real for x in args])
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 def do_intf (func, *args):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 for x in args:
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 if x.imag:
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 raise ValueError ('%s called with a complex number' % func.__name__)
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 if x.real - math.floor (x.real):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 raise ValueError ('%s called with a floating point number' % func.__name__)
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 return func (*[int (x.real) for x in args])
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 def realf (func):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 return lambda *args: do_realf (func, *args)
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 def intf (func):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 return lambda *args: do_intf (func, *args)
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112
134
7316dc5f61ef Added scientific notation and dice expressions (cheaply as operators)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
113 def dice (numRolls, maxValue):
7316dc5f61ef Added scientific notation and dice expressions (cheaply as operators)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
114 sumValue = 0
7316dc5f61ef Added scientific notation and dice expressions (cheaply as operators)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
115
7316dc5f61ef Added scientific notation and dice expressions (cheaply as operators)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
116 for i in range (0, numRolls):
7316dc5f61ef Added scientific notation and dice expressions (cheaply as operators)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
117 sumValue += int (random.random() * maxValue) + 1
7316dc5f61ef Added scientific notation and dice expressions (cheaply as operators)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
118
7316dc5f61ef Added scientific notation and dice expressions (cheaply as operators)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
119 return sumValue
7316dc5f61ef Added scientific notation and dice expressions (cheaply as operators)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
120
7316dc5f61ef Added scientific notation and dice expressions (cheaply as operators)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
121 def scientific (mantissa, exp):
7316dc5f61ef Added scientific notation and dice expressions (cheaply as operators)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
122 return mantissa * 10 ** exp
7316dc5f61ef Added scientific notation and dice expressions (cheaply as operators)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
123
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 Operators = []
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 OperatorData = {
134
7316dc5f61ef Added scientific notation and dice expressions (cheaply as operators)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
126 'sci': { 'symbol': 'e', 'operands': 2, 'priority': 1, 'function': intf (scientific) },
7316dc5f61ef Added scientific notation and dice expressions (cheaply as operators)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
127 'dice': { 'symbol': 'd', 'operands': 2, 'priority': 2, 'function': intf (dice) },
7316dc5f61ef Added scientific notation and dice expressions (cheaply as operators)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
128 'not': { 'symbol': '!', 'operands': 1, 'priority': 5, 'function': lambda x: not x },
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 'compl': { 'symbol': '~', 'operands': 1, 'priority': 5, 'function': intf (operator.inv) },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 'neg': { 'symbol': '-', 'operands': 1, 'priority': 5, 'function': lambda x: -x },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 'pow': { 'symbol': '**', 'operands': 2, 'priority': 10, 'function': lambda x, y: x ** y },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 'mul': { 'symbol': '*', 'operands': 2, 'priority': 50, 'function': lambda x, y: x * y },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 'div': { 'symbol': '/', 'operands': 2, 'priority': 50, 'function': lambda x, y: x / y },
135
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
134 'mod': { 'symbol': '%', 'operands': 2, 'priority': 50, 'function': realf (math.fmod) },
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135 'add': { 'symbol': '+', 'operands': 2, 'priority': 100, 'function': lambda x, y: x + y },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136 'sub': { 'symbol': '-', 'operands': 2, 'priority': 100, 'function': lambda x, y: x - y },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137 'eq': { 'symbol': '==', 'operands': 2, 'priority': 500, 'function': lambda x, y: x == y },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 'neq': { 'symbol': '!=', 'operands': 2, 'priority': 500, 'function': lambda x, y: x != y },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 'lt': { 'symbol': '<', 'operands': 2, 'priority': 500, 'function': lambda x, y: x < y },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140 'lteq': { 'symbol': '<=', 'operands': 2, 'priority': 500, 'function': lambda x, y: x <= y },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 'gt': { 'symbol': '>', 'operands': 2, 'priority': 500, 'function': lambda x, y: x > y },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 'gteq': { 'symbol': '>=', 'operands': 2, 'priority': 500, 'function': lambda x, y: x >= y },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 'btand': { 'symbol': '&', 'operands': 2, 'priority': 600, 'function': intf (operator.and_) },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144 'btxor': { 'symbol': '^', 'operands': 2, 'priority': 601, 'function': intf (operator.xor) },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 'btor': { 'symbol': '|', 'operands': 2, 'priority': 602, 'function': intf (operator.or_) },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 'and': { 'symbol': '&&', 'operands': 2, 'priority': 603, 'function': lambda x, y: x and y },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147 'or': { 'symbol': '||', 'operands': 2, 'priority': 604, 'function': lambda x, y: x or y },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 }
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149
146
c17b82b1f573 Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents: 137
diff changeset
150 for name, data in OperatorData.items():
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151 Operators.append (Operator (name=name, symbol=data['symbol'], operands=data['operands'],
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152 priority=data['priority'], function=data['function']))
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
153
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154 OperatorSymbols={}
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155 for op in Operators:
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156 if op.symbol in OperatorSymbols:
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 OperatorSymbols[op.symbol].append (op)
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158 else:
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159 OperatorSymbols[op.symbol] = [op]
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160
131
7349e9c6176b Added sgn(x) to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 130
diff changeset
161 def sgn (x):
7349e9c6176b Added sgn(x) to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 130
diff changeset
162 return cmp (x, 0)
7349e9c6176b Added sgn(x) to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 130
diff changeset
163
135
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
164 def integerclamp (x, bits, signed):
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
165 x = x % (2 ** bits)
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
166
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
167 if signed and x >= (2 ** (bits - 1)):
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
168 x = -(2 ** bits) + x
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
169
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
170 return x
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
171
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172 Constants = {
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
173 'pi': cmath.pi,
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174 'e': cmath.e,
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175 'phi': 1.6180339887498948482,
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176 'epsilon': epsilon,
132
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 131
diff changeset
177 'potato': cmath.sqrt(8) / 3,
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178 }
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
180 random.seed (time.time())
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182 Functions = {
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183 'round': { 'function': realf (round) },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 'floor': { 'function': realf (math.floor) },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185 'ceil': { 'function': realf (math.ceil) },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186 'fact': { 'function': intf (math.factorial) },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 'abs': { 'function': realf (math.fabs) },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188 'degrees': { 'function': realf (math.degrees) },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189 'radians': { 'function': realf (math.radians) },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
190 'erf': { 'function': realf (math.erf) },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191 'erfc': { 'function': realf (math.erfc) },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192 'gamma': { 'function': realf (math.gamma) },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 'lgamma': { 'function': realf (math.lgamma) },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194 'sqrt': { 'function': cmath.sqrt },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
195 'ln': { 'function': cmath.log },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196 'log': { 'function': cmath.log10 },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197 'sin': { 'function': cmath.sin },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
198 'sinh': { 'function': cmath.sinh },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199 'asin': { 'function': cmath.asin },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
200 'asinh': { 'function': cmath.asinh },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201 'cos': { 'function': cmath.cos },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202 'cosh': { 'function': cmath.cosh },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203 'acos': { 'function': cmath.acos },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204 'acosh': { 'function': cmath.acosh },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
205 'tan': { 'function': cmath.tan },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
206 'tanh': { 'function': cmath.tanh },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
207 'atan': { 'function': cmath.atan },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
208 'atanh': { 'function': cmath.atanh },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
209 'exp': { 'function': cmath.exp },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
210 'phase': { 'function': cmath.phase },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
211 'lg': { 'function': lambda x: cmath.log (x, 2) },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
212 're': { 'function': lambda x: x.real },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
213 'im': { 'function': lambda x: x.imag },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
214 'conjugate':{ 'function': lambda x: x.conjugate() },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215 'rand': { 'function': random.random },
131
7349e9c6176b Added sgn(x) to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 130
diff changeset
216 'sgn': { 'function': realf (sgn) },
135
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
217 'byte': { 'function': intf (lambda x: integerclamp (x, bits=8, signed=False)) },
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
218 'sbyte': { 'function': intf (lambda x: integerclamp (x, bits=8, signed=True)) },
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
219 'word': { 'function': intf (lambda x: integerclamp (x, bits=16, signed=False)) },
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
220 'sword': { 'function': intf (lambda x: integerclamp (x, bits=16, signed=True)) },
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
221 'dword': { 'function': intf (lambda x: integerclamp (x, bits=32, signed=False)) },
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
222 'sdword': { 'function': intf (lambda x: integerclamp (x, bits=32, signed=True)) },
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
223 'qword': { 'function': intf (lambda x: integerclamp (x, bits=64, signed=False)) },
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
224 'sqword': { 'function': intf (lambda x: integerclamp (x, bits=64, signed=True)) },
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
225 }
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
226
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
227 Tokens = ['(', ')']
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
228
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
229 # Symbol table
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
230 SymbolType = Enum ('CONSTANT', 'FUNCTION', 'OPERATOR', 'TOKEN')
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
231 SymbolTypes = {}
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
232 Symbols = []
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
233
146
c17b82b1f573 Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents: 137
diff changeset
234 for name, value in Constants.items():
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
235 Symbols.append (name)
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
236 SymbolTypes[name] = SymbolType.CONSTANT
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
237
146
c17b82b1f573 Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents: 137
diff changeset
238 for name, data in Functions.items():
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
239 Symbols.append (name)
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
240 SymbolTypes[name] = SymbolType.FUNCTION
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
242 for op in Operators:
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
243 if op.symbol not in Symbols:
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
244 Symbols.append (op.symbol)
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
245 SymbolTypes[op.symbol] = SymbolType.OPERATOR
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
246
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
247 for name in Tokens:
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
248 SymbolTypes[name] = SymbolType.TOKEN
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
249
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
250 Symbols += Tokens
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
251 Symbols = sorted (Symbols, key=lambda x: len (x), reverse=True)
126
0fc519afba89 - added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
252 PreferredBase = 10
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
253
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
254 def set_preferred_base(x):
126
0fc519afba89 - added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
255 global PreferredBase
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
256 PreferredBase = x
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
257
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
258 def rindex (li, value):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
259 return (len(li) - 1) - li[::-1].index(value)
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
260
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
261 def realPrint (x):
146
c17b82b1f573 Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents: 137
diff changeset
262 print (x)
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
263
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
264 Attributes = {
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
265 'hex': lambda self: self.set_preferred_base (0x10),
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
266 'binary': lambda self: self.set_preferred_base (0b10),
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
267 'decimal': lambda self: self.set_preferred_base (10),
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
268 }
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
269
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
270 Attributes['bin'] = Attributes['binary']
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
271 Attributes['dec'] = Attributes['decimal']
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
272 AttributeNames = sorted ([key for key in Attributes], key=lambda x:len(x), reverse=True)
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
273
135
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
274 def is_int (x):
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
275 return math.fabs (x - math.floor(x)) < epsilon
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
276
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
277 class Calculator (object):
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
278 def __init__ (self):
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
279 self.preferred_base = None
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
280
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
281 def set_preferred_base (self, x):
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
282 self.preferred_base = x
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
283
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
284 def trim_spaces (self, expr):
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
285 return re.sub ('\s+', '', expr.strip())
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
286
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
287 def parse_attributes (self, expr):
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
288 if expr[0] != '<':
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
289 return expr
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
290
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
291 i = 1
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
292 while expr[i] != '>':
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
293 if expr[i] == '|':
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
294 i += 1
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
295
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
296 for name in AttributeNames:
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
297 if expr[i:i + len(name)] == name:
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
298 Attributes[name] (self)
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
299 i += len(name)
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
300
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
301 if expr[i] == '>':
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
302 break
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
303
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
304 if expr[i] != '|':
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
305 raise ValueError ('malformed attributes')
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
306 break
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
307 else:
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
308 raise ValueError ('bad attributes: %s' % expr[i:])
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
309
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
310 return expr[i + 1:]
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
311
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
312 def parse_number (self, expr):
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
313 """Tries to parse a number from the expression. Returns (value, length) on success."""
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
314 i = 0
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
315 value = None
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
316 base = 10
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
317
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
318 # Possibly it's hexadecimal
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
319 if expr[0:2].lower() == '0x':
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
320 base = 0x10
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
321 digits = string.hexdigits
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
322 digitname = 'hexit'
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
323 i = 2
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
324 elif expr[0:2].lower() == '0b':
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
325 base = 0b10
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
326 digits = ['0', '1']
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
327 digitname = 'bit'
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
328 i = 2
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
329
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
330 if base != 10:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
331 if not self.preferred_base:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
332 self.preferred_base = base
134
7316dc5f61ef Added scientific notation and dice expressions (cheaply as operators)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 132
diff changeset
333
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
334 # Skip leading zeroes
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
335 while i < len (expr) and expr[i] == '0':
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
336 i += 1
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
337
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
338 startingIndex = i
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
339 while i < len (expr) and expr[i] in digits:
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
340 i += 1
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
341
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
342 if i < len(expr) and expr[i] == '.':
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
343 raise ValueError ('non-decimal floating point numbers are not supported')
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
344
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
345 if i == startingIndex:
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
346 if i < len (expr):
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
347 raise ValueError ('not a valid %s "%s" in %s' % (digitname, expr[i], expr[0:i+1]))
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
348 else:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
349 raise ValueError ('end of expression encountered while parsing '
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
350 'base-%d literal' % base)
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
351
136
3812bfce21e0 Use a value class
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
352 return (Value (int (expr[startingIndex:i], base)), i)
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
353
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
354 if expr[0] == 'i':
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
355 # Special case, we just have 'i' -- need special handling here because otherwise this would
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
356 # call float('') in the complex number routine, which throws an error.
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
357 # TODO: maybe 'i' can be a symbol instead?
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
358 return (1j, 1)
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
359
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
360 # Try parse increasingly long substrings until we are unable to create a float or complex number
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
361 # from it.
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
362 try:
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
363 while i < len (expr):
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
364 if expr[i] == 'i':
136
3812bfce21e0 Use a value class
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
365 value = Value (float (expr[:i]) * 1j)
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
366 else:
136
3812bfce21e0 Use a value class
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
367 value = Value (float (expr [:i + 1]))
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
368 i += 1
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
369
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
370 return (value, i)
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
371 except ValueError:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
372 if i != 0:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
373 # Got a number (the error happened when we tried to parse past the number)
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
374 return (value, i)
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
375 else:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
376 # The error happened on the first character. So this is not a number.
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
377 return None
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
378
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
379 def parse_symbol (self, expr):
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
380 for sym in Symbols:
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
381 if expr[:len (sym)] == sym:
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
382 return sym
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
383
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
384 return None
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
385
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
386 def tokenize (self, expr):
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
387 i=0
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
388 tokens = []
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
389
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
390 while i < len(expr):
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
391 sym = self.parse_symbol (expr[i:])
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
392
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
393 if sym:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
394 symtype = SymbolTypes[sym]
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
395 if symtype == SymbolType.CONSTANT:
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
396 tokens.append (Constants[sym])
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
397 else:
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
398 tokens.append (sym)
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
399
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
400 i += len(sym)
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
401 continue
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
402
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
403 result = self.parse_number (expr[i:])
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
404 if result:
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
405 num, length = result
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
406 tokens.append (num)
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
407 i += length
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
408 continue
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
409
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
410 raise ValueError ("""bad expression, couldn't parse: %s""" % expr[i:])
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
411
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
412 return tokens
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
413
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
414 def process_parens (self, expr):
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
415 """Processes parentheses of expr into sublists in-place.
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
416 [1.0, '*', '(', 3.5, '+', 1j, ')']
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
417 -> [1.0, '*', [3.5, '+', 1j]]"""
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
418 if '(' not in expr and ')' not in expr:
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
419 return
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
420
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
421 try:
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
422 parenStart = rindex (expr, '(')
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
423 parenEnd = expr.index (')', parenStart)
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
424 except ValueError:
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
425 raise ValueError ("""mismatched parentheses in expression: %s""" % expr)
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
426
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
427 subexpr = expr[parenStart + 1:parenEnd]
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
428 del expr[parenStart + 1:parenEnd + 1]
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
429 expr[parenStart] = subexpr
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
430 self.process_parens (expr)
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
431
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
432 def process_functions (self, expr):
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
433 """Processes functions in-place"""
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
434 i = 0
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
435 while i < len (expr):
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
436 if type (expr[i]) is list:
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
437 self.process_functions (expr[i])
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
438
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
439 if (type(expr[i]) is not str) or (expr[i] not in Functions):
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
440 i += 1
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
441 continue
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
442
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
443 # Ensure that arguments follow
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
444 if (i + 1 >= len (expr)) or (type (expr[i + 1]) is not list):
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
445 raise ValueError ("""function %s used without arguments""" % expr[i])
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
446
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
447 args = expr[i + 1]
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
448 del expr[i + 1]
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
449 expr[i] = FunctionCall (expr[i], args)
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
450 i += 1
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
451
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
452 def is_operand (self, x):
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
453 # Operands can be either lists (which also mean parens, thus can be single-expressions)
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
454 # or complex numbers
132
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 131
diff changeset
455 return type(x) in [complex, list, FunctionCall]
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
456
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
457 def find_fitting_operator (self, sym, numOperands):
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
458 # Pass 1: exact numOperands match
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
459 for op in Operators:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
460 if op.symbol != sym:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
461 continue
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
462
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
463 if op.operands == numOperands:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
464 return op
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
465
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
466 # Pass 2: by symbol
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
467 for op in Operators:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
468 if op.symbol == sym:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
469 return op
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
470
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
471 raise ValueError ('''unknown operator %s!''' % sym)
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
472
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
473 def process_operators (self, expr):
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
474 """Processes operators"""
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
475 i = 0
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
476
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
477 # Find all operators in this expression
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
478 while i < len (expr):
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
479 if type (expr[i]) is list:
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
480 self.process_functions (expr[i])
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
481 self.process_operators (expr[i])
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
482
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
483 if type (expr[i]) is FunctionCall:
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
484 self.process_functions (expr[i].args)
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
485 self.process_operators (expr[i].args)
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
486
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
487 if (type(expr[i]) is not str) or (expr[i] not in OperatorSymbols):
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
488 i += 1
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
489 continue
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
490
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
491 args = []
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
492 argIndices = []
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
493 if i > 0 and self.is_operand (expr[i - 1]):
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
494 args.append (expr[i - 1])
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
495 argIndices.append (i - 1)
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
496
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
497 if i - 1 < len(expr) and self.is_operand (expr[i + 1]):
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
498 args.append (expr[i + 1])
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
499 argIndices.append (i + 1)
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
500
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
501 # Resolve operators with the same symbol based on operand count
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
502 numOperands = 0
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
503 for arg in args:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
504 if self.is_operand (arg):
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
505 numOperands += 1
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
506
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
507 expr[i] = self.find_fitting_operator (expr[i], numOperands)
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
508 i += 1
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
509
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
510 def find_priority_operator (self, expr):
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
511 """Finds the operator with most priority in the expression"""
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
512 bestOp = None
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
513 bestOpIndex = -1
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
514
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
515 for i in range (0, len (expr)):
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
516 sym = expr[i]
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
517
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
518 if type (sym) is not Operator:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
519 continue
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
520
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
521 if not bestOp or sym.priority < bestOp.priority:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
522 bestOp = sym
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
523 bestOpIndex = i
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
524
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
525 return (bestOp, bestOpIndex)
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
526
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
527 def evaluate (self, expr, verbose=False):
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
528 printFunc = realPrint if verbose else lambda x:None
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
529 printFunc (self.tabs + 'Preprocess: %s' % expr)
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
530
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
531 # If there are sub-expressions in here, those need to be evaluated first
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
532 i = 0
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
533 while i < len (expr):
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
534 sym = expr[i]
126
0fc519afba89 - added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
535
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
536 if type (sym) is list and sym:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
537 printFunc (self.tabs + 'Evaluating sub-expression: %s' % (sym))
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
538 self.tabs += '\t'
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
539 expr[i] = self.evaluate (list (sym), verbose)
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
540 self.tabs = self.tabs[:-1]
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
541 printFunc (self.tabs + '-> %s' % expr[i])
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
542
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
543 # If there are function calls, evaluate those
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
544 if type (sym) is FunctionCall:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
545 self.tabs += '\t'
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
546 if sym.args:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
547 sym.args = [self.evaluate (sym.args, verbose)]
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
548 self.tabs = self.tabs[:-1]
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
549
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
550 printFunc (self.tabs + 'Evaluating function call: %s' % (sym))
136
3812bfce21e0 Use a value class
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
551 expr[i] = Value (Functions[sym.funcname]['function'] (*[x.value for x in sym.args]))
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
552 printFunc (self.tabs + '-> %s' % expr[i])
135
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
553
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
554 i += 1
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
555
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
556 printFunc (self.tabs + 'Evaluate: %s' % expr)
126
0fc519afba89 - added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
557 runaway = 0
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
558 while True:
126
0fc519afba89 - added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
559 runaway += 1
127
66b206bd9510 Cranked up the runaway (8 was only for testing)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 126
diff changeset
560 if runaway > 1000:
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
561 raise ValueError ('infinite loop detected')
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
562
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
563 op, i = self.find_priority_operator (expr)
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
564 if not op:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
565 break
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
566
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
567 if op.operands == 2:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
568 argIndices = [i - 1, i + 1]
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
569 else:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
570 argIndices = [i + 1]
126
0fc519afba89 - added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
571
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
572 args = [expr[x] for x in argIndices]
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
573 argIndices = sorted (argIndices, reverse=True)
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
574 printFunc (self.tabs + 'Processing: (%s, %d) with args %s' % (op, i, args))
136
3812bfce21e0 Use a value class
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
575 expr[i] = Value (op.function (*[x.value for x in args]))
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
576 printFunc (self.tabs + '-> %s' % expr[i])
126
0fc519afba89 - added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
577
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
578 for i2 in argIndices:
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
579 del expr[i2]
126
0fc519afba89 - added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
580
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
581 printFunc (self.tabs + 'Result: %s' % expr[0])
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
582
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
583 if len(expr) != 1:
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
584 printFunc (self.tabs + 'Bad expression detected, tokens: %s' % expr)
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
585 raise ValueError ('malformed expression')
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
586
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
587 return expr[0]
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
588
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
589 def repr_number (self, x):
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
590 """Returns a string representation for a real number"""
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
591 base = self.preferred_base if self.preferred_base else 10
135
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
592 if is_int (x) and base != 10:
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
593 digits='0123456789abcdef'
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
594 assert base <= len (digits), '''base %d is too large''' % base
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
595
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
596 divisor = base
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
597 rep = ''
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
598 x = int (x)
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
599 runaway = 0
125
c44b1aa85257 - added hex support to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
600
135
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
601 if not x:
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
602 return '0x0'
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
603
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
604 if x < 0:
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
605 return '-' + self.repr_number (abs (x))
ad27ab7e6fb6 Added 8 integer clamping functions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 134
diff changeset
606
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
607 while x > 0:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
608 runaway += 1
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
609 if runaway > 1000:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
610 raise ValueError('runaway triggered')
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
611
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
612 i = (x % divisor) / (divisor / base)
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
613 x -= i * (divisor / base)
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
614 rep += digits[i]
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
615 divisor *= base
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
616
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
617 rep += 'x' if base == 16 else 'b'
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
618 rep += '0'
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
619 return rep[::-1]
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
620
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
621 rep = '%.10f' % x
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
622
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
623 if '.' in rep:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
624 while rep[-1] == '0':
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
625 rep = rep[:-1]
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
626
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
627 if rep[-1] == '.':
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
628 rep = rep[:-1]
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
629
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
630 return rep
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
631
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
632 def repr_imaginary (self, x):
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
633 rep = self.repr_number (x)
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
634
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
635 if rep == '1':
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
636 return 'i'
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
637
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
638 if rep == '-1':
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
639 return '-i'
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
640
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
641 return rep + 'i'
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
642
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
643 def represent (self, x):
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
644 """Returns a string representation of a float or complex number"""
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
645 if math.fabs (x.imag) > epsilon:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
646 if math.fabs (x.real) > epsilon:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
647 # Complex number
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
648 return '%s %s %s' % (self.repr_number (x.real),
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
649 '+' if x.imag >= 0 else '-',
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
650 self.repr_imaginary (math.fabs (x.imag)))
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
651 else:
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
652 # Pure imaginary number
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
653 return self.repr_imaginary (x.imag)
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
654 else:
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
655 # Real number
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
656 return self.repr_number (x.real)
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
657
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
658 def calc (self, expr, verbose=False):
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
659 self.state = {}
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
660 self.tabs = ''
130
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
661 expr = self.trim_spaces (expr)
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
662 expr = self.parse_attributes (expr)
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
663 expr = self.tokenize (expr)
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
664 self.process_parens (expr)
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
665 self.process_functions (expr)
c82cef747008 - reverted previous commit (now that was a bad idea)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 129
diff changeset
666 self.process_operators (expr)
136
3812bfce21e0 Use a value class
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
667 result = self.evaluate (expr, verbose).value
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 127
diff changeset
668 return self.represent (result)

mercurial