code-ruby 1.1.1 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +13 -13
  3. data/.github/workflows/ci.yml +25 -24
  4. data/.npm-version +1 -0
  5. data/.rubocop.yml +14 -11
  6. data/Gemfile +5 -4
  7. data/Gemfile.lock +134 -28
  8. data/VERSION +1 -1
  9. data/bin/console +6 -0
  10. data/bin/dorian +31 -0
  11. data/code-ruby.gemspec +6 -1
  12. data/lib/code/error.rb +4 -25
  13. data/lib/code/node/code.rb +1 -1
  14. data/lib/code/node/function_parameter.rb +10 -8
  15. data/lib/code/node/while.rb +1 -1
  16. data/lib/code/object/boolean.rb +20 -16
  17. data/lib/code/object/class.rb +10 -4
  18. data/lib/code/object/code.rb +7 -3
  19. data/lib/code/object/context.rb +8 -8
  20. data/lib/code/object/date.rb +41 -7
  21. data/lib/code/object/decimal.rb +101 -56
  22. data/lib/code/object/dictionary.rb +245 -191
  23. data/lib/code/object/duration.rb +11 -7
  24. data/lib/code/object/function.rb +38 -25
  25. data/lib/code/object/global.rb +95 -42
  26. data/lib/code/object/html.rb +12 -14
  27. data/lib/code/object/http.rb +219 -0
  28. data/lib/code/object/identifier_list.rb +16 -16
  29. data/lib/code/object/integer.rb +129 -89
  30. data/lib/code/object/json.rb +18 -22
  31. data/lib/code/object/list.rb +141 -92
  32. data/lib/code/object/parameter.rb +9 -13
  33. data/lib/code/object/range.rb +77 -45
  34. data/lib/code/object/string.rb +15 -34
  35. data/lib/code/object/time.rb +17 -16
  36. data/lib/code/object.rb +126 -93
  37. data/lib/code/parser/string.rb +2 -1
  38. data/lib/code/type/sig.rb +3 -3
  39. data/lib/code-ruby.rb +119 -0
  40. data/package-lock.json +1 -1
  41. data/package.json +1 -1
  42. data/spec/code/object/http_spec.rb +91 -0
  43. data/spec/code/type_spec.rb +1 -1
  44. data/spec/code_spec.rb +10 -5
  45. data/spec/spec_helper.rb +18 -0
  46. metadata +50 -3
@@ -4,11 +4,15 @@ class Code
4
4
  class Object
5
5
  class Code < Object
6
6
  def initialize(*args, **_kargs, &)
7
- raw = args.first.presence || Nothing.new
8
- @raw = raw.is_a?(Node) ? raw : Node::Code.new(::Code.parse(raw.to_s))
7
+ @raw =
8
+ if args.first.is_a?(Node)
9
+ args.first
10
+ else
11
+ Node::Code.new(::Code.parse(args.first.to_s))
12
+ end
9
13
  end
10
14
 
11
- def evaluate(...)
15
+ def code_evaluate(...)
12
16
  raw.evaluate(...)
13
17
  end
14
18
  end
@@ -6,19 +6,19 @@ class Code
6
6
  attr_reader :parent
7
7
 
8
8
  def initialize(*args, **_kargs, &)
9
- raw = args.first || Dictionary.new
10
- raw = raw.raw if raw.is_a?(Object)
11
- @raw = raw.to_h
12
- @parent = Context.new(args.second) if args.second
9
+ super(args.first)
10
+ @parent = args.second if args.second.is_a?(Dictionary)
13
11
  end
14
12
 
15
- def lookup!(identifier)
16
- if code_has_key?(identifier).truthy?
13
+ def code_lookup!(identifier)
14
+ code_identifier = identifier.to_code
15
+
16
+ if code_has_key?(code_identifier).truthy?
17
17
  self
18
18
  elsif parent?
19
- parent.lookup!(identifier)
19
+ parent.code_lookup!(code_identifier)
20
20
  else
21
- raise Error::Undefined, "#{identifier} is not defined"
21
+ raise Error, "#{code_identifier} is not defined"
22
22
  end
23
23
  end
24
24
 
@@ -4,16 +4,15 @@ class Code
4
4
  class Object
5
5
  class Date < Object
6
6
  def initialize(*args, **_kargs, &)
7
- raw = args.map(&:to_s).join("-").presence || ::Date.current.to_s
8
- @raw = ::Date.parse(raw)
7
+ @raw = ::Date.parse(args.map(&:to_s).join("-"))
9
8
  rescue ::Date::Error
