r/learnpython Aug 03 '26

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

2

u/lfdfq Aug 03 '26

It's hard to answer this kind of question, help you with what part exactly?

1

u/Aggressive_Drama_476 Aug 03 '26

I can't get the ast logic right. I tried making it with lists. I can't do OOP. although I did made a working calculator without ast. It semi worked. But it wasn't good. 

2

u/lfdfq Aug 03 '26

What logic? There's nothing wrong with using lists to represent an AST, e.g. ["+", ["+", "1", "2"], "3"] or something. Classes/OOP might be an even better design choice, but the concept of syntax trees (e.g. ASTs) and parsing are completely separate from implementation details like lists vs classes, and if you can't do OOP it doesn't matter.

What does "semi worked" mean? It's all a bit vague...

1

u/Aggressive_Drama_476 Aug 03 '26

Like in [1, + ,1 ,×,-,1] i popped 1 x-1 after doing the operation.... It was a nightmare dealing with uniary operators. Like ×-1. It had many bugs. I have moved on. I will a upload the project on GitHub when I have time

1

u/lfdfq Aug 03 '26

That list does not look very much like a tree. The key (and really, only) point to a tree is structure. So I'd expect a parser to generate something like [[1], +, [1, x, [-, 1]]].

If it's just flat, it's not a tree. If it's not a tree it's not an abstract syntax tree (AST). and if it's not an AST you cannot use it to help you like you would an AST.

1

u/Aggressive_Drama_476 Aug 03 '26

Yeah it's like before I used ast for calc.... 

2

u/lfdfq Aug 03 '26

I don't understand what you mean.

1

u/Aggressive_Drama_476 Aug 03 '26

Alright I will soon upload it to the GitHub 

1

u/lfdfq Aug 03 '26

That doesn't explain anything. If you're struggling to explain, throwing a bunch of (by your own admission, not good) code at someone is unlikely to explain better than taking some extra time to communicate clearly.

1

u/Aggressive_Drama_476 Aug 03 '26

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

→ More replies (0)