Skip to content

Instantly share code, notes, and snippets.

@MostAwesomeDude
Created May 4, 2018 14:35
Show Gist options
  • Save MostAwesomeDude/c9253193ff4796098aeefcb45270c3c1 to your computer and use it in GitHub Desktop.
Save MostAwesomeDude/c9253193ff4796098aeefcb45270c3c1 to your computer and use it in GitHub Desktop.
from rpython.rlib.unroll import unrolling_iterable
class Vertex(object):
_handle = None
class Selector(object):
def __init__(self, label):
self._label = label
def repr(self):
return "<Selector(%s)>" % self._label
def makeVertex(name, fields):
selectors = {Selector(name + "." + f): f for f in fields}
ss = unrolling_iterable([(k, "_field_" + v) for k, v in selectors.iteritems()])
class Sub(Vertex):
def put(self, selector, target, address):
for s, f in ss:
if s is selector:
setattr(self, f, (target, address))
return
assert False
def get(self, selector):
for s, f in ss:
if s is selector:
return getattr(self, f)
assert False
def repr(self):
l = []
for s, f in ss:
l.append(f + ": " + getattr(self, f).repr())
pieces = ", ".join(l)
return "<%s(%s)>" % (name, pieces)
Sub.__name__ = name
for k, v in selectors.iteritems():
setattr(Sub, v, k)
setattr(Sub, "_field_" + v, (None, None))
return Sub
Top = makeVertex("Top", ["handle"])
Drop = makeVertex("Drop", ["_"])
Copy = makeVertex("Copy", ["_", "a", "b"])
class Int(makeVertex("Int", ["_"])):
_immutable_fields_ = '_i',
_field__ = None, None
def __init__(self, i):
self._i = i
def pairUp(v1, f1, v2, f2):
v1.put(f1, v2, f2)
v2.put(f2, v1, f1)
def copyDropLeft(vertex):
if not isinstance(vertex, Copy):
return False
drop, _ = vertex.get(Copy.a)
if not isinstance(drop, Drop):
return False
v1, f1 = vertex.get(Copy.b)
v2, f2 = vertex.get(Copy._)
print v1, f1.repr(), v2, f2.repr()
pairUp(v1, f1, v2, f2)
return True
def copyDropRight(vertex):
if not isinstance(vertex, Copy):
return False
drop, _ = vertex.get(Copy.b)
if not isinstance(vertex.get(Copy.b), Drop):
return False
v1, f1 = vertex.get(Copy.a)
v2, f2 = vertex.get(Copy._)
print v1, f1.repr(), v2, f2.repr()
pairUp(v1, f1, v2, f2)
return True
def entryPoint(argv):
c = Copy()
pairUp(Drop(), Drop._, c, Copy.a)
pairUp(Int(42), Int._, c, Copy.b)
graph = Top()
pairUp(c, Copy._, graph, Top.handle)
print graph, graph._field_handle
print copyDropLeft(c)
print copyDropRight(c)
print graph, graph._field_handle
return 0
def target(driver, *args):
driver.exe_name = "liger"
return entryPoint, None
if __name__ == "__main__":
sys.exit(entryPoint(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment