Simple ML to Python
While studying for my exams, I tried to implement some examples from a book about virtual machines.
The result was a nice little program which can compile simple functional programs into python bytecode!
The only thing you need is the ply parser generator. To compile a function just use:
import topcompiler
@topcompile.compile
def func(x, y):
"""let i = x*x in y*i"""
pass # Compiles the docstring into a function
func(2, 3) == 12
Update:
TOPCompiler now also understands recursive definitions!
@topcompiler.compile
def fac(x):
"""
let rec fac = fun i ->
if i > 1 then
fac (i-1)*i
else
1
in fac x
"""
pass
There is a working quicksort in tests.py.
Check out the code at http://bitbucket.org/ebo/pyvm!
January 23rd, 2010 at 08:49
nice!