The inventor's paradox is that often a general solution is easier than a specific solution. So I implemented a general order-of-precedence parser that takes in a list of symbols, a list of commands, and a string. It splits the string recursively by each symbol in the list, forming a tree of commands. It also parses the leafs as numbers.
During implementation, I discovered that PEMDAS is a lie! If we group multiplication before division we get this:
Instead of the correct version:
So the correct order of precedence is actually PEDMSA along with a left-to-right rule on division and a right-to-left rule on exponentiation.
You can run tests using
make test
or
guile -l precedence-parser.scm -c '(test)'