class Shuwar::Runtime

Attributes

marco_table[RW]
value_table[RW]

Public Class Methods

new() click to toggle source
# File lib/shuwar/runtime.rb, line 7
def initialize
  @value_table = {}
  @marco_table = {}
  @loaded_libs = []
  load_lib :base
end

Public Instance Methods

call_marco(key, *args) click to toggle source
# File lib/shuwar/runtime.rb, line 55
def call_marco(key, *args)
  self.instance_exec *args, &@marco_table[key]
end
dup() click to toggle source
Calls superclass method
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/shuwar/runtime.rb, line 59
def has_marco?(key)
  @marco_table.has_key? key
end
has_value?(key) click to toggle source
# File lib/shuwar/runtime.rb, line 40
def has_value?(key)
  @value_table.has_key? key
end
load_lib(name) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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