code-ruby 1.3.0 → 1.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e36d2ab574e54db93b35370cc0dec2021a518e595ae05d38a4775b39e23a663d
4
- data.tar.gz: 34f7b0587badd6faffbde82b8ebb85ae1bbec7ad1254b20de18c3f6bf6745d30
3
+ metadata.gz: 8d61185fef6ddda1ee1ec0f449c77dad74283cf8ae4d127f36cd02bfc4aa59df
4
+ data.tar.gz: cb77def47d007482cc37adc20a74cf1cc2f651886292b8f55d2ee0821ef1856d
5
5
  SHA512:
6
- metadata.gz: 669c6eeeea9097a0b9606571739580ed78bd57fe146c5b59495bb648f04acd919329d8837eebdc1211d46d8a1a2b6c1f5fc57035c854590239933f7d9079271a
7
- data.tar.gz: 42321fcb6e6b3201e44d3a270752c43362cfca512668d7f937384a91aa880ffa68d2836a3dcdb2736b9263419f53b4fb756e37bb48f5b6a66be8bd3cd5b1f0be
6
+ metadata.gz: fef5749131d1c4f02af24ae234091b7c3b3edea2b31e68ea7dd7a9be2e2ebd05c6633db2c75e8f047c30e2446b325d8a4f1b20923d13c37883c5cf1afcf4d9e6
7
+ data.tar.gz: '09c79b2ef656bf3da9fb7033a7b228fd54b85b04693aba7fcda2292388a139550d4022794bfd4f16e02ca105b55b3a8c5dc6eba51d1fce04731851cc0eea4762'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-ruby (1.3.0)
4
+ code-ruby (1.4.0)
5
5
  activesupport
6
6
  base64
7
7
  bigdecimal
@@ -17,7 +17,7 @@ PATH
17
17
  GEM
18
18
  remote: https://rubygems.org/
19
19
  specs:
20
- activesupport (8.0.1)
20
+ activesupport (8.0.2)
21
21
  base64
22
22
  benchmark (>= 0.3)
23
23
  bigdecimal
@@ -32,23 +32,23 @@ GEM
32
32
  uri (>= 0.13.1)
33
33
  addressable (2.8.7)
34
34
  public_suffix (>= 2.0.2, < 7.0)
35
- ast (2.4.2)
35
+ ast (2.4.3)
36
36
  base64 (0.2.0)
37
37
  benchmark (0.4.0)
38
- bigdecimal (3.1.8)
38
+ bigdecimal (3.1.9)
39
39
  bundler-audit (0.9.2)
40
40
  bundler (>= 1.2.0, < 3)
41
41
  thor (~> 1.0)
42
42
  cmdparse (3.0.7)
43
- concurrent-ruby (1.3.4)
44
- connection_pool (2.4.1)
43
+ concurrent-ruby (1.3.5)
44
+ connection_pool (2.5.0)
45
45
  crack (1.0.0)
46
46
  bigdecimal
47
47
  rexml
48
- csv (3.3.2)
48
+ csv (3.3.3)
49
49
  did-you-mean (0.1.1)
50
50
  levenshtein (>= 0.2.0)
51
- diff-lcs (1.5.1)
51
+ diff-lcs (1.6.1)
52
52
  dorian (2.6.3)
53
53
  csv
54
54
  dorian-arguments
@@ -76,74 +76,78 @@ GEM
76
76
  dorian-to_struct (2.0.2)
77
77
  drb (2.2.1)
78
78
  geom2d (0.4.1)
79
- git (2.3.3)
79
+ git (3.0.0)
80
80
  activesupport (>= 5.0)
81
81
  addressable (~> 2.8)
82
- process_executer (~> 1.1)
83
- rchardet (~> 1.8)
82
+ process_executer (~> 1.3)
83
+ rchardet (~> 1.9)
84
84
  haml (6.3.0)
85
85
  temple (>= 0.8.2)
86
86
  thor
87
87
  tilt
88
88
  hashdiff (1.1.2)
89
- hexapdf (1.0.3)
89
+ hexapdf (1.2.0)
90
90
  cmdparse (~> 3.0, >= 3.0.3)
91
91
  geom2d (~> 0.4, >= 0.4.1)
92
92
  openssl (>= 2.2.1)
