Class: Skeem::SkmLambdaRep

Inherits:
SkmMultiExpression show all
Includes:
DatumDSL
Defined in:
lib/skeem/s_expr_nodes.rb

Overview

Parse tree representation of a Lambda

  • Not bound to a frame (aka environment)
  • Knows the parse representation of its embedded definitions
  • Knows the parse representation of the body

Instance Attribute Summary collapse

Attributes inherited from SkmElement

#position

Instance Method Summary collapse

Methods included from DatumDSL

#boolean, #char, #identifier, #integer, #list, #rational, #real, #string, #to_datum, #vector

Methods inherited from SkmMultiExpression

#accept

Methods inherited from SkmElement

#accept, #boolean?, #char?, #complex?, #done!, #integer?, #list?, #null?, #number?, #pair?, #quasiquote, #quoted!, #rational?, #real?, #skm_eq?, #string?, #symbol?, #unquoted!, #vector?, #verbatim?

Constructor Details

#initialize(aPosition, theFormals, aBody) ⇒ SkmLambdaRep

Returns a new instance of SkmLambdaRep.



571
572
573
574
575
576
# File 'lib/skeem/s_expr_nodes.rb', line 571

def initialize(aPosition, theFormals, aBody)
  super(aPosition)
  @formals = theFormals
  @definitions = aBody[:defs]
  @sequence = aBody[:sequence]
end

Instance Attribute Details

#definitionsObject (readonly)

Returns the value of attribute definitions.



568
569
570
# File 'lib/skeem/s_expr_nodes.rb', line 568

def definitions
  @definitions
end

#formalsArray<SkmIdentifier> (readonly)

Returns the argument names.

Returns:



567
568
569
# File 'lib/skeem/s_expr_nodes.rb', line 567

def formals
  @formals
end

#sequenceObject (readonly)

Returns the value of attribute sequence.



569
570
571
# File 'lib/skeem/s_expr_nodes.rb', line 569

def sequence
  @sequence
end

Instance Method Details

#arityObject



596
597
598
# File 'lib/skeem/s_expr_nodes.rb', line 596

def arity
  formals.arity
end

#associationsObject



618
619
620
# File 'lib/skeem/s_expr_nodes.rb', line 618

def associations
  i[formals definitions sequence]
end

#bind_locals(aRuntime, theActuals) ⇒ Object



622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'lib/skeem/s_expr_nodes.rb', line 622

def bind_locals(aRuntime, theActuals)
  actuals =  theActuals
  count_actuals = actuals.size

  if (count_actuals < required_arity) ||
     ((count_actuals > required_arity) &&
     !formals.variadic?)
    # $stderr.puts "Error"
    # $stderr.puts self.inspect
    raise StandardError, msg_arity_mismatch(theActuals)
  end
  return if count_actuals.zero? && !formals.variadic?

  bind_required_locals(aRuntime, theActuals)
  if formals.variadic?
    variadic_part_raw = actuals.drop(required_arity)
    variadic_part = variadic_part_raw.map do |actual|
      case actual
        when ProcedureCall, SkmQuotation
          actual.evaluate(aRuntime)
        else
          to_datum(actual)
      end
    end
    variadic_arg_name = formals.formals.last
    args_coll = SkmPair.create_from_a(variadic_part)
    a_def = SkmBinding.new(variadic_arg_name, args_coll)
    a_def.evaluate(aRuntime)
    aRuntime.add_binding(a_def.variable, a_def.value)
    # $stderr.puts "Tef #{a_def.inspect}"
    # $stderr.puts "Tef #{actuals.inspect}"
    # $stderr.puts "Tef #{variadic_part.inspect}"
    # $stderr.puts "Tef #{aProcedureCall.inspect}"
    # a_def.evaluate(aRuntime)
  end
  # aProcedureCall.operands_consumed = true
end

#bound!(aFrame) ⇒ Object



607
608
609
# File 'lib/skeem/s_expr_nodes.rb', line 607

def bound!(aFrame)
  set_cond_environment(aFrame)
end

#call(aRuntime, theActuals) ⇒ Object



590
591
592
593
594
# File 'lib/skeem/s_expr_nodes.rb', line 590

def call(aRuntime, theActuals)
  set_cond_environment(aRuntime.environment) # Last chance for anonymous lambda
  application = SkmProcedureExec.new(self)
  application.run!(aRuntime, theActuals)
end

#callable?Boolean

Returns:

  • (Boolean)


582
583
584
# File 'lib/skeem/s_expr_nodes.rb', line 582

def callable?
  true
end

#evaluate(aRuntime) ⇒ Object



578
579
580
# File 'lib/skeem/s_expr_nodes.rb', line 578

def evaluate(aRuntime)
  SkmLambda.new(self, aRuntime)
end

#inspectObject



611
612
613
614
615
616
# File 'lib/skeem/s_expr_nodes.rb', line 611

def inspect
  result = inspect_prefix + "@object_id=#{object_id.to_s(16)}, "
  result << inspect_specific
  result << inspect_suffix
  result
end

#procedure?Boolean

Returns:

  • (Boolean)


586
587
588
# File 'lib/skeem/s_expr_nodes.rb', line 586

def procedure?
  true
end

#required_arityObject



600
601
602
# File 'lib/skeem/s_expr_nodes.rb', line 600

def required_arity
  formals.required_arity
end