Hacker News new | past | comments | ask | show | jobs | submit login
A Simple Interpreter in Python (ruslanspivak.com)
49 points by dunefox on Aug 21, 2021 | hide | past | favorite | 7 comments



Man, this is was one of my favorite articles of all time. I’ve created a functional programming language for my startup thanks for this article. Obviously that there was many things that the article didn’t touch like how to mame tail call optmizations or like how to suuport dictionaries or lists but anyway. This was the quickstart for me for start making things.

I also recommend people reading this book: it is super complete and comprehensive to get started and get your hands dirty https://monkeylang.org/

Another thing i recommend which is what i ended up doing: instead of interpreting stuff like

def visit_BinaryOperation(self, node): if node.operation == “add”: return node.right + node.left

I just simply add the values inside of an object and then add like

def visit_BinaryOperation(self, node): if node.operation == “add”: return node.right._add_(node.left)

This way i can handle for example what happens if we add a string with a string, or a int with a float INSIDE of the object of this value. So if node.right is an object of type String we handle in the _add_ method in the String class what happens if we add together objects of different types like for example an Int with a String.

But that’s it. It is an awesome recommendation


But left + right in Python is already syntactic sugar for calling left.__add__(right) and, if that returns NotImplemented, calling right.__radd__(left). So, replacing node.right + node.left with node.right.__add__(node.left) does nothing except sacrifice Python’s built-in fallback handling.


When you use the + operator in Python, it already calls __add__ (two underscores on each side) on the left operand with the right operand. Like x + y is x.__add__(y). https://docs.python.org/3/reference/datamodel.html#object.__...


I'm working through it at the moment (in Python and Julia) and it's great so far.

> https://monkeylang.org/

Do you know if the tutorials strongly depend on Go or can I easily map it to other languages?


The initial post says that the series will include implementing a debugger, but I don't see that in a scan of the articles. Anyone know if that is still to come, or was dropped, or what?


(2015)


The latest post in the series is from 2020.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: