A Python Interpreter Written in Python

(aosabook.org)

58 points | by xk3 3 days ago

7 comments

  • blueybingo 46 minutes ago
    the article glosses over something worth pausing on: the `getattr` trick for dispatching instructions (replacing the big if-elif chain) is actaully a really elegant pattern that shows up in a lot of real interpreters and command dispatchers, not just toy ones -- worth studying that bit specifically if you're building anything with extensible command sets.
  • anitil 3 hours ago
    Oooh it's a bytecode interpreter! I was wondering how they'd fit a parser/tokenizer in 500 lines unless the first was `import tokenizer, parser`. And it looks like 1500ish lines according to tokei

    I think because python is a stack-based interpreter this is a really great way to get some exposure to how it works if you're not too familiar with C. A nice project!

  • tekknolagi 3 hours ago
    • bjoli 3 hours ago
      And, in some ways, PyPy. I still think it is the sanest way to implement Python.

      It makes me sad that I have to write C to make any meaningful changes to Python. Same goes for ruby. Rubinius was such a nice project.

      Hacking on schemes and lisps made me realize how much more fun it is when the language is implemented in the language itself. It also makes sure you have the right abstractions for solving a bunch of real problems.

      • actionfromafar 14 minutes ago
        Well, one could rewrite Python (perhaps piece by piece?) in Shedskin.

        Shedskin is very nearly Python compatible, one could say it is an implementation of Python.

      • anitil 3 hours ago
        > And, in some ways, PyPy

        What do you mean by that? I'm not familiar with PyPy

        • nxpnsv 3 hours ago
          PyPy is python implemented in python. It is fast.
          • notpushkin 2 hours ago
            https://pypy.org/

            It lags behind CPython in features and currently only supports Python versions up to 3.11. There was a big discussion a month ago: https://news.ycombinator.com/item?id=47293415

            But you can help! https://pypy.org/howtohelp.html

            https://opencollective.com/pypy

          • Doxin 2 hours ago
            PyPy is python implemented in RPython, which is technically a python subset. It's so restricted it might as well be a different language though.
            • bjoli 1 hour ago
              It is restricted in a way that you would restrict yourself to write high speed software in most languages, and I found it is not that restrictive compared to C that you would have to use if you were to write a fast Python library.
              • Doxin 25 minutes ago
                oh for sure, but I still feel like telling people pypy is written in python is misleading. it's written in something significantly like python, but it's not python.
  • woadwarrior01 1 hour ago
    aka A Metacircular Interpreter
  • andltsemi3 40 minutes ago
    "Yaw dog I heard you liked python, so I put python in your python so you can interpret python while you interpret python"
  • kevinten10 1 hour ago
    [dead]
  • hcfman 2 hours ago
    Just wondering why you stopped there? Why not a python interpreter for a python interpreter for python ?