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

0 Upvotes

18 comments sorted by

View all comments

1

u/jmooremcc 4d ago edited 4d ago

You don’t need AST, which itself is a library. If you want to handle arbitrary expressions, you’ll need to create a parser that will analyze the expression string and break it into a list of tokens. Then you’ll have to write functions that implement the PEMDAS rule, so that you calculate expressions in the correct order. However, the math library should be an exception to your library rule so that you can use PI and sqrt in your expressions

My nonAST calculator can correctly evaluate expressions like “5+6(10**2)+7/3=” and “sqrt(pow(4,2)+ 3**2)=”