93
- i18n (1.14.6)
93
+ strscan (>= 3.1.2)
94
+ i18n (1.14.7)
94
95
  concurrent-ruby (~> 1.0)
95
- json (2.9.1)
96
+ json (2.10.2)
96
97
  language-ruby (1.1.2)
97
98
  dorian-arguments
98
99
  zeitwerk
99
- language_server-protocol (3.17.0.3)
100
+ language_server-protocol (3.17.0.4)
100
101
  levenshtein (0.2.2)
101
- libv8-node (18.19.0.0)
102
- libv8-node (18.19.0.0-arm64-darwin)
103
- libv8-node (18.19.0.0-x86_64-linux)
104
- logger (1.6.4)
102
+ libv8-node (23.6.1.0)
103
+ libv8-node (23.6.1.0-arm64-darwin)
104
+ libv8-node (23.6.1.0-x86_64-linux)
105
+ lint_roller (1.1.0)
106
+ logger (1.7.0)
105
107
  mini_portile2 (2.8.8)
106
- mini_racer (0.16.0)
107
- libv8-node (~> 18.19.0.0)
108
- minitest (5.25.4)
108
+ mini_racer (0.18.0)
109
+ libv8-node (~> 23.6.1.0)
110
+ minitest (5.25.5)
109
111
  mustermann (3.0.3)
110
112
  ruby2_keywords (~> 0.0.1)
111
113
  net-http (0.6.0)
112
114
  uri
113
- nokogiri (1.18.1)
115
+ nokogiri (1.18.6)
114
116
  mini_portile2 (~> 2.8.2)
115
117
  racc (~> 1.4)
116
- nokogiri (1.18.1-arm64-darwin)
118
+ nokogiri (1.18.6-arm64-darwin)
117
119
  racc (~> 1.4)
118
- nokogiri (1.18.1-x86_64-linux-gnu)
120
+ nokogiri (1.18.6-x86_64-linux-gnu)
119
121
  racc (~> 1.4)
120
- openssl (3.2.1)
122
+ openssl (3.3.0)
121
123
  ostruct (0.6.1)
122
124
  parallel (1.26.3)
123
- parser (3.3.6.0)
125
+ parser (3.3.7.3)
124
126
  ast (~> 2.4.1)
125
127
  racc
126
128
  prettier_print (1.2.1)
127
- process_executer (1.2.0)
129
+ prism (1.4.0)
130
+ process_executer (1.3.0)
128
131
  public_suffix (6.0.1)
129
132
  racc (1.8.1)
130
- rack (3.1.8)
133
+ rack (3.1.12)
131
134
  rack-protection (4.1.1)
132
135
  base64 (>= 0.1.0)
133
136
  logger (>= 1.6.0)
134
137
  rack (>= 3.0.0, < 4)
135
- rack-session (2.0.0)
138
+ rack-session (2.1.0)
139
+ base64 (>= 0.1.0)
136
140
  rack (>= 3.0.0)
137
141
  rainbow (3.1.1)
138
142
  rake (13.2.1)
139
- rchardet (1.8.0)
140
- regexp_parser (2.9.3)
141
- rexml (3.4.0)
143
+ rchardet (1.9.0)
144
+ regexp_parser (2.10.0)
145
+ rexml (3.4.1)
142
146
  rspec (3.13.0)
143
147
  rspec-core (~> 3.13.0)
144
148
  rspec-expectations (~> 3.13.0)
145
149
  rspec-mocks (~> 3.13.0)
146
- rspec-core (3.13.2)
150
+ rspec-core (3.13.3)
147
151
  rspec-support (~> 3.13.0)
148
152
  rspec-expectations (3.13.3)
149
153
  diff-lcs (>= 1.2.0, < 2.0)
@@ -152,37 +156,46 @@ GEM
152
156
  diff-lcs (>= 1.2.0, < 2.0)
153
157
  rspec-support (~> 3.13.0)
154
158
  rspec-support (3.13.2)
155
- rubocop (1.69.2)
159
+ rubocop (1.75.1)
156
160
  json (~> 2.3)
157
- language_server-protocol (>= 3.17.0)
161
+ language_server-protocol (~> 3.17.0.2)
162
+ lint_roller (~> 1.1.0)
158
163
  parallel (~> 1.10)
