Class: RubyLLM::Toolbox::SafeMath::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/toolbox/safe_math.rb

Overview

Recursive-descent parser with standard precedence.

Instance Method Summary collapse

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

#currentObject



66
67
68
# File 'lib/ruby_llm/toolbox/safe_math.rb', line 66

def current
  @tokens[@pos]
end

#done?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/ruby_llm/toolbox/safe_math.rb', line 70

def done?
  @pos >= @tokens.length
end

#expressionObject



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