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
# File 'lib/lang/runtime/class.rb', line 32

def call(method_name, arguments = [])
  method = lookup(method_name.to_s)
  call_method(method_name, method, arguments)
end

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



83
84
85
86
87
88
89
90
# File 'lib/lang/runtime/class.rb', line 83

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



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

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)


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

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

#inspectObject



110
111
112
# File 'lib/lang/runtime/class.rb', line 110

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

#instance_lookup(method_name) ⇒ Object



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

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



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

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



92
93
94
# File 'lib/lang/runtime/class.rb', line 92

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

#new_with_value(value, current_context) ⇒ Object



96
97
98
99
100
# File 'lib/lang/runtime/class.rb', line 96

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

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lang/runtime/class.rb', line 54

def super_call(method_name, arguments = [])
  if super_class == self
    @context['Errors'].call(
      :<,
      @context['String'].new_with_value(
        "Cannot call super implmentation for #{method_name} if there is no super class",
        @context
      )
    )
    @context['nil']
  else
    method = super_class.lookup(method_name)
    call_method(method_name, method, arguments)
  end
end

#to_sObject



106
107
108
# File 'lib/lang/runtime/class.rb', line 106

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