159
164
  parser (>= 3.3.0.2)
160
165
  rainbow (>= 2.2.2, < 4.0)
161
166
  regexp_parser (>= 2.9.3, < 3.0)
162
- rubocop-ast (>= 1.36.2, < 2.0)
167
+ rubocop-ast (>= 1.43.0, < 2.0)
163
168
  ruby-progressbar (~> 1.7)
164
169
  unicode-display_width (>= 2.4.0, < 4.0)
165
- rubocop-ast (1.37.0)
166
- parser (>= 3.3.1.0)
167
- rubocop-capybara (2.21.0)
168
- rubocop (~> 1.41)
169
- rubocop-factory_bot (2.26.1)
170
- rubocop (~> 1.61)
171
- rubocop-performance (1.23.0)
172
- rubocop (>= 1.48.1, < 2.0)
173
- rubocop-ast (>= 1.31.1, < 2.0)
174
- rubocop-rails (2.27.0)
170
+ rubocop-ast (1.43.0)
171
+ parser (>= 3.3.7.2)
172
+ prism (~> 1.4)
173
+ rubocop-capybara (2.22.1)
174
+ lint_roller (~> 1.1)
175
+ rubocop (~> 1.72, >= 1.72.1)
176
+ rubocop-factory_bot (2.27.1)
177
+ lint_roller (~> 1.1)
178
+ rubocop (~> 1.72, >= 1.72.1)
179
+ rubocop-performance (1.24.0)
180
+ lint_roller (~> 1.1)
181
+ rubocop (>= 1.72.1, < 2.0)
182
+ rubocop-ast (>= 1.38.0, < 2.0)
183
+ rubocop-rails (2.30.3)
175
184
  activesupport (>= 4.2.0)
185
+ lint_roller (~> 1.1)
176
186
  rack (>= 1.1)
177
- rubocop (>= 1.52.0, < 2.0)
178
- rubocop-ast (>= 1.31.1, < 2.0)
179
- rubocop-rake (0.6.0)
180
- rubocop (~> 1.0)
181
- rubocop-rspec (3.3.0)
182
- rubocop (~> 1.61)
183
- rubocop-rspec_rails (2.30.0)
184
- rubocop (~> 1.61)
185
- rubocop-rspec (~> 3, >= 3.0.1)
187
+ rubocop (>= 1.72.1, < 2.0)
188
+ rubocop-ast (>= 1.38.0, < 2.0)
189
+ rubocop-rake (0.7.1)
190
+ lint_roller (~> 1.1)
191
+ rubocop (>= 1.72.1)
192
+ rubocop-rspec (3.5.0)
193
+ lint_roller (~> 1.1)
194
+ rubocop (~> 1.72, >= 1.72.1)
195
+ rubocop-rspec_rails (2.31.0)
196
+ lint_roller (~> 1.1)
197
+ rubocop (~> 1.72, >= 1.72.1)
198
+ rubocop-rspec (~> 3.5)
186
199
  ruby-prof (1.7.1)
187
200
  ruby-progressbar (1.13.0)
188
201
  ruby2_keywords (0.0.5)
@@ -194,6 +207,7 @@ GEM
194
207
  rack-protection (= 4.1.1)
195
208
  rack-session (>= 2.0.0, < 3)
196
209
  tilt (~> 2.0)
210
+ strscan (3.1.2)
197
211
  syntax_tree (6.2.0)
198
212
  prettier_print (>= 1.2.0)
199
213
  syntax_tree-haml (4.0.3)
@@ -204,23 +218,25 @@ GEM
204
218
  prettier_print
205
219
  syntax_tree (>= 2.0.1)
206
220
  temple (0.10.3)
207
- terminal-table (3.0.2)
208
- unicode-display_width (>= 1.1.1, < 3)
221
+ terminal-table (4.0.0)
222
+ unicode-display_width (>= 1.1.1, < 4)
209
223
  thor (1.3.2)
210
- tilt (2.5.0)
224
+ tilt (2.6.0)
211
225
  tzinfo (2.0.6)
212
226
  concurrent-ruby (~> 1.0)
