Class: EleetScript::EleetScriptClass

Inherits:
EleetScriptClassSkeleton show all
Defined in:
lib/lang/runtime/class.rb

Direct Known Subclasses

EleetScriptArray

Instance Attribute Summary collapse

Attributes inherited from EleetScriptClassSkeleton

#memory, #ruby_value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EleetScriptClassSkeleton

#class?, #eql?, #hash, #instance?, #is_a?, set_is_class, set_is_instance

Constructor Details

#initialize(namespace, super_class = nil) ⇒ EleetScriptClass

Returns a new instance of EleetScriptClass.



20
21
22
23
24
25
26
27
28
29
# File 'lib/lang/runtime/class.rb', line 20

def initialize(namespace, super_class = nil)
  @methods = MethodHash.new
  @class_vars = ProcessedKeyHash.new
  @class_vars.set_key_preprocessor do |key|
    key[0..1] == '@@' ? key[2..-1] : key
  end
  @context = namespace.new_class_context(self, self)
  @super_class = super_class
  @ruby_value = self
end

Instance Attribute Details

#class_varsObject

Returns the value of attribute class_vars.



8
9
10
# File 'lib/lang/runtime/class.rb', line 8

def class_vars
  @class_vars
end

#contextObject

Returns the value of attribute context.



8
9
10
# File 'lib/lang/runtime/class.rb', line 8

def context
  @context
end

#methodsObject

Returns the value of attribute methods.



8
9
10
# File 'lib/lang/runtime/class.rb', line 8

def methods
  @methods
end

#nameObject Also known as: class_name

Returns the value of attribute name.



8
9
10
# File 'lib/lang/runtime/class.rb', line 8

def name
  @name
end

#super_classObject (readonly)

Returns the value of attribute super_class.



9
10
11
# File 'lib/lang/runtime/class.rb', line 9

def super_class
  @super_class
end

Class Method Details

.create(namespace, name, super_class = nil) ⇒ Object



13
14
15
16
17
# File 'lib/lang/runtime/class.rb', line 13

def create(namespace, name, super_class = nil)
  cls = EleetScriptClass.new(namespace, super_class)
  cls.name = name
  cls
end

Instance Method Details

#call(method_name, arguments = []) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lang/runtime/class.rb', line 32

def call(method_name, arguments = [])
  method = lookup(method_name.to_s)
  if method
    if method.arity == 3
      method.call(self, arguments, @context)
    else
      method.call(self, arguments)
    end
  else
    es_method_name = @context['String'].new_with_value(method_name.to_s, @context.namespace_context)
    call(NO_METHOD, arguments.dup.unshift(es_method_name))
  end
end

#class_def(method_name, es_block = nil, &block) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/lang/runtime/class.rb', line 76

def class_def(method_name, es_block = nil, &block)
  method_name = "@@#{method_name}"
  if block_given?
    @methods[method_name] = block
  else
    @methods[method_name] = es_method
  end
end

#def(method_name, es_block = nil, &block) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/lang/runtime/class.rb', line 67

def def(method_name, es_block = nil, &block)
  method_name = method_name.to_s
  if block_given?
    @methods[method_name] = block
  else
    @methods[method_name] = es_method
  end
end

#es_responds_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/lang/runtime/class.rb', line 95

def es_responds_to?(method_name)
  !lookup(method_name.to_s).nil?
end

#inspectObject



103
104
105
# File 'lib/lang/runtime/class.rb', line 103

def inspect
  to_s[0..-2] + " @methods(#{@methods.keys.join(', ')})>"
end

#instance_lookup(method_name) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/lang/runtime/class.rb', line 55

def instance_lookup(method_name)
  method = @methods[method_name]
  if method.nil? && has_super_class?
    return super_class.instance_lookup(method_name)
  end
  method
end

#lookup(method_name) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/lang/runtime/class.rb', line 46

def lookup(method_name)
  method_name = method_name[0..1] == '@@' ? method_name : "@@#{method_name}"
  method = @methods[method_name]
  if method.nil? && has_super_class?
    return super_class.lookup(method_name)
  end
  method
end

#new(current_context) ⇒ Object



85
86
87
# File 'lib/lang/runtime/class.rb', line 85

def new(current_context)
  EleetScriptClassInstance.new(@context, self, current_context.namespace_context)
end

#new_with_value(value, current_context) ⇒ Object



89
90
91
92
93
# File 'lib/lang/runtime/class.rb', line 89

def new_with_value(value, current_context)
  EleetScriptClassInstance.new(@context, self, current_context.namespace_context).tap do |instance|
    instance.ruby_value = value
  end
end

#to_sObject



99
100
101
# File 'lib/lang/runtime/class.rb', line 99

def to_s
  "<EleetScriptClass \"#{name || 'Unnamed'}\">"
end