Class: ShuntingYard::Parser

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/shunting_yard/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lexer: nil, interpreter: nil) ⇒ Parser

Returns a new instance of Parser.



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

def initialize(lexer: nil, interpreter: nil)
  @lexer = lexer || Lexer.new
  @interpreter = interpreter || Interpreter.new
end

Instance Attribute Details

#interpreterObject

Returns the value of attribute interpreter.



8
9
10
# File 'lib/shunting_yard/parser.rb', line 8

def interpreter
  @interpreter
end

#lexerObject

Returns the value of attribute lexer.



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

def lexer
  @lexer
end

Instance Method Details

#evaluate(input) ⇒ Object



18
19
20
21
# File 'lib/shunting_yard/parser.rb', line 18

def evaluate(input)
  rpn_tokens = to_rpn(input)
  interpreter.evaluate(rpn_tokens)
end

#to_rpn(input) ⇒ Object



23
24
25
26
# File 'lib/shunting_yard/parser.rb', line 23

def to_rpn(input)
  tokens = tokenize(input)
  interpreter.to_rpn(tokens)
end