-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
148 lines (118 loc) · 4.07 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
This file contains important information for the calculator.
"""
from collections import OrderedDict, namedtuple
from typing import List
Token = namedtuple('Token', ('name', 'value'))
class RuleMatch:
def __init__(self, name: str, matched: List[Token]):
self.name = name
self.matched = matched
self.value = None
def __str__(self):
return self._str(self)
def __repr__(self):
return str(self)
def _str(self, node, depth=0) -> str:
output = (('\t' * depth) + node.name + ' = ' + str(node.value.value if node.value else None)) + '\n'
for matched in node.matched:
if isinstance(matched, RuleMatch) and matched.matched:
output += self._str(matched, depth + 1)
else:
output += (('\t' * (depth + 1)) + matched.name + ': ' + matched.value) + '\n'
return output
token_map = OrderedDict((
(r'\d+(?:\.\d+)?', 'NUM'),
(r'sqrt', 'OPR'),
(r'exp', 'OPR'),
(r'det', 'OPR'),
(r'adj', 'OPR'),
(r'trans', 'OPR'),
(r'cof', 'OPR'),
(r'inv', 'OPR'),
(r'identity', 'OPR'),
(r'trnsform', 'OPR'),
(r'rref', 'OPR'),
(r'solve', 'OPR'),
(r'eval', 'OPR'),
(r'ls', 'OPR'),
(r'qr', 'OPR'),
(r'[a-zA-Z_]+', 'IDT'),
(r'=', 'EQL'),
(r'\+', 'ADD'),
(r'-', 'ADD'),
(r'\*\*', 'POW'),
(r'\*', 'MUL'),
(r'\/', 'MUL'),
(r'%', 'MUL'),
(r'\^', 'POW'),
(r'\(', 'LPA'),
(r'\)', 'RPA'),
(r'\[', 'LBR'),
(r'\]', 'RBR'),
(r'\,', 'CMA'),
(r'\|', 'PPE')
))
remove = ('EQL', 'LPA', 'RPA', 'LBR', 'RBR', 'CMA', 'PPE')
class ImmutableIndexedDict:
def __init__(self, data):
self._keys = tuple(item[0] for item in data if not item[0].startswith('^'))
self._data = {key.lstrip('^'): values for key, values in data}
# Caching indices cuts down on runtime.
idx = 0
self._key_indices = {}
for key in tuple(item[0] for item in data):
if not key.startswith('^'):
self._key_indices[key] = idx
idx += 1
self._len = len(self._key_indices)
def __getitem__(self, key):
return self._data[key]
def __len__(self):
return self._len
def index(self, key):
return self._key_indices.get(key, None)
def key_at(self, i):
return self._keys[i]
rules_map = {
'infix': ImmutableIndexedDict((
('asn', ('asb EQL add',)),
('^asb', ('IDT CMA asb', 'IDT')),
('add', ('mul ADD add', 'mui ADD add',)),
('mui', ('pow mul',)),
('mul', ('pow MUL mul',)),
('pow', ('opr POW pow',)),
('opr', ('OPR LPA opb RPA',)),
('^opb', ('add CMA opb', 'add')),
('neg', ('ADD num', 'ADD opr')),
('var', ('IDT',)),
('num', ('NUM', 'LPA add RPA')),
('mat', ('LBR mbd RBR',)),
('^mbd', ('mrw PPE mbd', 'mrw')),
('^mrw', ('add CMA mrw', 'add')),
)),
'prefix': ImmutableIndexedDict((
('asn', ('EQL asb add',)),
('^asb', ('CMA IDT asb', 'IDT')),
('opr', ('OPR opb',)),
('^opb', ('CMA pow opb', 'pow')),
('pow', ('POW mul pow',)),
('mul', ('MUL add mul',)),
('add', ('ADD opr add',)),
('neg', ('ADD num', 'ADD opr')),
('var', ('IDT',)),
('num', ('NUM', 'pow')),
)),
}
left_assoc = {
'ADD': 'add',
'MUL': 'mul',
}
precedence = [
'pow',
'mul',
'add'
]
operations = ('pos', 'neg', 'add', 'sub', 'mul', 'div', 'mod', 'pow', 'sqrt', 'exp', 'identity', 'det', 'trans', 'cof', 'adj', 'inv', 'rref', 'trnsform', 'solve', 'ls', 'eval')
class EvaluationException(Exception):
pass