213
- unicode-display_width (2.6.0)
214
- uri (1.0.2)
227
+ unicode-display_width (3.1.4)
228
+ unicode-emoji (~> 4.0, >= 4.0.4)
229
+ unicode-emoji (4.0.4)
230
+ uri (1.0.3)
215
231
  w_syntax_tree-erb (0.12.0)
216
232
  prettier_print (~> 1.2, >= 1.2.0)
217
233
  syntax_tree (~> 6.1, >= 6.1.1)
218
- webmock (3.24.0)
234
+ webmock (3.25.1)
219
235
  addressable (>= 2.8.0)
220
236
  crack (>= 0.3.2)
221
237
  hashdiff (>= 0.4.0, < 2.0.0)
222
238
  yaml (0.4.0)
223
- zeitwerk (2.7.1)
239
+ zeitwerk (2.7.2)
224
240
 
225
241
  PLATFORMS
226
242
  arm64-darwin-23
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.0
1
+ 1.4.0
data/bin/code CHANGED
@@ -43,6 +43,8 @@ require "ruby-prof" if profile
43
43
 
44
44
  RubyProf.start if profile
45
45
 
46
+ input = 'loop { print("> ") puts(evaluate(read)) }' if input.blank?
47
+
46
48
  if parsed.options.parse
47
49
  pp Code::Parser.parse(input).to_raw
48
50
  else
@@ -51,6 +53,7 @@ else
51
53
  input,
52
54
  output: $stdout,
53
55
  error: $stderr,
56
+ input: $stdin,
54
57
  timeout: parsed.options.timeout
55
58
  )
56
59
  )
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  module Concerns
3
5
  module Shared
@@ -124,7 +126,10 @@ class Code
124
126
 
125
127
  code_context.code_fetch(self)
126
128
  else
127
- raise(Error, "#{code_operator.inspect} not defined on #{code_inspect}:#{code_name}")
129
+ raise(
130
+ Error,
131
+ "#{code_operator.inspect} not defined on #{code_inspect}:#{code_name}"
132
+ )
128
133
  end
129
134
  end
130
135
 
@@ -411,7 +416,7 @@ class Code
411
416
  end
412
417
 
413
418
  def code_name
414
- Object::String.new(name)
419
+ Object::String.new(name.to_s.split("::")[2..].join("::"))
415
420
  end
416
421
  end
417
422
  end
data/lib/code/concerns.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  module Concerns
3
5
  end
@@ -12,6 +12,12 @@ class Code
12
12
  end
13
13
  end
14
14
 
15
+ def self.code_evaluate(*args, **globals)
16
+ code_args = args.to_code
17
+
18
+ new(*code_args.raw).code_evaluate(**globals)
19
+ end
20
+
15
21
  def code_evaluate(...)
16
22
  raw.evaluate(...)
17
23
  end
@@ -7,8 +7,10 @@ class Code
7
7
  code_operator = args.fetch(:operator, nil).to_code
8
8
  code_arguments = args.fetch(:arguments, []).to_code
9
9
  output = args.fetch(:output)
10
+ input = args.fetch(:input)
10
11
  code_context = args.fetch(:context).to_code
11
12
  code_value = code_arguments.code_first
13
+ globals = multi_fetch(args, *GLOBALS)
12
14
 
13
15
  case code_operator.to_s
14
16
  when "Boolean"
@@ -145,7 +147,7 @@ class Code
145
147
  code_arguments.any? ? Json.new(*code_arguments.raw) : Class.new(Json)
146
148
  when "evaluate"
147
149
  sig(args) { Object }
148
- Code.evaluate(code_value.to_s)
150
+ Code.code_evaluate(code_value.code_to_string, **globals)
149
151
  when "p"
150
152
  sig(args) { Object.repeat }
151
153
  output.puts(*code_arguments.raw.map(&:inspect))
@@ -154,6 +156,9 @@ class Code
154
156
  sig(args) { Object.repeat }
155
157
  output.print(*code_arguments.raw)
156
158
  Nothing.new
159
+ when "read"
160
+ sig(args) { Object.repeat }
161
+ input.gets.to_code
157
162
  when "puts"
158
163
  sig(args) { Object.repeat }
159
164
  output.puts(*code_arguments.raw)
@@ -437,7 +437,7 @@ class Code
437
437
  end
438
438
 
439
439
  def code_deep_duplicate