10
- raise Error, "#{raw.inspect} is an invalid date"
9
+ @raw = ::Date.current
11
10
  end
12
11
 
13
12
  def self.call(**args)
14
- operator = args.fetch(:operator, nil)
13
+ code_operator = args.fetch(:operator, nil).to_code
15
14
 
16
- case operator.to_s
15
+ case code_operator.to_s
17
16
  when "tomorrow"
18
17
  sig(args)
19
18
  code_tomorrow
@@ -59,12 +58,27 @@ class Code
59
58
  end
60
59
 
61
60
  def call(**args)
62
- operator = args.fetch(:operator, nil)
61
+ code_operator = args.fetch(:operator, nil).to_code
63
62
 
64
- case operator.to_s
63
+ case code_operator.to_s
65
64
  when "hour"
66
65
  sig(args)
67
66
  code_hour
67
+ when "hours"
68
+ sig(args)
69
+ code_hours
70
+ when "minute"
71
+ sig(args)
72
+ code_minute
73
+ when "minutes"
74
+ sig(args)
75
+ code_minutes
76
+ when "second"
77
+ sig(args)
78
+ code_second
79
+ when "seconds"
80
+ sig(args)
81
+ code_seconds
68
82
  else
69
83
  super
70
84
  end
@@ -73,6 +87,26 @@ class Code
73
87
  def code_hour
74
88
  Integer.new(0)
75
89
  end
90
+
91
+ def code_hours
92
+ Integer.new(0)
93
+ end
94
+
95
+ def code_minute
96
+ Integer.new(0)
97
+ end
98
+
99
+ def code_minutes
100
+ Integer.new(0)
101
+ end
102
+
103
+ def code_second
104
+ Integer.new(0)
105
+ end
106
+
107
+ def code_seconds
108
+ Integer.new(0)
109
+ end
76
110
  end
77
111
  end
78
112
  end
@@ -4,72 +4,77 @@ class Code
4
4
  class Object
5
5
  class Decimal < Object
6
6
  def initialize(*args, **_kargs, &)
7
- decimal = args.first || "0"
8
- exponent = args.second || "0"
9
- decimal = decimal.raw if decimal.is_an?(Object)
10
- exponent = exponent.raw if exponent.is_an?(Object)
11
- @raw = decimal.to_d * (10**exponent.to_d)
7
+ @raw =
8
+ if args.first.class.in?(NUMBER_CLASSES)
9
+ if args.second.class.in?(NUMBER_CLASSES)
10
+ args.first.to_s.to_d * (10**args.second.to_s.to_d)
11
+ else
12
+ args.first.to_s.to_d
13
+ end
14
+ else
15
+ 0.to_d
16
+ end
12
17
  rescue FloatDomainError
13
- raise Error, "#{decimal.inspect} * 10**#{exponent.inspect} is invalid"
18
+ @raw = 0.to_d
14
19
  end
15
20
 
16
21
  def call(**args)
17
- operator = args.fetch(:operator, nil)
18
- arguments = args.fetch(:arguments, List.new)
19
- value = arguments.code_first
22
+ code_operator = args.fetch(:operator, nil).to_code
23
+ code_arguments = args.fetch(:arguments, []).to_code
24
+ code_value = code_arguments.code_first
20
25
 
21
- case operator.to_s
26
+ case code_operator.to_s
22
27
  when "%", "modulo"
23
28
  sig(args) { Integer | Decimal }
24
- code_modulo(value)
29
+ code_modulo(code_value)
25
30
  when "&", "bitwise_and"
26
31
  sig(args) { Integer | Decimal }
27
- code_bitwise_and(value)
32
+ code_bitwise_and(code_value)
28
33
  when "*", "multiplication"
29
34
  sig(args) { Integer | Decimal }
30
- code_multiplication(value)
35
+ code_multiplication(code_value)
31
36
  when "**", "power"
32
37
  sig(args) { Integer | Decimal }
33
- code_power(value)
38
+ code_power(code_value)
34
39
  when "+", "plus"
35
40
  sig(args) { Object.maybe }
36
- arguments.any? ? code_plus(value) : self
41
+ code_arguments.any? ? code_plus(code_value) : self
37
42
  when "-", "minus"
38
43
  sig(args) { (Integer | Decimal).maybe }
39
- arguments.any? ? code_minus(value) : code_unary_minus
44
+ code_arguments.any? ? code_minus(code_value) : code_unary_minus
40
45
  when "/", "division"
41
46
  sig(args) { Integer | Decimal }
42
- code_division(value)
47
+ code_division(code_value)
43
48
  when "<", "inferior"
44
49
  sig(args) { Integer | Decimal }
