Class: Regaxp::Eiruc

Inherits:
Object
  • Object
show all
Defined in:
lib/regaxp/eiruc.rb

Overview

Parses and execute Reverse Polish notation expressions.

Defined Under Namespace

Classes: UndefinedFun

Instance Method Summary collapse

Constructor Details

#initializeEiruc

Returns a new instance of Eiruc.



10
11
12
# File 'lib/regaxp/eiruc.rb', line 10

def initialize
  @functions = {}
end

Instance Method Details

#evaluate(expression) ⇒ Object



14
15
16
# File 'lib/regaxp/eiruc.rb', line 14

def evaluate(expression)
  expression.reduce([]) { |stack, token| process_token token, stack }[0]
end

#fun(name, fun_object = nil, &fun_body) ⇒ Object



18
19
20
21
22
# File 'lib/regaxp/eiruc.rb', line 18

def fun(name, fun_object = nil, &fun_body)
  # TODO: validates fun_object is a callable
  # TODO: validates arity?
  functions[name.to_sym] = fun_body || fun_object
end