Implementing operator precedence in my calculator interpreter

As part of learning Ruby am trying to implement a basic interpreter
which reads input and do basic arithmetic calculations. So far basic
arithmetic operations are working but having problem in operator
precedence. Which is not handled yet. This is the code. Am at a beginner
level. Any mistakes in this code are due to my lack of knowledge. How
this code can be modified to handle operator precedence.

Sample output
2+2+2 = 6 #correct
10+10/2 = 10 # incorrect as in irb answer must be 15

Link to file :
https://github.com/pckrishnadas88/ruby-interpreter/blob/master/calc_version2.rb

Take a look at this algorithm:

https://en.wikipedia.org/wiki/Shunting-yard_algorithm

It is a basic rather straight-forward algorithm for parsing infix
notation with operator precedence and convert it to reversed Polish
notation, which is easy to evaluate.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs