Class: RubyLisp::Environment
- Inherits:
-
Object
- Object
- RubyLisp::Environment
- Defined in:
- lib/rubylisp/environment.rb
Instance Attribute Summary collapse
-
#is_namespace ⇒ Object
Returns the value of attribute is_namespace.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#out_env ⇒ Object
Returns the value of attribute out_env.
-
#outer ⇒ Object
Returns the value of attribute outer.
-
#vars ⇒ Object
Returns the value of attribute vars.
Instance Method Summary collapse
- #find(key) ⇒ Object
- #find_namespace ⇒ Object
- #get(key) ⇒ Object
-
#initialize(outer: nil, namespace: nil, is_namespace: false, out_env: nil) ⇒ Environment
constructor
A new instance of Environment.
-
#load_rbl_file(path) ⇒ Object
TODO: some notion of “required namespaces” whose vars can be accessed when qualified with the namespace name.
-
#refer(other_env) ⇒ Object
Copies all vars from
other_envto this one. - #repl ⇒ Object
- #set(key, val) ⇒ Object
- #stdlib ⇒ Object
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_namespace ⇒ Object
Returns the value of attribute is_namespace.
5 6 7 |
# File 'lib/rubylisp/environment.rb', line 5 def is_namespace @is_namespace end |
#namespace ⇒ Object
Returns the value of attribute namespace.
5 6 7 |
# File 'lib/rubylisp/environment.rb', line 5 def namespace @namespace end |
#out_env ⇒ Object
Returns the value of attribute out_env.
5 6 7 |
# File 'lib/rubylisp/environment.rb', line 5 def out_env @out_env end |
#outer ⇒ Object
Returns the value of attribute outer.
5 6 7 |
# File 'lib/rubylisp/environment.rb', line 5 def outer @outer end |
#vars ⇒ Object
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_namespace ⇒ Object
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. '../..', 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 |
#repl ⇒ Object
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 |
#stdlib ⇒ Object
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 |