Class: RubyLisp::Symbol

Inherits:
Value
  • Object
show all
Defined in:
lib/rubylisp/types.rb

Instance Attribute Summary

Attributes inherited from Value

#value

Instance Method Summary collapse

Methods inherited from Value

#initialize

Constructor Details

This class inherits a constructor from RubyLisp::Value

Instance Method Details

#==(other) ⇒ Object



80
81
82
# File 'lib/rubylisp/types.rb', line 80

def ==(other)
  other.is_a?(Symbol) && @value == other.value
end

#resolve(env) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rubylisp/types.rb', line 84

def resolve(env)
  # rbl: (.+ 1 2)
  # ruby: 1.+(2)
  instance_method = /^\.(.*)/.match(@value).to_a[1]
  if instance_method
    return lambda {|obj, *args| obj.send instance_method, *args}
  end

  # rbl: (File::open "/tmp/foo")
  # ruby: File::open("/tmp/foo")  OR  File.open("/tmp/foo")
  #
  # rbl: Foo::Bar::BAZ
  # ruby: Foo::Bar::BAZ
  if /^\w+(::\w+)+$/ =~ @value
    first_segment, *segments = @value.split('::')
    first_segment = Object.const_get first_segment
    return segments.reduce(first_segment) do |result, segment|
      if result.is_a? Proc
        # a method can only be in the final position
        raise RuntimeError, "Invalid value: #{@value}"
      elsif /^[A-Z]/ =~ segment
        # get module/class constant
        result.const_get segment
      else
        # if module/class method doesn't exist, trigger a NoMethodError
        result.send segment unless result.respond_to? segment
        # call module/class method
        lambda {|*args| result.send segment, *args }
      end
    end
  end

  env.get @value
end

#to_sObject



76
77
78
# File 'lib/rubylisp/types.rb', line 76

def to_s
  @value
end