Class: Loxxy::BackEnd::LoxClass

Inherits:
Object
  • Object
show all
Defined in:
lib/loxxy/back_end/lox_class.rb

Overview

Runtime representation of a Lox class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aName, aSuperclass, theMethods, anEngine) ⇒ LoxClass

Create a class with given name

Parameters:

  • aName (String)

    The name of the class



24
25
26
27
28
29
30
31
32
# File 'lib/loxxy/back_end/lox_class.rb', line 24

def initialize(aName, aSuperclass, theMethods, anEngine)
  @name = aName.dup
  @superclass = aSuperclass
  @meths = {}
  theMethods.each do |func|
    meths[func.name] = func
  end
  @stack = anEngine.stack
end

Instance Attribute Details

#methsHash{String => LoxFunction} (readonly)

Returns the list of methods.

Returns:

  • (Hash{String => LoxFunction})

    the list of methods



17
18
19
# File 'lib/loxxy/back_end/lox_class.rb', line 17

def meths
  @meths
end

#nameString (readonly)

Returns The name of the class.

Returns:

  • (String)

    The name of the class



13
14
15
# File 'lib/loxxy/back_end/lox_class.rb', line 13

def name
  @name
end

#stackObject (readonly)

Returns the value of attribute stack.



18
19
20
# File 'lib/loxxy/back_end/lox_class.rb', line 18

def stack
  @stack
end

#superclassObject (readonly)

Returns the value of attribute superclass.



14
15
16
# File 'lib/loxxy/back_end/lox_class.rb', line 14

def superclass
  @superclass
end

Instance Method Details

#!Datatype::False

Logical negation. As a function is a truthy thing, its negation is thus false.

Returns:



67
68
69
# File 'lib/loxxy/back_end/lox_class.rb', line 67

def !
  Datatype::False.instance
end

#accept(_visitor) ⇒ Object



34
35
36
# File 'lib/loxxy/back_end/lox_class.rb', line 34

def accept(_visitor)
  stack.push self
end

#arityObject



38
39
40
41
# File 'lib/loxxy/back_end/lox_class.rb', line 38

def arity
  initializer = find_method('init')
  initializer ? initializer.arity : 0
end

#call(engine, visitor) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/loxxy/back_end/lox_class.rb', line 43

def call(engine, visitor)
  instance = LoxInstance.new(self, engine)
  initializer = find_method('init')
  if initializer
    constructor = initializer.bind(instance)
    constructor.call(engine, visitor)
  end

  engine.stack.push(instance)
end

#find_method(aName) ⇒ Object

Parameters:

  • aName (String)

    the method name to search for



55
56
57
58
59
60
61
62
# File 'lib/loxxy/back_end/lox_class.rb', line 55

def find_method(aName)
  found = meths[aName]
  unless found || superclass.nil?
    found = superclass.find_method(aName)
  end

  found
end

#to_strObject

Text representation of a Lox class



72
73
74
# File 'lib/loxxy/back_end/lox_class.rb', line 72

def to_str
  name
end