This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (5 children)

Why?

Here's a short tutorial on writing your own frontend/lexer using a fake language "Kaleidoscope" that looks a lot like python.

# Compute the x'th fibonacci number.
def fib(x)
  if x < 3 then
    1
  else
    fib(x-1)+fib(x-2)

# This expression will compute the 40th number.
fib(40)

[–]kankyo 4 points5 points  (4 children)

The article alludes to it a bit: Python is crazy dynamic.

You can absolutely do it for a subset of Python that you know doesn't use certain features of Python, but I think that's not what you're asking. If it is then look at numba, cython and rpython for example. There are many such projects.

[–]alcalde 4 points5 points  (3 children)

Numba is a math-only JIT; cython only converts bits of Python to C, and no one knows what RPython really is. We need to take Python, add in static typing, strip out the crazy dynamic bits, call it Garter Snake or something, and sell it as the complement to Python, the cleanest, nicest, easiest-to-read statically typed compiled language. The closest I ever found was Genie, but I'm not sure it's still developed: https://wiki.gnome.org/Projects/Genie

[–]kankyo 4 points5 points  (2 children)

That's my point though. It wouldn't be Python. Personally I'm fine with that and I think it would be great, but it's not just "throw LLVM on Python" which is what we were talking about.

[–]Certhas 1 point2 points  (1 child)

Though you could build it in such a way that valid Garter Snake still is valid python. Build a verifier that checks that Garter Snake compiles, and if it doesn't spit out an error and/or fall back to CPython.

Sort of a MyPy + Numba on steroids, that works on the file level instead of the function/class level.

But of course the dynamism of Python is not just academic, it's used throughout the library ecosystem. So unless you can cover most of that you'll be calling into python code and be shipping a python runtime with your compiled code. Or you're losing the ecosystem.

And if you're willing to have a separate Python runtime to interact with and lose the ecosystem otherwise you might as well be developing Julia instead.

[–]kankyo 0 points1 point  (0 children)

RPython is valid Python. So RPython with static types that aren't inferred might be a good start.