Permalink
Browse files

All round tidying up

  • Loading branch information...
1 parent 457d74e commit 69e4c1a3c66021b091d5f53513e4745b3d74e6a5 @dpiponi committed Mar 17, 2012
Showing with 52 additions and 57 deletions.
  1. +23 −48 mpython.py
  2. +20 −3 test1.py
  3. +9 −6 test2.py
View
@@ -1,81 +1,56 @@
#!/usr/bin/python
from ast import *
+import sys
def name(x, ctx):
return Name(id = x, ctx = ctx, lineno = 0, col_offset = 0)
def call(f, *args):
f = name(f, Load())
- return Call(func = f, args = list(args), lineno = 0, col_offset = 0, keywords = [], vararg = None)
+ return Call(func = f, args = list(args), lineno = 0,
+ col_offset = 0, keywords = [], vararg = None)
def func(args, body):
- return Lambda(args = arguments(args = args, defaults = []), body = body, vararg = None, lineno = 0, col_offset = 0)
+ return Lambda(args = arguments(args = args, defaults = []),
+ body = body, vararg = None, lineno = 0, col_offset = 0)
unit = Tuple(elts = [], lineno=0, col_offset = 0, ctx = Load())
def transform(elt, generators):
- if len(generators) ==1:
- var = generators[0].target
- var = name(var.id, Param())
+ elt = call("__singleton__", elt)
- m = generators[0].iter
- ifs = generators[0].ifs
- elt = call("singleton", elt)
- for i in ifs[-1::-1]:
- ifexp = IfExp(i,
- call('singleton',unit),
- call('fail'), lineno=0, col_offset=0)
- lambdaFunction = func([name('_', Param())], elt)
- elt = call("concatMap", lambdaFunction, ifexp)
- lambdaFunction = func([var], elt)
- return call("concatMap", lambdaFunction, m)
- else:
- var = generators[0].target
- var = name(var.id, Param())
+ for generator in generators[-1::-1]:
+ for i in generator.ifs[-1::-1]:
+ elt = call("__concatMap__",
+ func([name('_', Param())], elt),
+ IfExp(i,
+ call('__singleton__', unit),
+ call('__fail__'), lineno=0, col_offset=0))
- m = generators[0].iter
- ifs = generators[0].ifs
- elt = transform(elt, generators[1:])
- for i in ifs[-1::-1]:
- ifexp = IfExp(i,
- call('singleton', unit),
- call('fail'), lineno=0, col_offset=0)
- lambdaFunction = func([name('_', Param())], elt)
- elt = call("concatMap", lambdaFunction, ifexp)
- lambdaFunction = func([var], elt)
- return call("concatMap", lambdaFunction, m)
+ elt = call("__concatMap__",
+ func([name(generator.target.id, Param())], elt),
+ generator.iter)
+ return elt
class RewriteComp(NodeTransformer):
def visit_ListComp(self, node):
- newNode = node
- elt = node.elt
- generators = node.generators
-
- elt = RewriteComp().visit(elt)
- generators = map(RewriteComp().visit,generators)
-
- newNode = transform(elt, generators)
- return newNode
+ return transform(RewriteComp().visit(node.elt),
+ [RewriteComp().visit(generator) for generator in node.generators])
-def concatMap(f, x):
+def __concatMap__(f, x):
return [z for y in x for z in f(y)]
-def singleton(x):
+def __singleton__(x):
return [x]
-def fail():
+def __fail__():
return []
-#print concatMap(singleton, [1,2,3])
-
-#print concatMap(lambda x:singleton(10*x), [1,2,3])
-
-import sys
source = open(sys.argv[1]).read()
e = compile(source, "<string>", "exec", PyCF_ONLY_AST)
e = RewriteComp().visit(e)
-f = compile(e, "<string>", "exec")
+f = compile(e, sys.argv[1], "exec")
print f
exec f
print "Done"
View
@@ -1,4 +1,21 @@
-a = [(x, y) for x in [0,1]
- for y in [0,1]]
+# Some simple regression tests
-print a
+a = [(x, y, z) for x in [0, 1, 2]
+ for y in [0, 1, 2]
+ for z in [0, 1, 2]]
+
+print len(a)
+
+b = [(x, y) for x in [0,1]
+ for y in [0,1]
+ if x+y < 2]
+
+print b
+
+c = [(x, y, z) for x in range(1, 10)
+ for y in range(1, 10)
+ if x < y
+ for z in range(1, 10)
+ if x*x+y*y == z*z]
+
+print c
View
@@ -1,25 +1,28 @@
import math
-def concatMap(k, m):
+def __concatMap__(k, m):
return lambda c:m(lambda a:k(a)(c))
-def singleton(x):
+def __singleton__(x):
return lambda f:f(x)
def callCC(f):
return lambda c:f(lambda a:lambda _:c(a))(c)
-def fail():
+def __fail__():
raise "Failure is not an option for continuations"
+def ret(x):
+ return __singleton__(x)
+
def id(x):
return x
def solve(a, b, c):
return callCC(lambda throw: [((-b-d)/(2*a), (-b+d)/(2*a))
- for a0 in (singleton(a) if a!=0 else throw("Not quadratic"))
- for d2 in singleton(b*b-4*a*c)
- for d in (singleton(math.sqrt(d2)) if d2>=0 else throw("No roots"))
+ for a0 in (ret(a) if a!=0 else throw("Not quadratic"))
+ for d2 in ret(b*b-4*a*c)
+ for d in (ret(math.sqrt(d2)) if d2>=0 else throw("No roots"))
])
print solve(1, 0, -9)(id)

0 comments on commit 69e4c1a

Please sign in to comment.