code-ruby 0.11.0 → 0.13.0

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 (49) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -0
  3. data/Gemfile.lock +5 -3
  4. data/Rakefile +5 -0
  5. data/bin/code +6 -0
  6. data/lib/code/node/base_10.rb +2 -2
  7. data/lib/code/node/base_16.rb +1 -5
  8. data/lib/code/node/base_2.rb +1 -5
  9. data/lib/code/node/base_8.rb +1 -5
  10. data/lib/code/node/call.rb +4 -4
  11. data/lib/code/node/code.rb +2 -1
  12. data/lib/code/node/decimal.rb +1 -4
  13. data/lib/code/node/dictionary.rb +6 -2
  14. data/lib/code/node/function.rb +3 -3
  15. data/lib/code/node/function_parameter.rb +5 -1
  16. data/lib/code/node/if.rb +2 -1
  17. data/lib/code/node/list.rb +2 -1
  18. data/lib/code/node/statement.rb +23 -23
  19. data/lib/code/node/string.rb +2 -1
  20. data/lib/code/object/argument.rb +4 -3
  21. data/lib/code/object/boolean.rb +4 -24
  22. data/lib/code/object/class.rb +3 -19
  23. data/lib/code/object/code.rb +16 -0
  24. data/lib/code/object/context.rb +6 -9
  25. data/lib/code/object/date.rb +25 -17
  26. data/lib/code/object/decimal.rb +30 -38
  27. data/lib/code/object/dictionary.rb +6 -21
  28. data/lib/code/object/duration.rb +7 -21
  29. data/lib/code/object/function.rb +29 -45
  30. data/lib/code/object/global.rb +56 -40
  31. data/lib/code/object/identifier_list.rb +0 -4
  32. data/lib/code/object/integer.rb +23 -46
  33. data/lib/code/object/json.rb +29 -0
  34. data/lib/code/object/list.rb +4 -21
  35. data/lib/code/object/nothing.rb +2 -20
  36. data/lib/code/object/parameter.rb +51 -0
  37. data/lib/code/object/range.rb +12 -27
  38. data/lib/code/object/string.rb +6 -27
  39. data/lib/code/object/time.rb +14 -24
  40. data/lib/code/object.rb +155 -15
  41. data/lib/code/parser.rb +1 -1
  42. data/lib/code/type.rb +10 -2
  43. data/lib/code/version.rb +1 -1
  44. data/lib/code-ruby.rb +6 -0
  45. data/lib/code.rb +8 -5
  46. data/spec/code/object/dictionary_spec.rb +0 -2
  47. data/spec/code_spec.rb +141 -29
  48. metadata +6 -3
  49. data/lib/code/object/number.rb +0 -11
data/spec/code_spec.rb CHANGED
@@ -3,6 +3,71 @@
3
3
  require "spec_helper"
4
4
 
5
5
  RSpec.describe Code do
