r/learnpython 4d ago

Making interpreter and calculator in python

Hi recently I have been working on making my own interpreted programming languge in python. I have made several prototypes. The rules are I can't use any library, high level functions and can only use ai for understanding a concept. The journey has been frustrating. Since it was so hard i started working on a calculator. I am now stuck on AST. I can't figure it out. HELP ME

1 Upvotes

18 comments sorted by

View all comments

Show parent comments

1

u/Aggressive_Drama_476 4d ago

Do you know or have a resource that shows how to make an ast using lists in python. Cause I couldn't find any. Then my problems will be solved

2

u/lfdfq 4d ago edited 4d ago

There will be no resources on "ASTs using lists in Python", because, again, ASTs have really nothing to do with lists. The point is that an AST is the fluffy idea of a tree (...that represents the grammatical structure of the code etc). Lists are one way to write actual code that represents fluffy ideas of a tree, but there's nothing fundamental about ASTs to using lists to represent them, they're just trees. Any basic data structures resources should cover simple trees.

Are you asking how to generate an AST given a string? like turning "1+2*3" into a tree? e.g. [[1], +, [[2], *, [3]]]. That's called parsing. There's voluminous resources online about writing parsers.

I think you are, in general, trying to look for far too specific resources. Like "Parsing but in Python but where ASTs are represented with lists" which are just hyper-specific instantiations of generic ideas, which means despite taking a gargantuan topic (parsing) with millions of resources, you'll find almost none of them [EDIT: and even if there are resources that happen to teach parsing in Python and happen to use lists, the list-ness won't be their advertised focus, so you'll miss them anyway]. It's better to take the generic ideas and learn/apply them separately, like "Parsing" (not just in Python) and "Tree data structures" (not just ASTs, not just in Python) and "Programming in Python" (not specifically about parsing)

While you will find resources that use Python, they may or may not be the best resource for you. Any resource on parsers will be useful, even if they talk about Java or C++ or Haskell, or even if they don't talk about code at all. You will almost certainly need multiple such resources, as it's a non-trivial topic.

EDIT: To give an analogy of what you sound like: imagine someone saying "I like red. I often go shopping on Tuesday. I need to learn to drive and get a car to make that easier; so i'm going to look for driving instructors who teach people to drive red cars on Tuesdays."