45
- code_inferior(value)
50
+ code_inferior(code_value)
46
51
  when "<<", "left_shift"
47
52
  sig(args) { Integer | Decimal }
48
- code_left_shift(value)
53
+ code_left_shift(code_value)
49
54
  when "<=", "inferior_or_equal"
50
55
  sig(args) { Integer | Decimal }
51
- code_inferior_or_equal(value)
56
+ code_inferior_or_equal(code_value)
52
57
  when "<=>", "compare"
53
58
  sig(args) { Integer | Decimal }
54
- code_compare(value)
59
+ code_compare(code_value)
55
60
  when ">", "superior"
56
61
  sig(args) { Integer | Decimal }
57
- code_superior(value)
62
+ code_superior(code_value)
58
63
  when ">=", "superior_or_equal"
59
64
  sig(args) { Integer | Decimal }
60
- code_superior_or_equal(value)
65
+ code_superior_or_equal(code_value)
61
66
  when ">>", "right_shift"
62
67
  sig(args) { Integer | Decimal }
63
- code_right_shift(value)
68
+ code_right_shift(code_value)
64
69
  when "^", "bitwise_xor"
65
70
  sig(args) { Integer | Decimal }
66
- code_bitwise_xor(value)
71
+ code_bitwise_xor(code_value)
67
72
  when "abs"
68
73
  sig(args)
69
74
  code_abs
70
75
  when "ceil"
71
76
  sig(args) { Integer.maybe }
72
- code_ceil(value)
77
+ code_ceil(code_value)
73
78
  when "clone"
74
79
  sig(args)
75
80
  code_clone
@@ -81,7 +86,7 @@ class Code
81
86
  code_five?
82
87
  when "floor"
83
88
  sig(args) { Integer.maybe }
84
- code_floor(value)
89
+ code_floor(code_value)
85
90
  when "four?"
86
91
  sig(args)
87
92
  code_four?
@@ -93,7 +98,7 @@ class Code
93
98
  code_one?
94
99
  when "round"
95
100
  sig(args) { Integer.maybe }
96
- code_round(value)
101
+ code_round(code_value)
97
102
  when "seven?"
98
103
  sig(args)
99
104
  code_seven?
@@ -120,7 +125,7 @@ class Code
120
125
  code_to_string
121
126
  when "truncate"
122
127
  sig(args) { Integer.maybe }
123
- code_truncate(value)
128
+ code_truncate(code_value)
124
129
  when "two?"
125
130
  sig(args)
126
131
  code_two?
@@ -129,7 +134,7 @@ class Code
129
134
  code_zero?
130
135
  when "|", "bitwise_or"
131
136
  sig(args) { Integer | Decimal }
132
- code_bitwise_or(value)
137
+ code_bitwise_or(code_value)
133
138
  else
134
139
  super
135
140
  end
@@ -140,20 +145,28 @@ class Code
140
145
  end
141
146
 
142
147
  def code_bitwise_and(other)
143
- Integer.new(raw.to_i & other.raw.to_i)
148
+ code_other = other.to_code
149
+
150
+ Integer.new(raw.to_i & code_other.raw.to_i)
144
151
  end
145
152
 
146
153
  def code_bitwise_or(other)
147
- Integer.new(raw.to_i | other.raw.to_i)
154
+ code_other = other.to_code
155
+
156
+ Integer.new(raw.to_i | code_other.raw.to_i)
148
157
  end
149
158
 
150
159
  def code_bitwise_xor(other)
151
- Integer.new(raw.to_i ^ other.raw.to_i)
160
+ code_other = other.to_code
161
+
162
+ Integer.new(raw.to_i ^ code_other.raw.to_i)
152
163
  end
153
164
 
154
165
  def code_ceil(n = nil)
155
- n = Integer.new(0) if n.nil? || n.is_a?(Nothing)
156
- Decimal.new(raw.ceil(n.raw))
166
+ code_n = n.to_code
167
+ code_n = Integer.new(0) if code_n.nothing?
168
+
169
+ Decimal.new(raw.ceil(code_n.raw))
157
170
  end
158
171
 
159
172
  def code_clone
@@ -161,11 +174,15 @@ class Code
161
174
  end
162
175
 
163
176
  def code_compare(other)
164
- Integer.new(raw <=> other.raw)
177
+ code_other = other.to_code
178
+
179
+ Integer.new(raw <=> code_other.raw)
165
180
  end
166
181
 
167
182
  def code_division(other)
168
- Decimal.new(raw / other.raw)
183
+ code_other = other.to_code
184
+
185
+ Decimal.new(raw / code_other.raw)
169
186
  end
