Class: Ruspea::Runtime::Env

Inherits:
Object
  • Object
show all
Includes:
Error
Defined in:
lib/ruspea/runtime/env.rb

Direct Known Subclasses

Language::Core, Language::Ruby

Defined Under Namespace

Classes: Empty

Instance Method Summary collapse

Constructor Details

#initialize(context = nil, table = nil) ⇒ Env

Returns a new instance of Env.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruspea/runtime/env.rb', line 36

def initialize(context = nil, table = nil)
  @table =
    if table.nil?
      {}
    else
      table.dup
    end
  @context = context || Empty.instance

  @fn = Fn.new(fn_define, fn_fetch)
end

Instance Method Details

#==(other) ⇒ Object



77
78
79
80
# File 'lib/ruspea/runtime/env.rb', line 77

def ==(other)
  return false if self.class != other.class
  @table == other.table && @context == other.context
end

#around(env) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/ruspea/runtime/env.rb', line 65

def around(env)
  new_context = env
    .context
    .around(self)

  Env.new(new_context, env.table)
end

#call(*args, context: nil, evaler: nil) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/ruspea/runtime/env.rb', line 56

def call(*args, context: nil, evaler: nil)
  # evaler will always send an array
  # to a #call that is not a #arity
  if args.is_a?(Array) && args.length == 1
    args = args.first
  end
  @fn.call(*args, context: context, evaler: evaler)
end

#define(sym, value) ⇒ Object



48
49
50
# File 'lib/ruspea/runtime/env.rb', line 48

def define(sym, value)
  @table[sym] = value
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/ruspea/runtime/env.rb', line 73

def eql?(other)
  self == other
end

#hashObject



82
83
84
# File 'lib/ruspea/runtime/env.rb', line 82

def hash
  @table.hash + @context.hash + :rsp_env.hash
end

#inspectObject



86
87
88
89
90
# File 'lib/ruspea/runtime/env.rb', line 86

def inspect
  @table.map { |k, v|
    "#{k} => #{v}"
  }
end

#lookup(sym) ⇒ Object



52
53
54
# File 'lib/ruspea/runtime/env.rb', line 52

def lookup(sym)
  @table.fetch(sym) { @context.lookup(sym) }
end