Class: Ikra::AST::ClassDefNode

Inherits:
Node show all
Defined in:
lib/ast/nodes.rb,
lib/ast/printer.rb,
lib/ast/visitor.rb,
lib/types/inference/ast_inference.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#parent

Instance Method Summary collapse

Methods inherited from Node

#eql?, #hash

Constructor Details

#initialize(name:, ruby_class:, instance_variables: [], instance_methods: [], class_variables: [], class_methods: []) ⇒ ClassDefNode

Class variables/methods are defined as instance variables/methods on the singleton class ClassDefNode



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ast/nodes.rb', line 53

def initialize(
        name:, 
        ruby_class:, 
        instance_variables: [], 
        instance_methods: [], 
        class_variables: [], 
        class_methods: [])
    @name = name
    @ruby_class = ruby_class
    @instance_variables = instance_variables
    @instance_methods = instance_methods
end

Instance Attribute Details

#instance_methodsObject (readonly)

Returns the value of attribute instance_methods.



47
48
49
# File 'lib/ast/nodes.rb', line 47

def instance_methods
  @instance_methods
end

#instance_variablesObject (readonly)

Returns the value of attribute instance_variables.



46
47
48
# File 'lib/ast/nodes.rb', line 46

def instance_variables
  @instance_variables
end

#nameObject (readonly)

Returns the value of attribute name.



45
46
47
# File 'lib/ast/nodes.rb', line 45

def name
  @name
end

#ruby_classObject (readonly)

Returns the value of attribute ruby_class.



48
49
50
# File 'lib/ast/nodes.rb', line 48

def ruby_class
  @ruby_class
end

Instance Method Details

#==(other) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/ast/nodes.rb', line 100

def ==(other)
    return super(other) && 
        name == other.name &&
        ruby_class == other.ruby_class &&
        instance_variables == other.instance_variables &&
        instance_methods == other.instance_methods &&
        class_variables == other.class_variables &&
        class_methods == other.class_methods
end

#accept(visitor) ⇒ Object



20
21
22
# File 'lib/ast/visitor.rb', line 20

def accept(visitor)
    return visitor.visit_class_def_node(self)
end

#add_instance_method(inst_meth) ⇒ Object



81
82
83
84
# File 'lib/ast/nodes.rb', line 81

def add_instance_method(inst_meth)
    instance_methods.push(inst_meth)
    inst_meth.parent = self
end

#add_instance_variable(inst_var) ⇒ Object



76
77
78
79
# File 'lib/ast/nodes.rb', line 76

def add_instance_variable(inst_var)
    instance_variables.push(inst_var)
    inst_meth.parent = self
end

#cloneObject



66
67
68
69
70
71
72
73
74
# File 'lib/ast/nodes.rb', line 66

def clone
    return ClassDefNode.new(
        name: @name,
        ruby_class: @ruby_class,
        instance_variables: @instance_variables.map do |i| i.clone end,
        instance_methods: @instance_methods.map do |i| i.clone end,
        class_variables: @class_variables.map do |c| c.clone end,
        class_methods: @class_methods.map do |c| c.clone end)
end

#enclosing_classObject



96
97
98
# File 'lib/ast/nodes.rb', line 96

def enclosing_class
    return self
end

#get_typeObject



42
43
44
# File 'lib/types/inference/ast_inference.rb', line 42

def get_type
    return ruby_class.to_ikra_type
end

#has_instance_method?(selector) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/ast/nodes.rb', line 86

def has_instance_method?(selector)
    return instance_method(selector) != nil
end

#instance_method(selector) ⇒ Object



90
91
92
93
94
# File 'lib/ast/nodes.rb', line 90

def instance_method(selector)
    return instance_methods.find do |meth|
        meth.name == selector
    end
end

#to_sObject



16
17
18
# File 'lib/ast/printer.rb', line 16

def to_s
    return "[ClassDefNode: #{name}; #{instance_variables.to_s}; #{instance_methods.to_s}]"
end