-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.py
112 lines (88 loc) · 2.83 KB
/
context.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
#
# Copyright 2009 Benjamin Mellor
#
# This file is part of Fundy.
#
# Fundy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
class SimpleRecord(object):
def __init__(self, graph):
self.graph = graph
def get_assoc(self):
assert False
def get_prec(self):
assert False
def get_fixity(self):
assert False
def __repr__(self):
"""
NOT_RPYTHON:
"""
return repr(self.graph)
class OperatorRecord(SimpleRecord):
def __init__(self, graph, assoc, prec, fixity):
self.graph = graph
self.assoc = assoc
self.prec = prec
self.fixity = fixity
def get_assoc(self):
return self.assoc
def get_prec(self):
return self.prec
def get_fixity(self):
return self.fixity
def __repr__(self):
"""
NOT_RPYTHON:
"""
return '<%r, %r, %r> %r' % (self.assoc, self.prec, self.fixity,
self.graph)
class Context(object):
def __init__(self):
self.graphs = {}
def copy(self):
other = Context()
other.graphs = self.graphs.copy()
return other
def bind(self, name, graph):
self.graphs[name] = SimpleRecord(graph)
def bind_operator(self, name, graph, assoc, prec, fixity):
# TODO: support overloaded names
self.graphs[name] = OperatorRecord(graph, assoc, prec, fixity)
def lookup(self, name):
return self.graphs[name].graph
def is_operator(self, name):
return isinstance(self.graphs[name], OperatorRecord)
def get_assoc(self, name):
return self.graphs[name].get_assoc()
def get_prec(self, name):
return self.graphs[name].get_prec()
def get_fixity(self, name):
return self.graphs[name].get_fixity()
def items(self):
"""
NOT_RPYTHON:
"""
for name in self.graphs:
yield name, self.lookup(name)
def update(self, other):
self.graphs.update(other.graphs)
def __repr__(self):
"""
NOT_RPYTHON:
"""
names = sorted(self.graphs.keys())
lines = ['%s -> %r' % (name, self.graphs[name])
for name in sorted(self.graphs.keys())]
return '\n'.join(lines)