Class: Loxxy::Datatype::BuiltinDatatype

Inherits:
Object
  • Object
show all
Defined in:
lib/loxxy/datatype/builtin_datatype.rb

Overview

Abstract class that generalizes a value from a Lox built-in data types. An instance acts merely as a wrapper around a Ruby representation of the value.

Direct Known Subclasses

Boolean, LXString, Nil, Number

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aValue) ⇒ BuiltinDatatype

Constructor. Initialize a Lox value from one of its built-in data type.



13
14
15
# File 'lib/loxxy/datatype/builtin_datatype.rb', line 13

def initialize(aValue)
  @value = validated_value(aValue)
end

Instance Attribute Details

#valueObject (readonly)

Returns The Ruby representation.

Returns:

  • (Object)

    The Ruby representation



10
11
12
# File 'lib/loxxy/datatype/builtin_datatype.rb', line 10

def value
  @value
end

Instance Method Details

#!Datatype::Boolean

Negation ('not') Returns a boolean with opposite truthiness value.

Returns:



41
42
43
# File 'lib/loxxy/datatype/builtin_datatype.rb', line 41

def !
  falsey? ? True.instance : False.instance
end

#!=(other) ⇒ Datatype::Boolean

Check for inequality of this object with another Lox object

Parameters:

Returns:



34
35
36
# File 'lib/loxxy/datatype/builtin_datatype.rb', line 34

def !=(other)
  !(self == other)
end

#accept(visitor) ⇒ Object

Part of the 'visitee' role in Visitor design pattern.

Parameters:



67
68
69
# File 'lib/loxxy/datatype/builtin_datatype.rb', line 67

def accept(visitor)
  visitor.visit_builtin(self)
end

#and(operand2) ⇒ Object

Returns the first falsey argument (if any), otherwise returns the last truthy argument.

Parameters:



48
49
50
# File 'lib/loxxy/datatype/builtin_datatype.rb', line 48

def and(operand2)
  falsey? ? self : logical_2nd_arg(operand2)
end

#falsey?Boolean

Is the value considered falsey in Lox? Rule: false and nil are falsey and everything else is truthy. This test used in conditional statements (i.e. if, while)

Returns:



20
21
22
# File 'lib/loxxy/datatype/builtin_datatype.rb', line 20

def falsey?
  false # Default implementation
end

#or(operand2) ⇒ Object

Returns the first truthy argument (if any), otherwise returns the last falsey argument.

Parameters:



55
56
57
# File 'lib/loxxy/datatype/builtin_datatype.rb', line 55

def or(operand2)
  truthy? ? self : logical_2nd_arg(operand2)
end

#to_strString

Method called from Lox to obtain the text representation of the boolean.

Returns:

  • (String)


61
62
63
# File 'lib/loxxy/datatype/builtin_datatype.rb', line 61

def to_str
  value.to_s # Default implementation...
end

#truthy?Boolean

Is the value considered truthy in Lox? Rule: false and nil are falsey and everything else is truthy. This test used in conditional statements (i.e. if, while)

Returns:



27
28
29
# File 'lib/loxxy/datatype/builtin_datatype.rb', line 27

def truthy?
  true # Default implementation
end