440
- List.new(raw.dup.map { |element| element.code_deep_duplicate })
440
+ List.new(raw.dup.map(&:code_deep_duplicate))
441
441
  end
442
442
 
443
443
  def code_get(argument)
@@ -4,17 +4,18 @@ class Code
4
4
  class Object
5
5
  class String < Object
6
6
  def initialize(*args, **_kargs, &)
7
- if args.first.is_an?(Class)
8
- @raw = args.first.raw.name
9
- elsif args.first.is_an?(Object)
10
- @raw = args.first.raw.to_s
11
- elsif args.first.is_a?(::Class)
12
- @raw = args.first.name
13
- elsif args.first
14
- @raw = args.first.to_s
15
- else
16
- @raw = ""
17
- end
7
+ @raw =
8
+ if args.first.is_an?(Class)
9
+ args.first.raw.name
10
+ elsif args.first.is_an?(Object)
11
+ args.first.raw.to_s
12
+ elsif args.first.is_a?(::Class)
13
+ args.first.name
14
+ elsif args.first
15
+ args.first.to_s
16
+ else
17
+ ""
18
+ end
18
19
  end
19
20
 
20
21
  def call(**args)
data/lib/code/object.rb CHANGED
@@ -30,20 +30,16 @@ class Code
30
30
  Type::Or.new(self, other)
31
31
  end
32
32
 
33
- def self.name
34
- super.split("::")[2..].join("::")
35
- end
36
-
37
- def self.code_new(*args)
38
- new(*args)
33
+ def self.code_new(*)
34
+ new(*)
39
35
  end
40
36
 
41
37
  def name
42
38
  self.class.name
43
39
  end
44
40
 
45
- def code_new(*args)
46
- self.class.code_new(**args)
41
+ def code_new(*)
42
+ self.class.code_new(*)
47
43
  end
48
44
  end
49
45
  end
@@ -59,6 +59,10 @@ class Code
59
59
  str("do")
60
60
  end
61
61
 
62
+ def begin_keyword
63
+ str("do")
64
+ end
65
+
62
66
  def end_keyword
63
67
  str("end")
64
68
  end
@@ -107,8 +111,9 @@ class Code
107
111
 
108
112
  def block
109
113
  (
110
- do_keyword << whitespace? << parameters.aka(:parameters).maybe <<
111
- code.aka(:body) << end_keyword.maybe
114
+ (do_keyword | begin_keyword) << whitespace? <<
115
+ parameters.aka(:parameters).maybe << code.aka(:body) <<
116
+ end_keyword.maybe
112
117
  ) |
