Sun, 19 Apr 2015 20:52:49 +0300
Cranked up the runaway (8 was only for testing)
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 | |
123
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
29 | import re |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
30 | import cmath |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
31 | import math |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
32 | import random |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
33 | import time |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | import operator |
125
c44b1aa85257
- added hex support to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents:
124
diff
changeset
|
35 | import string |
123
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
36 | from copy import deepcopy |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
37 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
38 | epsilon = 1e-10 |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
39 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
40 | # http://stackoverflow.com/a/2182437 |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
41 | class Enum (set): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
42 | def __init__ (self, *args): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
43 | super (Enum, self).__init__ (args) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
44 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
45 | def __getattr__ (self, name): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
46 | if name in self: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
47 | return name |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
48 | raise AttributeError |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
49 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
50 | def __setattr__ (self, name, value): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
51 | raise AttributeError |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
52 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
53 | class Operator (object): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
54 | def __init__ (self, name, symbol, operands, priority, function): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
55 | self.name = name |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
56 | self.symbol = symbol |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
57 | self.operands = operands |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
58 | self.priority = priority |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
59 | self.function = function |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
60 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
61 | def __str__ (self): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
62 | return '''<operator %s>''' % self.name |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
63 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
64 | def __repr__ (self): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
65 | return self.__str__() |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
66 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
67 | class FunctionCall (object): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
68 | def __init__ (self, funcname, args): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
69 | assert (type(args) is list) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
70 | self.funcname = funcname |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
71 | self.args = args |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
72 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
73 | def __str__ (self): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
74 | return '''<function %s (%s)>''' % (self.funcname, self.args) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
75 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
76 | def __repr__ (self): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
77 | return self.__str__() |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
78 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
79 | def do_realf (func, *args): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
80 | for x in args: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
81 | if x.imag: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
82 | 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
|
83 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
84 | return func (*[x.real for x in args]) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
85 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
86 | def do_intf (func, *args): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
87 | for x in args: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
88 | if x.imag: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
89 | 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
|
90 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
91 | if x.real - math.floor (x.real): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
92 | 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
|
93 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
94 | 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
|
95 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
96 | def realf (func): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
97 | return lambda *args: do_realf (func, *args) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
98 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
99 | def intf (func): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
100 | return lambda *args: do_intf (func, *args) |
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 | Operators = [] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
103 | OperatorData = { |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
104 | 'lneg': { 'symbol': '!', 'operands': 1, 'priority': 5, 'function': lambda x: not x }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
105 | '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
|
106 | '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
|
107 | '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
|
108 | '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
|
109 | 'div': { '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
|
110 | 'mod': { 'symbol': '%', 'operands': 2, 'priority': 50, 'function': lambda x, y: math.fmod (x, y) }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
111 | '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
|
112 | '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
|
113 | '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
|
114 | '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
|
115 | '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
|
116 | '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
|
117 | '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
|
118 | '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
|
119 | '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
|
120 | '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
|
121 | '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
|
122 | '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
|
123 | '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
|
124 | } |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
125 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
126 | for name, data in OperatorData.iteritems(): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
127 | 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
|
128 | priority=data['priority'], function=data['function'])) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
129 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
130 | OperatorSymbols={} |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
131 | for op in Operators: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
132 | if op.symbol in OperatorSymbols: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
133 | OperatorSymbols[op.symbol].append (op) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
134 | else: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
135 | OperatorSymbols[op.symbol] = [op] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
136 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
137 | Constants = { |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
138 | 'pi': cmath.pi, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
139 | 'e': cmath.e, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
140 | 'phi': 1.6180339887498948482, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
141 | 'epsilon': epsilon, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
142 | } |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
143 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
144 | random.seed (time.time()) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
145 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
146 | Functions = { |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
147 | 'round': { 'function': realf (round) }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
148 | 'floor': { 'function': realf (math.floor) }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
149 | 'ceil': { 'function': realf (math.ceil) }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
150 | 'fact': { 'function': intf (math.factorial) }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
151 | 'abs': { 'function': realf (math.fabs) }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
152 | 'degrees': { 'function': realf (math.degrees) }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
153 | 'radians': { 'function': realf (math.radians) }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
154 | 'erf': { 'function': realf (math.erf) }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
155 | 'erfc': { 'function': realf (math.erfc) }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
156 | 'gamma': { 'function': realf (math.gamma) }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
157 | 'lgamma': { 'function': realf (math.lgamma) }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
158 | 'sqrt': { 'function': cmath.sqrt }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
159 | 'ln': { 'function': cmath.log }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
160 | 'log': { 'function': cmath.log10 }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
161 | 'sin': { 'function': cmath.sin }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
162 | 'sinh': { 'function': cmath.sinh }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
163 | 'asin': { 'function': cmath.asin }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
164 | 'asinh': { 'function': cmath.asinh }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
165 | 'cos': { 'function': cmath.cos }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
166 | 'cosh': { 'function': cmath.cosh }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
167 | 'acos': { 'function': cmath.acos }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
168 | 'acosh': { 'function': cmath.acosh }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
169 | 'tan': { 'function': cmath.tan }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
170 | 'tanh': { 'function': cmath.tanh }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
171 | 'atan': { 'function': cmath.atan }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
172 | 'atanh': { 'function': cmath.atanh }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
173 | 'exp': { 'function': cmath.exp }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
174 | 'phase': { 'function': cmath.phase }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
175 | 'lg': { 'function': lambda x: cmath.log (x, 2) }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
176 | 're': { 'function': lambda x: x.real }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
177 | 'im': { 'function': lambda x: x.imag }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
178 | 'conjugate':{ 'function': lambda x: x.conjugate() }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
179 | 'rand': { 'function': random.random }, |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
180 | } |
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 | Tokens = ['(', ')'] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
183 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
184 | # Symbol table |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
185 | SymbolType = Enum ('CONSTANT', 'FUNCTION', 'OPERATOR', 'TOKEN') |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
186 | SymbolTypes = {} |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
187 | Symbols = [] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
188 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
189 | for name, value in Constants.iteritems(): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
190 | Symbols.append (name) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
191 | SymbolTypes[name] = SymbolType.CONSTANT |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
192 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
193 | for name, data in Functions.iteritems(): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
194 | Symbols.append (name) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
195 | SymbolTypes[name] = SymbolType.FUNCTION |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
196 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
197 | for op in Operators: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
198 | if op.symbol not in Symbols: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
199 | Symbols.append (op.symbol) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
200 | SymbolTypes[op.symbol] = SymbolType.OPERATOR |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
201 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
202 | for name in Tokens: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
203 | SymbolTypes[name] = SymbolType.TOKEN |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
204 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
205 | Symbols += Tokens |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
206 | 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
|
207 | PreferredBase = 10 |
123
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
208 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
209 | def parse_number (expr): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
210 | """Tries to parse a number from the expression. Returns (value, length) on success.""" |
126
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
211 | global PreferredBase |
123
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
212 | i = 0 |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
213 | value = None |
126
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
214 | base = 10 |
123
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
215 | |
125
c44b1aa85257
- added hex support to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents:
124
diff
changeset
|
216 | # Possibly it's hexadecimal |
c44b1aa85257
- added hex support to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents:
124
diff
changeset
|
217 | if expr[0:2].lower() == '0x': |
126
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
218 | base = PreferredBase = 0x10 |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
219 | digits = string.hexdigits |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
220 | digitname = 'hexit' |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
221 | i = 2 |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
222 | elif expr[0:2].lower() == '0b': |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
223 | base = PreferredBase = 0b10 |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
224 | digits = ['0', '1'] |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
225 | digitname = 'bit' |
125
c44b1aa85257
- added hex support to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents:
124
diff
changeset
|
226 | i = 2 |
c44b1aa85257
- added hex support to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents:
124
diff
changeset
|
227 | |
126
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
228 | if base != 10: |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
229 | # Skip leading zeroes |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
230 | while i < len (expr) and expr[i] == '0': |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
231 | i += 1 |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
232 | |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
233 | startingIndex = i |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
234 | while i < len (expr) and expr[i] in digits: |
125
c44b1aa85257
- added hex support to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents:
124
diff
changeset
|
235 | i += 1 |
c44b1aa85257
- added hex support to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents:
124
diff
changeset
|
236 | |
c44b1aa85257
- added hex support to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents:
124
diff
changeset
|
237 | if i < len(expr) and expr[i] == '.': |
126
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
238 | raise ValueError ('non-decimal floating point numbers are not supported') |
125
c44b1aa85257
- added hex support to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents:
124
diff
changeset
|
239 | |
126
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
240 | if i == startingIndex: |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
241 | if i < len (expr): |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
242 | raise ValueError ('not a valid %s "%s" in %s' % (digitname, expr[i], expr[0:i+1])) |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
243 | else: |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
244 | raise ValueError ('end of expression encountered while parsing ' |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
245 | 'base-%d literal' % base) |
125
c44b1aa85257
- added hex support to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents:
124
diff
changeset
|
246 | |
126
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
247 | return (complex (int (expr[startingIndex:i], base), 0), i) |
125
c44b1aa85257
- added hex support to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents:
124
diff
changeset
|
248 | |
123
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
249 | if expr[0] == 'i': |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
250 | # Special case, we just have 'i' -- need special handling here because otherwise this would |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
251 | # call float('') in the complex number routine, which throws an error. |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
252 | # TODO: maybe 'i' can be a symbol instead? |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
253 | return (1j, 1) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
254 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
255 | # Try parse increasingly long substrings until we are unable to create a float or complex number |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
256 | # from it. |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
257 | try: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
258 | while i < len (expr): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
259 | if expr[i] == 'i': |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
260 | value = complex (0.0, float (expr[:i])) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
261 | else: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
262 | value = complex (float (expr [:i + 1]), 0.0) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
263 | i += 1 |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
264 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
265 | return (value, i) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
266 | except ValueError: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
267 | if i != 0: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
268 | # Got a number (the error happened when we tried to parse past the number) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
269 | return (value, i) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
270 | else: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
271 | # The error happened on the first character. So this is not a number. |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
272 | return None |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
273 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
274 | def parse_symbol (expr): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
275 | for sym in Symbols: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
276 | if expr[:len (sym)] == sym: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
277 | return sym |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
278 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
279 | return None |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
280 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
281 | def tokenize (expr): |
126
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
282 | global PreferredBase |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
283 | PreferredBase = 10 |
123
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
284 | expr = re.sub ('\s+', '', expr.strip()) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
285 | i=0 |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
286 | tokens = [] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
287 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
288 | while i < len(expr): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
289 | sym = parse_symbol (expr[i:]) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
290 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
291 | if sym: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
292 | symtype = SymbolTypes[sym] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
293 | if symtype == SymbolType.CONSTANT: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
294 | tokens.append (Constants[sym]) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
295 | else: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
296 | tokens.append (sym) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
297 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
298 | i += len(sym) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
299 | continue |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
300 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
301 | result = parse_number (expr[i:]) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
302 | if result: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
303 | num, length = result |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
304 | tokens.append (num) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
305 | i += length |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
306 | continue |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
307 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
308 | raise ValueError ("""bad expression, couldn't parse: %s""" % expr[i:]) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
309 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
310 | return tokens |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
311 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
312 | def rindex (li, value): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
313 | return (len(li) - 1) - li[::-1].index(value) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
314 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
315 | def process_parens (expr): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
316 | """Processes parentheses of expr into sublists in-place. |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
317 | [1.0, '*', '(', 3.5, '+', 1j, ')'] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
318 | -> [1.0, '*', [3.5, '+', 1j]]""" |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
319 | if '(' not in expr and ')' not in expr: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
320 | return |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
321 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
322 | try: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
323 | parenStart = rindex (expr, '(') |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
324 | parenEnd = expr.index (')', parenStart) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
325 | except ValueError: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
326 | raise ValueError ("""mismatched parentheses in expression: %s""" % expr) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
327 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
328 | subexpr = expr[parenStart + 1:parenEnd] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
329 | del expr[parenStart + 1:parenEnd + 1] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
330 | expr[parenStart] = subexpr |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
331 | process_parens (expr) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
332 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
333 | def process_functions (expr): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
334 | """Processes functions in-place""" |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
335 | i = 0 |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
336 | while i < len (expr): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
337 | if type (expr[i]) is list: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
338 | process_functions (expr[i]) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
339 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
340 | if (type(expr[i]) is not str) or (expr[i] not in Functions): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
341 | i += 1 |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
342 | continue |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
343 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
344 | # Ensure that arguments follow |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
345 | if (i + 1 >= len (expr)) or (type (expr[i + 1]) is not list): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
346 | raise ValueError ("""function %s used without arguments""" % expr[i]) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
347 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
348 | args = expr[i + 1] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
349 | del expr[i + 1] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
350 | expr[i] = FunctionCall (expr[i], args) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
351 | i += 1 |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
352 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
353 | def is_operand (x): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
354 | # Operands can be either lists (which also mean parens, thus can be single-expressions) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
355 | # or complex numbers |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
356 | return type(x) in [complex, list] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
357 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
358 | def find_fitting_operator (sym, numOperands): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
359 | # Pass 1: exact numOperands match |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
360 | for op in Operators: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
361 | if op.symbol != sym: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
362 | continue |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
363 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
364 | if op.operands == numOperands: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
365 | # print '%s, %d -> %s (1st pass)' % (sym, numOperands, op) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
366 | return op |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
367 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
368 | # Pass 2: by symbol |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
369 | for op in Operators: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
370 | if op.symbol == sym: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
371 | # print '%s, %d -> %s (2nd pass)' % (sym, numOperands, op) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
372 | return op |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
373 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
374 | raise ValueError ('''unknown operator %s!''' % sym) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
375 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
376 | def process_operators (expr): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
377 | """Processes operators""" |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
378 | i = 0 |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
379 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
380 | # Find all operators in this expression |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
381 | while i < len (expr): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
382 | if type (expr[i]) is list: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
383 | process_functions (expr[i]) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
384 | process_operators (expr[i]) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
385 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
386 | if type (expr[i]) is FunctionCall: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
387 | process_functions (expr[i].args) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
388 | process_operators (expr[i].args) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
389 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
390 | if (type(expr[i]) is not str) or (expr[i] not in OperatorSymbols): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
391 | i += 1 |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
392 | continue |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
393 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
394 | args = [] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
395 | argIndices = [] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
396 | if i > 0 and is_operand (expr[i - 1]): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
397 | args.append (expr[i - 1]) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
398 | argIndices.append (i - 1) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
399 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
400 | if i - 1 < len(expr) and is_operand (expr[i + 1]): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
401 | args.append (expr[i + 1]) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
402 | argIndices.append (i + 1) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
403 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
404 | # Resolve operators with the same symbol based on operand count |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
405 | numOperands = 0 |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
406 | for arg in args: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
407 | if is_operand (arg): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
408 | numOperands += 1 |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
409 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
410 | expr[i] = find_fitting_operator (expr[i], numOperands) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
411 | i += 1 |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
412 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
413 | def find_priority_operator (expr): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
414 | """Finds the operator with most priority in the expression""" |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
415 | bestOp = None |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
416 | bestOpIndex = -1 |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
417 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
418 | for i in range (0, len (expr)): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
419 | sym = expr[i] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
420 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
421 | if type (sym) is not Operator: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
422 | continue |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
423 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
424 | if not bestOp or sym.priority < bestOp.priority: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
425 | bestOp = sym |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
426 | bestOpIndex = i |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
427 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
428 | return (bestOp, bestOpIndex) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
429 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
430 | def realPrint (x): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
431 | print x |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
432 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
433 | tabs='' |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
434 | def evaluate (expr, verbose=False): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
435 | global tabs |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
436 | printFunc = realPrint if verbose else lambda x:None |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
437 | printFunc (tabs + 'Preprocess: %s' % expr) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
438 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
439 | # If there are sub-expressions in here, those need to be evaluated first |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
440 | i = 0 |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
441 | while i < len (expr): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
442 | sym = expr[i] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
443 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
444 | if type (sym) is list and sym: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
445 | printFunc (tabs + 'Evaluating sub-expression: %s' % (sym)) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
446 | tabs += '\t' |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
447 | expr[i] = evaluate (list (sym), verbose) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
448 | tabs = tabs[:-1] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
449 | printFunc (tabs + '-> %s' % expr[i]) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
450 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
451 | # If there are function calls, evaluate those |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
452 | if type (sym) is FunctionCall: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
453 | tabs += '\t' |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
454 | if sym.args: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
455 | sym.args = [evaluate (sym.args, verbose)] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
456 | tabs = tabs[:-1] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
457 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
458 | printFunc (tabs + 'Evaluating function call: %s' % (sym)) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
459 | expr[i] = Functions[sym.funcname]['function'] (*sym.args) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
460 | printFunc (tabs + '-> %s' % expr[i]) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
461 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
462 | i += 1 |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
463 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
464 | printFunc (tabs + 'Evaluate: %s' % expr) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
465 | runaway = 0 |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
466 | while True: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
467 | runaway += 1 |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
468 | if runaway > 1000: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
469 | raise ValueError ('infinite loop detected') |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
470 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
471 | op, i = find_priority_operator (expr) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
472 | if not op: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
473 | break |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
474 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
475 | if op.operands == 2: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
476 | argIndices = [i - 1, i + 1] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
477 | else: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
478 | argIndices = [i + 1] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
479 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
480 | args = [expr[x] for x in argIndices] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
481 | argIndices = sorted (argIndices, reverse=True) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
482 | printFunc (tabs + 'Processing: (%s, %d) with args %s' % (op, i, args)) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
483 | expr[i] = op.function (*args) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
484 | printFunc (tabs + '-> %s' % expr[i]) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
485 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
486 | for i2 in argIndices: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
487 | del expr[i2] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
488 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
489 | printFunc (tabs + 'Result: %s' % expr[0]) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
490 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
491 | if len(expr) != 1: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
492 | printFunc (tabs + 'Bad expression detected, tokens: %s' % expr) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
493 | raise ValueError ('malformed expression') |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
494 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
495 | return expr[0] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
496 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
497 | def repr_number (x): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
498 | """Returns a string representation for a real number""" |
126
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
499 | if math.fabs (x - math.floor(x)) < epsilon and PreferredBase != 10: |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
500 | digits='0123456789abcdef' |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
501 | assert PreferredBase <= len (digits), '''preferred base %d is too large''' % PreferredBase |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
502 | |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
503 | divisor = PreferredBase |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
504 | rep = '' |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
505 | x = int (x) |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
506 | runaway = 0 |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
507 | |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
508 | while x > 0: |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
509 | runaway += 1 |
127
66b206bd9510
Cranked up the runaway (8 was only for testing)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
126
diff
changeset
|
510 | if runaway > 1000: |
126
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
511 | raise ValueError('runaway triggered') |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
512 | |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
513 | i = (x % divisor) / (divisor / PreferredBase) |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
514 | x -= i * (divisor / PreferredBase) |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
515 | rep += digits[i] |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
516 | divisor *= PreferredBase |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
517 | |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
518 | rep += 'x' if PreferredBase == 16 else 'b' |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
519 | rep += '0' |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
520 | return rep[::-1] |
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
521 | |
123
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
522 | rep = '%.10f' % x |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
523 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
524 | if '.' in rep: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
525 | while rep[-1] == '0': |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
526 | rep = rep[:-1] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
527 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
528 | if rep[-1] == '.': |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
529 | rep = rep[:-1] |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
530 | |
126
0fc519afba89
- added binary support for calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
125
diff
changeset
|
531 | |
125
c44b1aa85257
- added hex support to the calculator
Teemu Piippo <crimsondusk64@gmail.com>
parents:
124
diff
changeset
|
532 | |
123
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
533 | return rep |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
534 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
535 | def repr_imaginary (x): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
536 | rep = repr_number (x) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
537 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
538 | if rep == '1': |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
539 | return 'i' |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
540 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
541 | if rep == '-1': |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
542 | return '-i' |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
543 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
544 | return rep + 'i' |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
545 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
546 | def represent (x): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
547 | """Returns a string representation of a float or complex number""" |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
548 | if math.fabs (x.imag) > epsilon: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
549 | if math.fabs (x.real) > epsilon: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
550 | # Complex number |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
551 | return '%s %s %s' % (repr_number (x.real), |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
552 | '+' if x.imag >= 0 else '-', |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
553 | repr_imaginary (math.fabs (x.imag))) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
554 | else: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
555 | # Pure imaginary number |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
556 | return repr_imaginary (x.imag) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
557 | else: |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
558 | # Real number |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
559 | return repr_number (x.real) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
560 | |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
561 | def calc (expr, verbose=False): |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
562 | expr = tokenize (expr) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
563 | process_parens (expr) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
564 | process_functions (expr) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
565 | process_operators (expr) |
aeb0d0788869
- added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
566 | return represent (evaluate (expr, verbose)) |