Class: Shuwar::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/shuwar/runtime.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRuntime

Returns a new instance of Runtime.



7
8
9
10
11
12
# File 'lib/shuwar/runtime.rb', line 7

def initialize
  @value_table = {}
  @marco_table = {}
  @loaded_libs = []
  load_lib :base
end

Instance Attribute Details

#marco_tableObject

Returns the value of attribute marco_table.



5
6
7
# File 'lib/shuwar/runtime.rb', line 5

def marco_table
  @marco_table
end

#value_tableObject

Returns the value of attribute value_table.



5
6
7
# File 'lib/shuwar/runtime.rb', line 5

def value_table
  @value_table
end

Instance Method Details

#call_marco(key, *args) ⇒ Object



55
56
57
# File 'lib/shuwar/runtime.rb', line 55

def call_marco(key, *args)
  self.instance_exec *args, &@marco_table[key]
end

#dupObject



14
15
16
17
18
19
# File 'lib/shuwar/runtime.rb', line 14

def dup
  a = super
  a.value_table = a.value_table.dup
  a.marco_table = a.marco_table.dup
  a
end

#evaluate(x) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/shuwar/runtime.rb', line 63

def evaluate(x)
  case x
    when Array
      if x.empty?
        nil
      elsif has_marco? x[0]
        call_marco *x
      else
        lambda{|f,*as| self.instance_exec *as, &f}.call *x.map{|a| evaluate a }
      end
    when Symbol
      get_value x
    else x
  end
end

#get_value(key) ⇒ Object



34
35
36
37
38
# File 'lib/shuwar/runtime.rb', line 34

def get_value(key)
  raise "Why reference a non-symbol?" unless key
  raise "No value for #{key}" unless has_value? key
  @value_table[key]
end

#has_marco?(key) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/shuwar/runtime.rb', line 59

def has_marco?(key)
  @marco_table.has_key? key
end

#has_value?(key) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/shuwar/runtime.rb', line 40

def has_value?(key)
  @value_table.has_key? key
end

#load_lib(name) ⇒ Object



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

def load_lib(name)
  return if @loaded_libs.include? name
  @loaded_libs.push name
  vs, ms = Shuwar::Stdlib.load name
  @value_table.merge! vs
  @marco_table.merge! ms
end

#new_func(key, &block) ⇒ Object



44
45
46
47
# File 'lib/shuwar/runtime.rb', line 44

def new_func(key, &block)
  raise "There's already a function #{key}" if @value_table.has_key? key
  set_value(key, block)
end

#new_marco(key, &block) ⇒ Object



49
50
51
52
53
# File 'lib/shuwar/runtime.rb', line 49

def new_marco(key, &block)
  raise "There's already a marco #{key}" if @marco_table.has_key? key
  raise "Why set value to a non-symbol?" unless key.is_a? Symbol
  @marco_table[key] = block
end

#set_value(key, val) ⇒ Object



29
30
31
32
# File 'lib/shuwar/runtime.rb', line 29

def set_value(key, val)
  raise "Why set value to a non-symbol?" unless key.is_a? Symbol
  @value_table[key] = val
end