class Ruspea::Runtime::Env

Attributes

context[RW]
table[RW]

Public Class Methods

new(context = nil, table = nil) click to toggle source
# 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

Public Instance Methods

==(other) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/ruspea/runtime/env.rb, line 48
def define(sym, value)
  @table[sym] = value
end
eql?(other) click to toggle source
# File lib/ruspea/runtime/env.rb, line 73
def eql?(other)
  self == other
end
hash() click to toggle source
# File lib/ruspea/runtime/env.rb, line 82
def hash
  @table.hash + @context.hash + :rsp_env.hash
end
inspect() click to toggle source
# File lib/ruspea/runtime/env.rb, line 86
def inspect
  @table.map { |k, v|
    "#{k} => #{v}"
  }
end
lookup(sym) click to toggle source
# File lib/ruspea/runtime/env.rb, line 52
def lookup(sym)
  @table.fetch(sym) { @context.lookup(sym) }
end

Private Instance Methods

fn_define() click to toggle source
# File lib/ruspea/runtime/env.rb, line 98
def fn_define
  @fn_define ||= Lm.new(
    params: [Sym.new("sym"), Sym.new("val")],
    body: ->(env, _) {
      define(
        env.lookup(Sym.new("sym")),
        env.lookup(Sym.new("val"))
      )
    }
  )
end
fn_fetch() click to toggle source
# File lib/ruspea/runtime/env.rb, line 110
def fn_fetch
  @fn_fetch ||= Lm.new(
    params: [Sym.new("sym")],
    body: ->(env, _) {
      lookup env.lookup(Sym.new("sym"))
    }
  )
end