Class: RubyLLM::Toolbox::SafeMath::Parser
- Inherits:
-
Object
- Object
- RubyLLM::Toolbox::SafeMath::Parser
- Defined in:
- lib/ruby_llm/toolbox/safe_math.rb
Overview
Recursive-descent parser with standard precedence.
Instance Method Summary collapse
- #current ⇒ Object
- #done? ⇒ Boolean
- #expression ⇒ Object
-
#initialize(tokens) ⇒ Parser
constructor
A new instance of Parser.
Constructor Details
#initialize(tokens) ⇒ Parser
Returns a new instance of Parser.
61 62 63 64 |
# File 'lib/ruby_llm/toolbox/safe_math.rb', line 61 def initialize(tokens) @tokens = tokens @pos = 0 end |
Instance Method Details
#current ⇒ Object
66 67 68 |
# File 'lib/ruby_llm/toolbox/safe_math.rb', line 66 def current @tokens[@pos] end |
#done? ⇒ Boolean
70 71 72 |
# File 'lib/ruby_llm/toolbox/safe_math.rb', line 70 def done? @pos >= @tokens.length end |
#expression ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/ruby_llm/toolbox/safe_math.rb', line 74 def expression value = term while op?(%w[+ -]) operator = take[1] rhs = term value = operator == "+" ? value + rhs : value - rhs end value end |