Class: Klam::Environment

Constant Summary

Constants included from Primitives::Lists

Primitives::Lists::EMPTY_LIST

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Primitives::Interop

#rb_const, #rb_send, #rb_send_block, uncurry

Methods included from Primitives::Arithmetic

#*, #+, #-, #/, #<, #<=, #>, #>=, #number?

Methods included from Primitives::Time

#get_time

Methods included from Primitives::Streams

#close, #open, #read_byte, #write_byte

Methods included from Primitives::Vectors

#absvec_read, #absvec_store, #absvector, #absvector?

Methods included from Primitives::GenericFunctions

#equal, #eval_kl

Methods included from Primitives::Lists

#cons, #cons?, #hd, #tl

Methods included from Primitives::ErrorHandling

#error_to_string, #simple_error

Methods included from Primitives::Assignments

#set, #value

Methods included from Primitives::Strings

#cn, #n_to_string, #pos, #str, #string?, #string_to_n, #tlstr

Methods included from Primitives::Symbols

#intern

Methods included from Primitives::BooleanOperations

#_if, #and, #or

Constructor Details

#initializeEnvironment

Returns a new instance of Environment.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/klam/environment.rb', line 16

def initialize
  # The global assignments namespace. Errors are thrown here in the
  # missing element handler rather than in the value primitive in order
  # to facilitate inlining later.
  @assignments = ::Hash.new do |_, name|
    ::Kernel.raise ::Klam::Error, "The variable #{name} is unbound."
  end

  @arities = ::Hash.new { |h, k| h[k] = __arity(k) }
  @curried_methods = ::Hash.new { |h, k| h[k] = __method(k).to_proc.curry }

  # Grab a handle to this object's eigenclass for use later when the
  # compiled code needs to reference it. It is used, e.g., when renaming
  # methods.
  @eigenclass = class << self; self; end

  @compiler = ::Klam::Compiler.new(self)

  # The open primitive depends on having *home-directory* assigned.
  set(:"*home-directory*", ::Dir.pwd)
  set(:"*dump-kl*", false)
  set(:"*dump-rb*", false)
  set(:"*include-backtrace-in-error-string*", false)
end

Class Method Details

.def_method(name, proc) ⇒ Object

Define method is private, so open it up



61
62
63
# File 'lib/klam/environment.rb', line 61

def def_method(name, proc)
  define_method(name, proc)
end

.rename_method(old_name, new_name) ⇒ Object



65
66
67
68
# File 'lib/klam/environment.rb', line 65

def rename_method(old_name, new_name)
  alias_method(new_name, old_name)
  remove_method(old_name)
end

Instance Method Details

#__apply(rator, *rands) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/klam/environment.rb', line 41

def __apply(rator, *rands)
  if rator.kind_of?(::Symbol)
    @curried_methods[rator].call(*rands)
  else
    rator.call(*rands)
  end
end

#__arity(sym) ⇒ Object



53
54
55
56
57
# File 'lib/klam/environment.rb', line 53

def __arity(sym)
  @eigenclass.instance_method(sym).arity
rescue ::NameError
  -1
end

#__method(sym) ⇒ Object



49
50
51
# File 'lib/klam/environment.rb', line 49

def __method(sym)
  @eigenclass.instance_method(sym).bind(self)
end