Class: Trxl::Environment
- Inherits:
-
Object
- Object
- Trxl::Environment
- Defined in:
- lib/trxl/trxl.rb
Instance Method Summary collapse
- #[](variable) ⇒ Object
-
#[]=(variable, value) ⇒ Object
FIXME find out why map definition doesn’t work.
- #add_library(name) ⇒ Object
- #depth ⇒ Object
- #empty? ⇒ Boolean
- #enter_scope ⇒ Object
- #exit_scope ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize(local_env = {}) ⇒ Environment
constructor
called when parsing starts.
- #libraries ⇒ Object
- #library_included?(name) ⇒ Boolean
- #local ⇒ Object
- #merge(other_env) ⇒ Object
- #merge!(other_env) ⇒ Object
- #select(&block) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(local_env = {}) ⇒ Environment
called when parsing starts
22 23 24 |
# File 'lib/trxl/trxl.rb', line 22 def initialize(local_env = {}) @stack = [ local_env ] end |
Instance Method Details
#[](variable) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/trxl/trxl.rb', line 52 def [](variable) var = variable.to_sym @stack.each do |env| if env.has_key?(var) return env[var] end end nil end |
#[]=(variable, value) ⇒ Object
FIXME find out why map definition doesn’t work
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/trxl/trxl.rb', line 63 def []=(variable, value) var = variable.to_sym # search all scopes @stack.each do |env| if env.has_key?(var) return env[var] = value end end # not found, assign it in local scope local[var] = value end |
#add_library(name) ⇒ Object
95 96 97 |
# File 'lib/trxl/trxl.rb', line 95 def add_library(name) (@libraries || []) << name.to_sym end |
#depth ⇒ Object
39 40 41 |
# File 'lib/trxl/trxl.rb', line 39 def depth @stack.size end |
#empty? ⇒ Boolean
81 82 83 |
# File 'lib/trxl/trxl.rb', line 81 def empty? @stack.size == 1 ? peek.empty? : @stack.all? { |h| h.empty? } end |
#enter_scope ⇒ Object
26 27 28 |
# File 'lib/trxl/trxl.rb', line 26 def enter_scope push #peek.dup end |
#exit_scope ⇒ Object
30 31 32 |
# File 'lib/trxl/trxl.rb', line 30 def exit_scope pop end |
#has_key?(key) ⇒ Boolean
85 86 87 |
# File 'lib/trxl/trxl.rb', line 85 def has_key?(key) @stack.any? { |env| env.has_key?(key.to_sym) } end |
#libraries ⇒ Object
103 104 105 |
# File 'lib/trxl/trxl.rb', line 103 def libraries @libraries.dup # don't allow modifications outside end |
#library_included?(name) ⇒ Boolean
99 100 101 |
# File 'lib/trxl/trxl.rb', line 99 def library_included?(name) @libraries ? @libraries.include?(name.to_sym) : false end |
#local ⇒ Object
34 35 36 |
# File 'lib/trxl/trxl.rb', line 34 def local peek end |
#merge(other_env) ⇒ Object
43 44 45 |
# File 'lib/trxl/trxl.rb', line 43 def merge(other_env) self.class.new(local.merge(other_env)) end |
#merge!(other_env) ⇒ Object
47 48 49 |
# File 'lib/trxl/trxl.rb', line 47 def merge!(other_env) local.merge!(other_env); self end |
#select(&block) ⇒ Object
89 90 91 92 93 |
# File 'lib/trxl/trxl.rb', line 89 def select(&block) @stack.inject([]) do |memo, env| memo << env.select(&block) end[0] end |
#to_s ⇒ Object
75 76 77 78 79 |
# File 'lib/trxl/trxl.rb', line 75 def to_s @stack.inject([]) do |stack, env| stack << env.inspect end.join(',') end |