Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 97b012da58
Fetching contributors…

Cannot retrieve contributors at this time

file 74 lines (61 sloc) 1.532 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
module WLang
  class Scope

    attr_reader :subject
    attr_reader :parent

    def initialize(subject, parent)
      @subject, @parent = subject, parent
    end

    def self.null
      @null ||= NullScope.new
    end

    def self.coerce(arg, parent = nil)
      return arg if Scope===arg && parent.nil?
      clazz = case arg
        when Binding then BindingScope
        when Scope then ProxyScope
        when Proc then ProcScope
        else
          ObjectScope
      end
      clazz.new(arg, parent)
    end

    def self.chain(scopes)
      scopes.compact.inject(nil){|parent,child|
        Scope.coerce(child, parent)
      }
    end

    def root
      parent.nil? ? self : parent.root
    end

    def push(x)
      Scope.coerce(x, self)
    end

    def pop
      @parent
    end

    def with(x)
      yield(self.push(x))
    end

    def evaluate(expr, *default)
      unfound = lambda{ default.empty? ? throw(:fail) : default.first }
      expr = expr.to_s.strip
      if expr.to_s.index('.').nil?
        fetch(expr.to_sym, &unfound)
      else
        keys = expr.split('.').map(&:to_sym)
        keys.inject(self){|scope,key|
          Scope.coerce(scope.fetch(key, &unfound))
        }.subject
      end
    end

    protected

      def safe_parent
        parent || Scope.null
      end

  end # class Scope
end # module WLang
require 'wlang/scope/null_scope'
require 'wlang/scope/proxy_scope'
require 'wlang/scope/object_scope'
require 'wlang/scope/binding_scope'
require 'wlang/scope/proc_scope'
Something went wrong with that request. Please try again.