Class: Code::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/code/object.rb,
lib/code/object/date.rb,
lib/code/object/list.rb,
lib/code/object/time.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/duration.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, Date, Decimal, Dictionary, Duration, Function, Global, IdentifierList, Integer, List, Nothing, Number, Range, RubyFunction, String, Time

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(**args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
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
# File 'lib/code/object.rb', line 25

def self.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}:#{name}"
    )
  end
end

.code_and_operator(other) ⇒ Object



94
95
96
# File 'lib/code/object.rb', line 94

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

.code_different(other) ⇒ Object



98
99
100
# File 'lib/code/object.rb', line 98

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

.code_equal_equal(other) ⇒ Object



102
103
104
# File 'lib/code/object.rb', line 102

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

.code_equal_equal_equal(other) ⇒ Object



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

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

.code_exclamation_pointObject



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

def self.code_exclamation_point
  Boolean.new(falsy?)
end

.code_exclusive_range(value) ⇒ Object



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

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

.code_inclusive_range(value) ⇒ Object



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

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

.code_or_operator(other) ⇒ Object



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

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

.code_selfObject



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

def self.code_self
  self
end

.code_to_stringObject



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

def self.code_to_string
  String.new(to_s)
end

.falsy?Boolean

Returns:



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

def self.falsy?
  !truthy?
end

.hashObject



138
139
140
141
142
143
144
# File 'lib/code/object.rb', line 138

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

  [self.class, raw].hash
end

.maybeObject



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

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

.multi_fetch(hash, *keys) ⇒ Object



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

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

.nameObject

Raises:

  • (NotImplementedError)


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

.sig(args, &block) ⇒ Object



21
22
23
# File 'lib/code/object.rb', line 21

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

.to_sObject



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

def self.to_s
  name
end

.truthy?Boolean

Returns:



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

def self.truthy?
  true
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



166
167
168
169
170
171
172
# File 'lib/code/object.rb', line 166

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?



174
175
176
177
178
179
180
# File 'lib/code/object.rb', line 174

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

#call(**args) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/code/object.rb', line 183

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



252
253
254
# File 'lib/code/object.rb', line 252

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

#code_different(other) ⇒ Object



256
257
258
# File 'lib/code/object.rb', line 256

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

#code_equal_equal(other) ⇒ Object



260
261
262
# File 'lib/code/object.rb', line 260

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

#code_equal_equal_equal(other) ⇒ Object



284
285
286
# File 'lib/code/object.rb', line 284

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

#code_exclamation_pointObject



264
265
266
# File 'lib/code/object.rb', line 264

def code_exclamation_point
  Boolean.new(falsy?)
end

#code_exclusive_range(value) ⇒ Object



268
269
270
# File 'lib/code/object.rb', line 268

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

#code_inclusive_range(value) ⇒ Object



272
273
274
# File 'lib/code/object.rb', line 272

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

#code_or_operator(other) ⇒ Object



276
277
278
# File 'lib/code/object.rb', line 276

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

#code_selfObject



280
281
282
# File 'lib/code/object.rb', line 280

def code_self
  self
end

#code_to_stringObject



288
289
290
# File 'lib/code/object.rb', line 288

def code_to_string
  String.new(to_s)
end

#falsy?Boolean

Returns:



292
293
294
# File 'lib/code/object.rb', line 292

def falsy?
  !truthy?
end

#hashObject



296
297
298
299
300
301
302
# File 'lib/code/object.rb', line 296

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

  [self.class, raw].hash
end

#multi_fetch(hash, *keys) ⇒ Object



304
305
306
# File 'lib/code/object.rb', line 304

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

#sig(args, &block) ⇒ Object



308
309
310
# File 'lib/code/object.rb', line 308

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

#to_sObject

Raises:

  • (NotImplementedError)


312
313
314
# File 'lib/code/object.rb', line 312

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

#truthy?Boolean

Returns:



316
317
318
# File 'lib/code/object.rb', line 316

def truthy?
  true
end