170
187
 
171
188
  def code_eight?
@@ -177,8 +194,10 @@ class Code
177
194
  end
178
195
 
179
196
  def code_floor(n = nil)
180
- n = Integer.new(0) if n.nil? || n.is_a?(Nothing)
181
- Decimal.new(raw.floor(n.raw))
197
+ code_n = n.to_code
198
+ code_n = Integer.new(0) if code_n.nothing?
199
+
200
+ Decimal.new(raw.floor(code_n.raw))
182
201
  end
183
202
 
184
203
  def code_four?
@@ -186,27 +205,39 @@ class Code
186
205
  end
187
206
 
188
207
  def code_inferior(other)
189
- Boolean.new(raw < other.raw)
208
+ code_other = other.to_code
209
+
210
+ Boolean.new(raw < code_other.raw)
190
211
  end
191
212
 
192
213
  def code_inferior_or_equal(other)
193
- Boolean.new(raw <= other.raw)
214
+ code_other = other.to_code
215
+
216
+ Boolean.new(raw <= code_other.raw)
194
217
  end
195
218
 
196
219
  def code_left_shift(other)
197
- Integer.new(raw.to_i << other.raw.to_i)
220
+ code_other = other.to_code
221
+
222
+ Integer.new(raw.to_i << code_other.raw.to_i)
198
223
  end
199
224
 
200
225
  def code_minus(other)
201
- Decimal.new(raw - other.raw)
226
+ code_other = other.to_code
227
+
228
+ Decimal.new(raw - code_other.raw)
202
229
  end
203
230
 
204
231
  def code_modulo(other)
205
- Decimal.new(raw % other.raw)
232
+ code_other = other.to_code
233
+
234
+ Decimal.new(raw % code_other.raw)
206
235
  end
207
236
 
208
237
  def code_multiplication(other)
209
- Decimal.new(raw * other.raw)
238
+ code_other = other.to_code
239
+
240
+ Decimal.new(raw * code_other.raw)
210
241
  end
211
242
 
212
243
  def code_nine?
@@ -218,24 +249,32 @@ class Code
218
249
  end
219
250
 
220
251
  def code_plus(other)
221
- if other.is_an?(Integer) || other.is_a?(Decimal)
222
- Decimal.new(raw + other.raw)
252
+ code_other = other.to_code
253
+
254
+ if code_other.is_an?(Integer) || other.is_a?(Decimal)
255
+ Decimal.new(raw + code_other.raw)
223
256
  else
224
- String.new(to_s + other.to_s)
257
+ String.new(to_s + code_other.to_s)
225
258
  end
226
259
  end
227
260
 
228
261
  def code_power(other)
229
- Decimal.new(raw**other.raw)
262
+ code_other = other.to_code
263
+
264
+ Decimal.new(raw**code_other.raw)
230
265
  end
231
266
 
232
267
  def code_right_shift(other)
233
- Integer.new(raw.to_i >> other.raw.to_i)
268
+ code_other = other.to_code
269
+
270
+ Integer.new(raw.to_i >> code_other.raw.to_i)
234
271
  end
235
272
 
236
273
  def code_round(n = nil)
237
- n = Integer.new(0) if n.nil? || n.is_a?(Nothing)
238
- Decimal.new(raw.round(n.raw))
274
+ code_n = n.to_code
275
+ code_n = Integer.new(0) if code_n.nothing?
276
+
277
+ Decimal.new(raw.round(code_n.raw))
239
278
  end
240
279
 
241
280
  def code_seven?
@@ -251,11 +290,15 @@ class Code
251
290
  end
252
291
 
253
292
  def code_superior(other)
254
- Boolean.new(raw > other.raw)
293
+ code_other = other.to_code
294
+
295
+ Boolean.new(raw > code_other.raw)
255
296
  end
256
297
 
257
298
  def code_superior_or_equal(other)
258
- Boolean.new(raw >= other.raw)
299
+ code_other = other.to_code
300
+
301
+ Boolean.new(raw >= code_other.raw)
259
302
  end
260
303
 
261
304
  def code_ten?
@@ -279,8 +322,10 @@ class Code
279
322
  end
280
323
 
281
324
  def code_truncate(n = nil)
282
- n = Integer.new(0) if n.nil? || n.is_a?(Nothing)
283
- Decimal.new(raw.truncate(n.raw))
325
+ code_n = n.to_code
326
+ code_n = Integer.new(0) if code_n.nothing?
327
+
328
+ Decimal.new(raw.truncate(code_n.raw))
284
329
  end
285
330
 
286
331
  def code_two?