class Keisan::Functions::ProcFunction

Attributes

function_proc[R]

Public Class Methods

new(name, function_proc) click to toggle source
Calls superclass method Keisan::Function::new
# File lib/keisan/functions/proc_function.rb, line 6
def initialize(name, function_proc)
  raise Exceptions::InvalidFunctionError.new unless function_proc.is_a?(Proc)

  super(name, function_proc.arity)
  @function_proc = function_proc
end

Public Instance Methods

call(context, *args) click to toggle source
# File lib/keisan/functions/proc_function.rb, line 13
def call(context, *args)
  validate_arguments!(args.count)
  function_proc.call(*args).to_node
end
evaluate(ast_function, context = nil) click to toggle source
# File lib/keisan/functions/proc_function.rb, line 25
def evaluate(ast_function, context = nil)
  validate_arguments!(ast_function.children.count)
  context ||= Context.new

  ast_function.instance_variable_set(
    :@children,
    ast_function.children.map {|child| child.simplify(context).to_node}
  )

  if ast_function.children.all? {|child| child.well_defined?(context)}
    value(ast_function, context).to_node.evaluate(context)
  else
    ast_function
  end
end
simplify(ast_function, context = nil) click to toggle source
# File lib/keisan/functions/proc_function.rb, line 41
def simplify(ast_function, context = nil)
  validate_arguments!(ast_function.children.count)
  context ||= Context.new

  ast_function.instance_variable_set(
    :@children,
    ast_function.children.map {|child| child.simplify(context)}
  )

  if ast_function.children.all? {|child| child.is_a?(AST::ConstantLiteral)}
    value(ast_function, context).to_node.simplify(context)
  else
    ast_function
  end
end
value(ast_function, context = nil) click to toggle source
# File lib/keisan/functions/proc_function.rb, line 18
def value(ast_function, context = nil)
  validate_arguments!(ast_function.children.count)
  context ||= Context.new
  argument_values = ast_function.children.map {|child| child.value(context)}
  call(context, *argument_values).value(context)
end