Class: JSE::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/jse/env.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent: nil) ⇒ Env

Returns a new instance of Env.



3
4
5
6
7
# File 'lib/jse/env.rb', line 3

def initialize(parent: nil)
  @parent = parent
  @bindings = {}
  @current_meta = {}
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



9
10
11
# File 'lib/jse/env.rb', line 9

def parent
  @parent
end

Instance Method Details

#clear_metaObject



56
57
58
# File 'lib/jse/env.rb', line 56

def clear_meta
  @current_meta = {}
end

#eval(node) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/jse/env.rb', line 40

def eval(node)
  if node.respond_to?(:apply)
    node.apply(self)
  else
    node
  end
end

#exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/jse/env.rb', line 30

def exists?(name)
  return true if @bindings.key?(name)
  return @parent.exists?(name) if @parent
  false
end

#get_metaObject



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

def get_meta
  @current_meta
end

#load(mod) ⇒ Object



36
37
38
# File 'lib/jse/env.rb', line 36

def load(mod)
  mod.each { |name, functor| register(name, functor) }
end

#register(name, value) ⇒ Object



19
20
21
22
23
24
# File 'lib/jse/env.rb', line 19

def register(name, value)
  if @bindings.key?(name)
    raise "Symbol '#{name}' already exists in current scope"
  end
  @bindings[name] = value
end

#resolve(name) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/jse/env.rb', line 11

def resolve(name)
  if @bindings.key?(name)
    @bindings[name]
  elsif @parent
    @parent.resolve(name)
  end
end

#set(name, value) ⇒ Object



26
27
28
# File 'lib/jse/env.rb', line 26

def set(name, value)
  @bindings[name] = value
end

#set_meta(dict) ⇒ Object



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

def set_meta(dict)
  @current_meta = dict
end