Class: ShuntingYard::Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/shunting_yard/interpreter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInterpreter

Returns a new instance of Interpreter.



6
7
8
9
# File 'lib/shunting_yard/interpreter.rb', line 6

def initialize
  @functions = []
  @operators = []
end

Instance Attribute Details

#functionsObject

Returns the value of attribute functions.



3
4
5
# File 'lib/shunting_yard/interpreter.rb', line 3

def functions
  @functions
end

#operatorsObject

Returns the value of attribute operators.



4
5
6
# File 'lib/shunting_yard/interpreter.rb', line 4

def operators
  @operators
end

Instance Method Details

#add_function(*args) ⇒ Object



11
12
13
# File 'lib/shunting_yard/interpreter.rb', line 11

def add_function(*args)
  functions << Function.new(*args)
end

#add_operator(*args) ⇒ Object



15
16
17
# File 'lib/shunting_yard/interpreter.rb', line 15

def add_operator(*args)
  operators << Operator.new(*args)
end

#evaluate(rpn_tokens) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/shunting_yard/interpreter.rb', line 73

def evaluate(rpn_tokens)
  rpn = rpn_tokens.dup
  stack = []

  while rpn.any?
    current = rpn.shift

    case current
    when Function, Operator
      arity = current.evaluator.arity
      raise InvalidArgumentsCountError if stack.size < arity

      operands = stack.pop(arity)
      stack << current.evaluator.(*operands)
    when Operand
      stack << current.value
    end
  end

  raise InvalidArgumentsCountError if stack.size > 1

  stack[0]
end

#to_rpn(source_tokens) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/shunting_yard/interpreter.rb', line 19

def to_rpn(source_tokens)
  tokens = source_tokens.dup
  output = ReversePolishNotation.new
  op_stack = []

  while tokens.any?
    current = match_token(tokens.shift)

    case current
    when ArgumentSeparator
      while op_stack.any? &&
            op_stack.last.class != Parenthesis
        output << op_stack.pop
      end
    when Function
      op_stack << current
    when Parenthesis
      case current.side
      when :left
        op_stack << current
      when :right
        while op_stack.last.class != Parenthesis
          raise MismatchedParenthesesError if op_stack.empty?

          output << op_stack.pop
        end

        op_stack.pop
      end
    when Operand
      output << current
    when Operator
      while op_stack.any? &&
            op_stack.last.class != Parenthesis &&
            (op_stack.last.class == Function ||
              op_stack.last.precedence > current.precedence ||
              op_stack.last.precedence == current.precedence && current.associativity == :left)
        output << op_stack.pop
      end

      op_stack << current
    end
  end

  while op_stack.any?
    current = op_stack.pop
    raise MismatchedParenthesesError if current.class == Parenthesis

    output << current
  end

  output
end