6
+
7
+ %w[
8
+ 1.day.ago
9
+ 1.day.from_now
10
+ 1.hour.ago
11
+ 1.hour.from_now
12
+ 2.days.ago
13
+ 2.days.from_now
14
+ 2.hours.ago
15
+ 2.hours.from_now
16
+ Boolean.new
17
+ Boolean.new(true)
18
+ Boolean.new(false)
19
+ Class.new
20
+ Class.new(Boolean)
21
+ Class.new(Class)
22
+ Context.new
23
+ Context.new(a:1)
24
+ Date.new
25
+ Date.new("2024-03-05")
26
+ Date.today
27
+ Date.yesterday
28
+ Date.tomorrow
29
+ Decimal.new
30
+ Decimal.new(0)
31
+ Decimal.new(1.2)
32
+ Dictionary.new
33
+ Dictionary.new(a:1)
34
+ Duration.new
35
+ Duration.new(1.day)
36
+ Duration.new("P1D")
37
+ Function.new
38
+ Integer.new
39
+ Integer.new(0)
40
+ Integer.new(1)
41
+ Integer.new(1.2)
42
+ List.new
43
+ List.new([])
44
+ List.new([1,2])
45
+ Nothing.new
46
+ Nothing.new(1)
47
+ Object.new
48
+ Object.new(1)
49
+ Range.new
50
+ Range.new(1,2)
51
+ Range.new(-1)
52
+ Range.new(1,2,exclude_end:false)
53
+ Range.new(1,2,exclude_end:true)
54
+ String.new
55
+ String.new(:hello)
56
+ Time.new
57
+ Time.new("2024-03-05.06:10:59.UTC")
58
+ Time.now
59
+ Time.tomorrow
60
+ Time.yesterday
61
+ Time.tomorrow
62
+ Code.new
63
+ Parameter.new
64
+ Argument.new
65
+ Argument.new(1)
66
+ Argument.new(1,name:"index")
67
+ IdentifierList.new
68
+ IdentifierList.new([])
69
+ ].each { |input| it(input) { Code.evaluate(input) } }
70
+
6
71
  [
7
72
  [
8
73
  "user = { name: :Dorian, age: 31 } user.delete_if(String) user.keys",
@@ -31,13 +96,65 @@ RSpec.describe Code do
31
96
  %w[0x10 16],
32
97
  %w[1.0e1 10.0],
33
98
  %w[1.1 1.1],
99
+ %w[1.day.ago.after? false],
100
+ %w[1.day.ago.after?(1.day.from_now) false],
101
+ %w[1.day.ago.before? true],
102
+ %w[1.day.ago.before?(1.day.from_now) true],
103
+ %w[1.day.from_now.after? true],
104
+ %w[1.day.from_now.after?(1.day.ago) true],
105
+ %w[1.day.from_now.before? false],
106
+ %w[1.day.from_now.before?(1.day.ago) false],
34
107
  %w[10e1.0 100.0],
35
108
  %w[1e1 10],
109
+ %w[2.days.ago.future? false],
110
+ %w[2.days.ago.past? true],
111
+ %w[2.days.from_now.future? true],
112
+ %w[2.days.from_now.past? false],
36
113
  %w[9975×14÷8 17456.25],
37
114
  %w[:Hello.include?(:H) true],
38
115
  %w[:admin? :admin?],
39
116
  %w[:hello :hello],
40
117
  %w[:update!.to_string :update!],
118
+ %w[Boolean(1) true],
119
+ %w[Boolean(2.days.ago) true],
120
+ %w[Boolean(2.days.ago) true],
121
+ %w[Boolean(false) false],
122
+ %w[Boolean(nothing) false],
123
+ %w[Boolean(true) true],
124
+ %w[Boolean({}) true],
125
+ %w[Boolean.new false],
126
+ %w[Boolean.new(1) true],
127
+ %w[Boolean.new(2.days.ago) true],
128
+ %w[Boolean.new(2.days.ago) true],
129
+ %w[Boolean.new(false) false],
130
+ %w[Boolean.new(nothing) false],
131
+ %w[Boolean.new(true) true],
132
+ %w[Boolean.new({}) true],
133
+ %w[Class(2.days.ago) Time],
134
+ %w[Class(Boolean) Boolean],
135
+ %w[Class(Time) Time],
136
+ %w[Class(nothing) Nothing],
137
+ %w[Class(true) Boolean],
138
+ %w[Class.new Nothing],
139
+ %w[Class.new Nothing],
140
+ %w[Class.new(2.days.ago) Time],
141
+ %w[Class.new(Boolean) Boolean],
142
+ %w[Class.new(Date) Date],
143
+ %w[Class.new(Time) Time],
144
+ %w[Class.new(nothing) Nothing],
145
+ %w[Class.new(true) Boolean],
146
+ %w[Date Date],
147
+ %w[Date("0001-01-01").to_string :0001-01-01],
148
+ %w[Date("2024-03-02").to_string :2024-03-02],
149
+ %w[Decimal(1) 1.0],
150
+ %w[Decimal(1) 1],
151
+ %w[Decimal(:1) 1.0],
152
+ %w[Decimal(:1) 1],
153
+ %w[Decimal.new 0],
154
+ %w[Decimal.new(1) 1.0],
155
+ %w[Decimal.new(1) 1],
156
+ %w[Decimal.new(:1) 1.0],
157
+ %w[Decimal.new(:1) 1],
41
158
  %w[false false],
42
159
  %w[true true],
43
160
  %w[{} {}],
@@ -57,14 +174,6 @@ RSpec.describe Code do
57
174
  ["1 >> 1", "0"],
58
175
  ["1 ^ 2", "3"],
59
176
  ["1 | 2", "3"],
60
- %w[1.day.ago.after?(1.day.from_now) false],
61
- %w[1.day.ago.before?(1.day.from_now) true],
62
- %w[1.day.from_now.after?(1.day.ago) true],
63
- %w[1.day.from_now.before?(1.day.ago) false],
64
- %w[1.day.ago.after? false],
65
- %w[1.day.ago.before? true],
66
- %w[1.day.from_now.after? true],
67
- %w[1.day.from_now.before? false],
68
177
  ["2 * 3 + 2", "8"],
69
178
  ["2 * 3", "6"],
70
179
  ["2 ** 3 ** 2", "512"],
@@ -73,6 +182,20 @@ RSpec.describe Code do
73
182
  ["2 / 4", "0.5"],
74
183
  ["3.times {}", "3"],
75
184
  ["4 % 3", "1"],
185
+ ["Boolean([])", "true"],
186
+ ["Boolean(true, false)", "true"],
187
+ ["Boolean.new([])", "true"],
188
+ ["Boolean.new(false, true)", "false"],
189
+ ["Boolean.new(true, false)", "true"],
190
+ ["Class(true, 1)", "Boolean"],
191
+ ["Class.new(Boolean, Time)", "Boolean"],
192
+ ["Class.new(Time, Boolean)", "Time"],
193
+ ["Date(2024, 3, 2).to_string", ":2024-03-02"],
194
+ ["Date(2024,3, 2).to_string", ":2024-03-02"],
195
+ ["Decimal(1, :2)", "100"],
196
+ ["Decimal(:1, 2)", "100.0"],
197
+ ["Decimal.new(1, :2)", "100"],
198
+ ["Decimal.new(:1, 2)", "100.0"],
76
199
  ["[,,].include?(4)", "false"],
77
200
  ["[,].include?(4)", "false"],
78
201
  ["[1, 2, 3, \n ].include?(4)", "false"],
@@ -80,6 +203,8 @@ RSpec.describe Code do
80
203
  ["[1, 2, 3]", "[1, 2, 3]"],
81
204
  ["[1, 2, 3].include?(2)", "true"],
82
205
  ["[1, 2, 3].include?(4)", "false"],
206
+ ["[1, 2, 3].map { |i| next if i == 2 i ** 2}", "[1, nothing, 9]"],
207
+ ["[1, 2, 3].map { |i| next(0) if i.even? i ** 2}", "[1, 0, 9]"],
83
208
  ["[1, 2, 3].select { |n| n.even? }", "[2]"],
84
209
  ["[1, 2, 3].select { |n| n.even? }.select { |n| n.odd? }", "[]"],
85
210
  ["[1, 2].map(&:to_string)", "[:1, :2]"],
@@ -89,6 +214,8 @@ RSpec.describe Code do
89
214
  ["[[true]]", "[[true]]"],
90
215
  ["[]", "[]"],
91
216
  ["\r\n", "nothing"],
217
+ ["a = 0 [1, 2, 3].each { |i| next if i == 2 a += i } a", "4"],
218
+ ["a = 0 loop a += 1 break end a", "1"],
92
219
  ["a = 0\nuntil a > 10 a += 1 end a", "11"],
93
220
  ["a = 0\nwhile a < 10 a += 1 end a", "10"],
94
221
  ["a = 1 3.times { a += 1 } a", "4"],
@@ -142,14 +269,12 @@ RSpec.describe Code do
142
269
  ['user = {} user.name = "Dorian" user.name', ":Dorian"],
143
270
  ['user = {} user[:name] = "Dorian" user[:name]', ":Dorian"],
144
271
  ['{ "first_name": "Dorian" }', '{"first_name" => "Dorian"}'],
145
- ["a = 0 loop a += 1 break end a", "1"],
146
- ["a = 0 [1, 2, 3].each { |i| next if i == 2 a += i } a", "4"],
147
- ["[1, 2, 3].map { |i| next if i == 2 i ** 2}", "[1, nothing, 9]"],
148
- ["[1, 2, 3].map { |i| next(0) if i.even? i ** 2}", "[1, 0, 9]"],
149
- %w[2.days.ago.past? true],
150
- %w[2.days.from_now.past? false],
151
- %w[2.days.ago.future? false],
152
- %w[2.days.from_now.future? true]
272
+ ['{ "first_name": "Dorian" }.as_json', '{"first_name" => "Dorian"}'],
273
+ ["nothing.to_json", ":null"],
274
+ ["1.to_json", ":1"],
275
+ ["1.0.to_json", ":1"],
276
+ ["1.1.to_json", %{'"1.1"'}],
277
+ ["", ""]
153
278
  ].each do |input, expected|
154
279
  it "#{input} == #{expected}" do
155
280
  expect(Code.evaluate(input)).to eq(Code.evaluate(expected))
@@ -159,19 +284,6 @@ RSpec.describe Code do
159
284
  end
160
285
  end
161
286
 
162
- %w[
163
- 1.day.ago
164
- 1.day.from_now
165
- 1.hour.ago
166
- 1.hour.from_now
167
- 2.days.ago
168
- 2.days.from_now
169
- 2.hours.ago
170
- 2.hours.from_now
171
- Date.tomorrow
172
- Time.tomorrow
173
- ].each { |input| it(input) { Code.evaluate(input) } }
174
-
175
287
  [["puts(true)", "true\n"], %w[print(false) false]].each do |input, expected|
176
288
  it "#{input} prints #{expected}" do
177
289
  output = StringIO.new
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-02 00:00:00.000000000 Z
11
+ date: 2024-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -94,6 +94,7 @@ files:
94
94
  - Gemfile
95
95
  - Gemfile.lock
96
96
  - README.md
97
+ - Rakefile
97
98
  - bin/code
98
99
  - bin/console
99
100
  - code-ruby.gemspec
@@ -132,6 +133,7 @@ files:
132
133
  - lib/code/object/argument.rb
133
134
  - lib/code/object/boolean.rb
134
135
  - lib/code/object/class.rb
136
+ - lib/code/object/code.rb
135
137
  - lib/code/object/context.rb
136
138
  - lib/code/object/date.rb
137
139
  - lib/code/object/decimal.rb
@@ -141,9 +143,10 @@ files:
141
143
  - lib/code/object/global.rb
142
144
  - lib/code/object/identifier_list.rb
143
145
  - lib/code/object/integer.rb
146
+ - lib/code/object/json.rb
144
147
  - lib/code/object/list.rb
145
148
  - lib/code/object/nothing.rb
146
- - lib/code/object/number.rb
149
+ - lib/code/object/parameter.rb
147
150
  - lib/code/object/range.rb
148
151
  - lib/code/object/string.rb
149
152
  - lib/code/object/time.rb
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Code
4
- class Object
5
- class Number < Object
6
- def self.name
7
- "Number"
8
- end
9
- end
10
- end
11
- end