113
118
  (
114
119
  opening_curly_bracket << whitespace? <<
@@ -7,41 +7,8 @@ class Code
7
7
  While
8
8
  end
9
9
 
10
- def name
11
- Name
12
- end
13
-
14
- def code
15
- Code
16
- end
17
-
18
- def whitespace
19
- Whitespace
20
- end
21
-
22
- def whitespace?
23
- whitespace.maybe
24
- end
25
-
26
- def class_keyword
27
- str("class")
28
- end
29
-
30
- def end_keyword
31
- str("end")
32
- end
33
-
34
- def lesser
35
- str("<")
36
- end
37
-
38
10
  def root
39
- (
40
- class_keyword << whitespace? << name.aka(:name) <<
41
- (
42
- whitespace? << lesser << whitespace? << name.aka(:superclass)
43
- ).maybe << code.aka(:body) << end_keyword.maybe
44
- ).aka(:class) | statement
11
+ statement
45
12
  end
46
13
  end
47
14
  end
@@ -23,6 +23,18 @@ class Code
23
23
  whitespace.maybe
24
24
  end
25
25
 
26
+ def do_keyword
27
+ str("do")
28
+ end
29
+
30
+ def begin_keyword
31
+ str("begin")
32
+ end
33
+
34
+ def end_keyword
35
+ str("end")
36
+ end
37
+
26
38
  def opening_parenthesis
27
39
  str("(")
28
40
  end
@@ -88,11 +100,15 @@ class Code
88
100
  (whitespace? << closing_parenthesis.ignore).maybe
89
101
  end
90
102
 
103
+ def body
104
+ ((begin_keyword | do_keyword) << code << end_keyword.maybe) |
105
+ (opening_curly_bracket << code << closing_curly_bracket.maybe) | code
106
+ end
107
+
91
108
  def root
92
109
  (
93
110
  parameters.aka(:parameters) << whitespace? << equal << greater <<
94
- whitespace? << opening_curly_bracket << code.aka(:body) <<
95
- closing_curly_bracket.maybe
111
+ whitespace? << body.aka(:body)
96
112
  ).aka(:function) | Dictionary
97
113
  end
98
114
  end
@@ -15,8 +15,22 @@ class Code
15
15
  str(")")
16
16
  end
17
17
 
18
+ def end_keyword
19
+ str("end")
20
+ end
21
+
22
+ def do_keyword
23
+ str("do")
24
+ end
25
+
26
+ def begin_keyword
27
+ str("begin")
28
+ end
29
+
18
30
  def root
19
31
  (opening_parenthesis << code << closing_parenthesis.maybe).aka(:group) |
32
+ (begin_keyword << code << end_keyword.maybe).aka(:group) |
33
+ (do_keyword << code << end_keyword.maybe).aka(:group) |
20
34
  Call
21
35
  end
22
36
  end
@@ -23,6 +23,22 @@ class Code
23
23
  whitespace.maybe
24
24
  end
25
25
 
26
+ def do_keyword
27
+ str("do")
28
+ end
29
+
30
+ def begin_keyword
31
+ str("begin")
32
+ end
33
+
34
+ def opening_curly_bracket
35
+ str("{")
36
+ end
37
+
38
+ def closing_curly_bracket
39
+ str("{")
40
+ end
41
+
26
42
  def if_keyword
27
43
  str("if")
28
44
  end
@@ -43,20 +59,25 @@ class Code
43
59
  str("end")
44
60
  end
45
61
 
62
+ def body
63
+ ((begin_keyword | do_keyword) << code << end_keyword.maybe) |
64
+ (opening_curly_bracket << code << closing_curly_bracket.maybe) | code
65
+ end
66
+
46
67
  def root
47
68
  (
48
69
  (if_keyword | unless_keyword).aka(:first_operator) << whitespace <<
49
- statement.aka(:first_statement) << code.aka(:first_body) <<
70
+ statement.aka(:first_statement) << body.aka(:first_body) <<
50
71
  (
51
72
  (
52
73
  elsif_keyword.aka(:operator) << whitespace <<
53
- statement.aka(:statement) << code.aka(:body)
74
+ statement.aka(:statement) << body.aka(:body)
54
75
  ) |
55
76
  (
56
77
  else_keyword << whitespace <<
57
78
  (if_keyword | unless_keyword).aka(:operator) <<
58
- whitespace << statement.aka(:statement) << code.aka(:body)
59
- ) | (else_keyword.aka(:operator) << code.aka(:body))
79
+ whitespace << statement.aka(:statement) << body.aka(:body)
80
+ ) | (else_keyword.aka(:operator) << body.aka(:body))
60
81
  ).repeat(1).aka(:elses).maybe << end_keyword.maybe
61
82
  ).aka(:if) | statement
62
83
  end
@@ -91,10 +91,18 @@ class Code
91
91
  str("elsif")
92
92
  end
93
93
 
94
+ def begin_keyword
95
+ str("begin")
96
+ end
97
+
94
98
  def else_keyword
95
99
  str("else")
96
100
  end
97
101
 
102
+ def begin_keyword
103
+ str("begin")
104
+ end
105
+
98
106
  def special_character
99
107
  ampersand | equal | pipe | dot | colon | comma | space | newline |
100
108
  opening_curly_bracket | closing_curly_bracket | opening_parenthesis |
@@ -113,8 +121,9 @@ class Code
113
121
 
114
122
  def root
115
123
  (do_keyword << separator).absent << (
116
- else_keyword << separator
117
- ).absent << (elsif_keyword << separator).absent <<
124
+ begin_keyword << separator
125
+ ).absent << (else_keyword << separator).absent <<
126
+ (elsif_keyword << separator).absent <<
118
127
  (end_keyword << separator).absent << character.repeat(1)
119
128
  end
120
129
  end
@@ -11,6 +11,10 @@ class Code
11
11
  Whitespace
12
12
  end
