Class: Code::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/code/object.rb,
lib/code/object/list.rb,
lib/code/object/class.rb,
lib/code/object/range.rb,
lib/code/object/global.rb,
lib/code/object/number.rb,
lib/code/object/string.rb,
lib/code/object/boolean.rb,
lib/code/object/context.rb,
lib/code/object/decimal.rb,
lib/code/object/integer.rb,
lib/code/object/nothing.rb,
lib/code/object/argument.rb,
lib/code/object/function.rb,
lib/code/object/dictionary.rb,
lib/code/object/ruby_function.rb,
lib/code/object/identifier_list.rb

Defined Under Namespace

Classes: Argument, Boolean, Class, Context, Decimal, Dictionary, Function, Global, IdentifierList, Integer, List, Nothing, Number, Range, RubyFunction, String

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.maybeObject



5
6
7
# File 'lib/code/object.rb', line 5

def self.maybe
  Type::Maybe.new(self)
end

.nameObject



9
10
11
# File 'lib/code/object.rb', line 9

def self.name
  "Object"
end

.repeat(minimum = 0, maximum = nil) ⇒ Object



13
14
15
# File 'lib/code/object.rb', line 13

def self.repeat(minimum = 0, maximum = nil)
  Type::Repeat.new(self, minimum:, maximum:)
end

.|(other) ⇒ Object



17
18
19
# File 'lib/code/object.rb', line 17

def self.|(other)
  Type::Or.new(self, other)
end

Instance Method Details

#<=>(other) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/code/object.rb', line 21

def <=>(other)
  if respond_to?(:raw)
    raw <=> (other.respond_to?(:raw) ? other.raw : other)
  else
    other <=> self
  end
end

#==(other) ⇒ Object Also known as: eql?



29
30
31
32
33
34
35
# File 'lib/code/object.rb', line 29

def ==(other)
  if respond_to?(:raw)
    raw == (other.respond_to?(:raw) ? other.raw : other)
  else
    other == self
  end
end

#call(**args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/code/object.rb', line 38

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, [])
  value = arguments.first&.value

  case operator.to_s
  when "!", "not"
    sig(args)
    code_exclamation_point
  when "!=", "different"
    sig(args) { Object }
    code_different(value)
  when "&&", "and"
    sig(args) { Object }
    code_and_operator(value)
  when "+", "self"
    sig(args)
    code_self
  when "..", "inclusive_range"
    sig(args) { Object }
    code_inclusive_range(value)
  when "...", "exclusive_range"
    sig(args) { Object }
    code_exclusive_range(value)
  when "==", "equal"
    sig(args) { Object }
    code_equal_equal(value)
  when "===", "strict_equal"
    sig(args) { Object }
    code_equal_equal_equal(value)
  when "falsy?"
    sig(args)
    Boolean.new(falsy?)
  when "to_string"
    sig(args)
    code_to_string
  when "truthy?"
    sig(args)
    Boolean.new(truthy?)
  when "||", "or"
    sig(args) { Object }
    code_or_operator(value)
  when /=$/
    sig(args) { Object }

    if operator == "="
      context = args[:context]
      context.code_set(self, value)
    else
      context = args[:context].lookup!(self)
      context.code_set(
        self,
        context.code_fetch(self).call(
          **args,
          operator: operator[..-2],
          arguments: [Argument.new(value)]
        )
      )
    end

    context.code_fetch(self)
  else
    raise(
      Code::Error::Undefined,
      "#{operator} not defined on #{inspect}:#{self.class.name}"
    )
  end
end

#code_and_operator(other) ⇒ Object



107
108
109
# File 'lib/code/object.rb', line 107

def code_and_operator(other)
  truthy? ? other : self
end

#code_different(other) ⇒ Object



111
112
113
# File 'lib/code/object.rb', line 111

def code_different(other)
  Boolean.new(self != other)
end

#code_equal_equal(other) ⇒ Object



115
116
117
# File 'lib/code/object.rb', line 115

def code_equal_equal(other)
  Boolean.new(self == other)
end

#code_equal_equal_equal(other) ⇒ Object



139
140
141
# File 'lib/code/object.rb', line 139

def code_equal_equal_equal(other)
  Boolean.new(self === other)
end

#code_exclamation_pointObject



119
120
121
# File 'lib/code/object.rb', line 119

def code_exclamation_point
  truthy? ? Object::Boolean.new(false) : Object::Boolean.new(true)
end

#code_exclusive_range(value) ⇒ Object



123
124
125
# File 'lib/code/object.rb', line 123

def code_exclusive_range(value)
  Range.new(self, value, exclude_end: true)
end

#code_inclusive_range(value) ⇒ Object



127
128
129
# File 'lib/code/object.rb', line 127

def code_inclusive_range(value)
  Range.new(self, value, exclude_end: false)
end

#code_or_operator(other) ⇒ Object



131
132
133
# File 'lib/code/object.rb', line 131

def code_or_operator(other)
  truthy? ? self : other
end

#code_selfObject



135
136
137
# File 'lib/code/object.rb', line 135

def code_self
  self
end

#code_to_stringObject



143
144
145
# File 'lib/code/object.rb', line 143

def code_to_string
  String.new(to_s)
end

#falsy?Boolean

Returns:



147
148
149
# File 'lib/code/object.rb', line 147

def falsy?
  !truthy?
end

#hashObject

Raises:

  • (NotImplementedError)


151
152
153
154
155
# File 'lib/code/object.rb', line 151

def hash
  raise NotImplementedError, "#{self.class.name}#hash" unless respond_to?(:raw)

  [self.class, raw].hash
end

#multi_fetch(hash, *keys) ⇒ Object



157
158
159
# File 'lib/code/object.rb', line 157

def multi_fetch(hash, *keys)
  keys.map { |key| [key, hash.fetch(key)] }.to_h
end

#sig(args, &block) ⇒ Object



161
162
163
# File 'lib/code/object.rb', line 161

def sig(args, &block)
  Type::Sig.sig(args, object: self, &block)
end

#to_sObject

Raises:

  • (NotImplementedError)


165
166
167
# File 'lib/code/object.rb', line 165

def to_s
  raise NotImplementedError, "#{self.class.name}#to_s"
end

#truthy?Boolean

Returns:



169
170
171
# File 'lib/code/object.rb', line 169

def truthy?
  true
end