Class: Skeem::SkmLambda

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

Overview

class

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(aRepresentation, aRuntime) ⇒ SkmLambda

Returns a new instance of SkmLambda.



723
724
725
726
727
# File 'lib/skeem/s_expr_nodes.rb', line 723

def initialize(aRepresentation, aRuntime)
  super(nil)
  @representation = aRepresentation
  @environment = aRuntime.environment
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



719
720
721
# File 'lib/skeem/s_expr_nodes.rb', line 719

def environment
  @environment
end

#representationObject (readonly)

Returns the value of attribute representation.



718
719
720
# File 'lib/skeem/s_expr_nodes.rb', line 718

def representation
  @representation
end

Instance Method Details

#arityObject



756
757
758
# File 'lib/skeem/s_expr_nodes.rb', line 756

def arity
  formals.arity
end

#associationsObject



778
779
780
# File 'lib/skeem/s_expr_nodes.rb', line 778

def associations
  i[formals definitions sequence]
end

#bind_locals(aRuntime, theActuals) ⇒ Object



782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
# File 'lib/skeem/s_expr_nodes.rb', line 782

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



767
768
769
# File 'lib/skeem/s_expr_nodes.rb', line 767

def bound!(aFrame)
  set_cond_environment(aFrame)
end

#call(aRuntime, theActuals) ⇒ Object

TODO def quasiquote(aRuntime) quasi_test = test.quasiquote(aRuntime) quasi_consequent = consequent.quasiquote(aRuntime) quasi_alternate = alternate.quasiquote(aRuntime)

   self.class.new(position, quasi_test, quasi_consequent, quasi_alternate)
end


750
751
752
753
754
# File 'lib/skeem/s_expr_nodes.rb', line 750

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)


733
734
735
# File 'lib/skeem/s_expr_nodes.rb', line 733

def callable?
  true
end

#doppelganger(aRuntime) ⇒ Object



848
849
850
851
852
# File 'lib/skeem/s_expr_nodes.rb', line 848

def doppelganger(aRuntime)
  twin = dup
  twin.set_cond_environment(aRuntime.environment.dup)
  twin
end

#dup_cond(aRuntime) ⇒ Object



838
839
840
841
842
843
844
845
846
# File 'lib/skeem/s_expr_nodes.rb', line 838

def dup_cond(aRuntime)
  if environment
    self
  else
    twin = dup
    twin.set_cond_environment(aRuntime.environment)
    twin
  end
end

#evaluate(_runtime) ⇒ Object



729
730
731
# File 'lib/skeem/s_expr_nodes.rb', line 729

def evaluate(_runtime)
  self
end

#evaluate_sequence(aRuntime) ⇒ Object



820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
# File 'lib/skeem/s_expr_nodes.rb', line 820

def evaluate_sequence(aRuntime)
  result = nil
  sequence&.each do |cmd|
    if cmd.kind_of?(SkmLambda)
      result = cmd.dup_cond(aRuntime)
    else
      result = cmd.evaluate(aRuntime)
    end
  rescue NoMethodError => e
    $stderr.puts inspect
    $stderr.puts sequence.inspect
    $stderr.puts cmd.inspect
    raise e
  end

  result
end

#inspectObject



771
772
773
774
775
776
# File 'lib/skeem/s_expr_nodes.rb', line 771

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

#procedure?Boolean

Returns:

  • (Boolean)


737
738
739
# File 'lib/skeem/s_expr_nodes.rb', line 737

def procedure?
  true
end

#required_arityObject



760
761
762
# File 'lib/skeem/s_expr_nodes.rb', line 760

def required_arity
  formals.required_arity
end

#set_cond_environment(theFrame) ⇒ Object

rubocop: disable Naming/AccessorMethodName

Raises:

  • (StandardError)


856
857
858
859
860
861
862
863
864
865
866
867
# File 'lib/skeem/s_expr_nodes.rb', line 856

def set_cond_environment(theFrame)
  # $stderr.puts "Lambda #{object_id.to_s(16)}, env [#{environment.object_id.to_s(16)}]"
  # $stderr.puts "  Runtime environment: #{theFrame.object_id.to_s(16)}"
  # $stderr.puts "  Called from #{caller(1, 1)}"
  raise StandardError unless theFrame.kind_of?(SkmFrame)

  unless environment
    @environment = theFrame
    freeze
    # $stderr.puts "  Lambda's environment updated!"
  end
end