13
13
 
14
+ def whitespace?
15
+ whitespace.maybe
16
+ end
17
+
14
18
  def code
15
19
  Code
16
20
  end
@@ -27,10 +31,31 @@ class Code
27
31
  str("end")
28
32
  end
29
33
 
34
+ def do_keyword
35
+ str("do")
36
+ end
37
+
38
+ def begin_keyword
39
+ str("begin")
40
+ end
41
+
30
42
  def loop_keyword
31
43
  str("loop")
32
44
  end
33
45
 
46
+ def opening_curly_bracket
47
+ str("{")
48
+ end
49
+
50
+ def closing_curly_bracket
51
+ str("}")
52
+ end
53
+
54
+ def body
55
+ ((do_keyword | begin_keyword) << code << end_keyword.maybe) |
56
+ (opening_curly_bracket << code << closing_curly_bracket) | code
57
+ end
58
+
34
59
  def root
35
60
  (
36
61
  (
@@ -38,7 +63,7 @@ class Code
38
63
  (while_keyword | until_keyword).aka(:operator) << whitespace <<
39
64
  statement.aka(:statement)
40
65
  ) | (loop_keyword.aka(:operator) << whitespace)
41
- ) << code.aka(:body) << end_keyword.maybe
66
+ ) << body.aka(:body)
42
67
  ).aka(:while) | statement
43
68
  end
44
69
  end
data/lib/code.rb CHANGED
@@ -1,43 +1,50 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Code
4
- GLOBALS = %i[output error context object].freeze
4
+ GLOBALS = %i[context error input object output source object].freeze
5
5
  DEFAULT_TIMEOUT = 0
6
6
 
7
7
  def initialize(
8
- input,
9
- output: StringIO.new,
8
+ source,
9
+ context: Object::Context.new,
10
10
  error: StringIO.new,
11
+ input: StringIO.new,
12
+ object: Object::Global.new,
13
+ output: StringIO.new,
11
14
  timeout: DEFAULT_TIMEOUT
12
15
  )
16
+ @context = context
17
+ @error = error
13
18
  @input = input
19
+ @object = object
14
20
  @output = output
15
- @error = error
16
- @timeout = timeout || DEFAULT_TIMEOUT
17
- @context = Object::Context.new
21
+ @source = source
22
+ @timeout = timeout
18
23
  end
19
24
 
20
- def self.parse(input, timeout: DEFAULT_TIMEOUT)
21
- Timeout.timeout(timeout) { Parser.parse(input).to_raw }
25
+ def self.parse(source, timeout: DEFAULT_TIMEOUT)
26
+ Timeout.timeout(timeout) { Parser.parse(source).to_raw }
22
27
  end
23
28
 
24
- def self.evaluate(
25
- input,
26
- output: StringIO.new,
27
- error: StringIO.new,
28
- timeout: DEFAULT_TIMEOUT
29
- )
30
- new(input, output:, error:, timeout:).evaluate
29
+ def self.evaluate(...)
30
+ new(...).evaluate
31
31
  end
32
32
 
33
33
  def evaluate
34
34
  Timeout.timeout(timeout) do
35
- parsed = Code.parse(input)
36
- Node::Code.new(parsed).evaluate(context:, output:, error:)
35
+ Node::Code.new(Code.parse(source)).evaluate(
36
+ context:,
37
+ error:,
38
+ input:,
39
+ object:,
40
+ output:,
41
+ source:,
42
+ timeout:
43
+ )
37
44
  end
38
45
  end
39
46
 
40
47
  private
41
48
 
42
- attr_reader :input, :timeout, :output, :error, :context
49
+ attr_reader :context, :error, :input, :object, :output, :source, :timeout
43
50
  end
data/spec/code_spec.rb CHANGED
@@ -353,6 +353,8 @@ RSpec.describe Code do
353
353
  next if code_input.is_a?(Code::Object::Decimal)
354
354
 
355
355
  expect(code_input.to_json).to eq(code_expected.to_json)
356
+ YAML.unsafe_load(code_input.to_yaml)
357
+ YAML.unsafe_load(code_expected.to_yaml)
356
358
  end
357
359
  end
358
360
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-03-28 00:00:00.000000000 Z
10
+ date: 2025-04-04 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: activesupport