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



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

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

Instance Attribute Details

#engineLoxxy::BackEnd::Engine (readonly)



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

def engine
  @engine
end

#methsHash{String => LoxFunction} (readonly)

Returns the list of methods.

Returns:

  • (Hash{String => LoxFunction})

    the list of methods



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

def meths
  @meths
end

#nameString (readonly)

Returns The name of the class.

Returns:

  • (String)

    The name of the class



11
12
13
# File 'lib/loxxy/back_end/lox_class.rb', line 11

def name
  @name
end

#superclassObject (readonly)

Returns the value of attribute superclass.



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

def superclass
  @superclass
end

Instance Method Details

#!Datatype::False

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

Returns:



65
66
67
# File 'lib/loxxy/back_end/lox_class.rb', line 65

def !
  Datatype::False.instance
end

#accept(_visitor) ⇒ Object



32
33
34
# File 'lib/loxxy/back_end/lox_class.rb', line 32

def accept(_visitor)
  engine.expr_stack.push self
end

#arityObject



36
37
38
39
# File 'lib/loxxy/back_end/lox_class.rb', line 36

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

#call(engine, visitor) ⇒ Object



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

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

#find_method(aName) ⇒ Object

Parameters:

  • aName (String)

    the method name to search for



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

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



70
71
72
# File 'lib/loxxy/back_end/lox_class.rb', line 70

def to_str
  name
end