Class: RubyLisp::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/rubylisp/environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(outer: nil, namespace: nil, is_namespace: false, out_env: nil) ⇒ Environment

Returns a new instance of Environment.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rubylisp/environment.rb', line 7

def initialize(outer: nil, namespace: nil, is_namespace: false, out_env: nil)
  @vars = {'*ns*' => (outer or self)}
  @outer = outer
  @namespace = namespace or
               (outer.namespace if outer) or
               "__ns_#{rand 10000}"
  @is_namespace = is_namespace
  @out_env = if out_env
               out_env
             else
               find_namespace
             end
end

Instance Attribute Details

#is_namespaceObject

Returns the value of attribute is_namespace.



5
6
7
# File 'lib/rubylisp/environment.rb', line 5

def is_namespace
  @is_namespace
end

#namespaceObject

Returns the value of attribute namespace.



5
6
7
# File 'lib/rubylisp/environment.rb', line 5

def namespace
  @namespace
end

#out_envObject

Returns the value of attribute out_env.



5
6
7
# File 'lib/rubylisp/environment.rb', line 5

def out_env
  @out_env
end

#outerObject

Returns the value of attribute outer.



5
6
7
# File 'lib/rubylisp/environment.rb', line 5

def outer
  @outer
end

#varsObject

Returns the value of attribute vars.



5
6
7
# File 'lib/rubylisp/environment.rb', line 5

def vars
  @vars
end

Instance Method Details

#find(key) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/rubylisp/environment.rb', line 33

def find key
  if @vars.member? key
    self
  elsif @outer
    @outer.find key
  else
    nil
  end
end

#find_namespaceObject



21
22
23
24
25
26
27
# File 'lib/rubylisp/environment.rb', line 21

def find_namespace
  env = self
  while !env.is_namespace && !env.outer.nil?
    env = env.outer
  end
  env
end

#get(key) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/rubylisp/environment.rb', line 43

def get key
  env = find key
  if env
    env.vars[key]
  else
    raise RuntimeError, "Unable to resolve symbol: #{key}"
  end
end

#load_rbl_file(path) ⇒ Object

TODO: some notion of “required namespaces” whose vars can be accessed when qualified with the namespace name



68
69
70
71
72
73
74
75
# File 'lib/rubylisp/environment.rb', line 68

def load_rbl_file path
  root = File.expand_path '../..', File.dirname(__FILE__)
  input = File.read "#{root}/#{path}"

  namespace = Environment.new
  Parser.parse input, namespace
  namespace
end

#refer(other_env) ⇒ Object

Copies all vars from other_env to this one.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubylisp/environment.rb', line 53

def refer other_env
  @vars = @vars.merge other_env.vars do |key, oldval, newval|
    if key == '*ns*'
      oldval
    else
      puts "WARNING: #{namespace}/#{key} being replaced by " +
           "#{other_env.namespace}/#{key}"
      newval
    end
  end
end

#replObject



83
84
85
86
87
# File 'lib/rubylisp/environment.rb', line 83

def repl
  namespace = load_rbl_file 'rubylisp/repl.rbl'
  refer namespace
  self
end

#set(key, val) ⇒ Object



29
30
31
# File 'lib/rubylisp/environment.rb', line 29

def set key, val
  @vars[key] = val
end

#stdlibObject



77
78
79
80
81
# File 'lib/rubylisp/environment.rb', line 77

def stdlib
  namespace = load_rbl_file 'rubylisp/core.rbl'
  refer namespace
  self
end