code-ruby 0.6.1 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1d1e6240535fbaa53a8402bb09fca25f382435c77b631953da7650279c1779c4
4
- data.tar.gz: 31ca274073a554129651ad5f6f398ec421bdf34ce20d0953f360b801bb4ba1f4
3
+ metadata.gz: 7ab3a0f0749357d4065315d58b613f8d44cba5f567b6ae33444cff90c1dedde0
4
+ data.tar.gz: b48a0d5c713c0eb33c53bd30fafaaa7bc03f657a7ac7a5c70ae257eedf677cc0
5
5
  SHA512:
6
- metadata.gz: a81a91547923b0bcdca0b971fc0a18a684c439a4bf4821fd4b981afa06d4dbcb7baaf00b4e0e04513a2bc4abbd965b538ab92a7f4cf5739a42d1ffd306c72108
7
- data.tar.gz: b4135cc2b58e02bde82fe6fb41de533f5c37e0c8ca13bd2507e8c559fb2ebd71b796ab36059c5db0f0a51ca73091c75581136c38a5558e89e0f3ef8341f7a436
6
+ metadata.gz: e137c317b1d87a1a61d30ce70d4bccdd75417ab002c6ad57249081f2e8b8189e4194efaad6e277f68448fa8f55b1402e337d5387d34449b69fce4425b17d72e7
7
+ data.tar.gz: 2bdfced46d44b0ef4787a998fb17ce073b38a939d044a9f48f39c74dc42adbec3962622792f9852579203f090a66db78dc45aa118d25ff325089cde03cb00c93
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ .env
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.0
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 3.3.0
data/Gemfile CHANGED
@@ -2,5 +2,7 @@ source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
+ ruby "3.3.0"
6
+
5
7
  gem "rspec"
6
8
  gem "ruby-prof"
data/Gemfile.lock CHANGED
@@ -1,13 +1,15 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-ruby (0.6.0)
4
+ code-ruby (0.6.2)
5
+ bigdecimal (~> 3)
5
6
  language-ruby (~> 0)
6
7
  zeitwerk (~> 2)
7
8
 
8
9
  GEM
9
10
  remote: https://rubygems.org/
10
11
  specs:
12
+ bigdecimal (3.1.6)
11
13
  diff-lcs (1.5.0)
12
14
  language-ruby (0.6.0)
13
15
  zeitwerk (~> 2)
@@ -24,16 +26,20 @@ GEM
24
26
  diff-lcs (>= 1.2.0, < 2.0)
25
27
  rspec-support (~> 3.12.0)
26
28
  rspec-support (3.12.1)
27
- ruby-prof (1.6.3)
29
+ ruby-prof (1.7.0)
28
30
  zeitwerk (2.6.12)
29
31
 
30
32
  PLATFORMS
31
- arm64-darwin-22
33
+ arm64-darwin-23
34
+ ruby
32
35
 
33
36
  DEPENDENCIES
34
37
  code-ruby!
35
38
  rspec
36
39
  ruby-prof
37
40
 
41
+ RUBY VERSION
42
+ ruby 3.3.0p0
43
+
38
44
  BUNDLED WITH
39
- 2.4.22
45
+ 2.5.5
data/README.md CHANGED
@@ -1,3 +1,7 @@
1
1
  # code-ruby
2
2
 
3
3
  A programming language, like `Code.evaluate("1 + 1") # => 2`
4
+
5
+ TODO
6
+
7
+ - [ ] `code` command
data/bin/code CHANGED
@@ -9,11 +9,7 @@ OptionParser
9
9
  .new do |opts|
10
10
  opts.banner = "Usage: template [options]"
11
11
 
12
- opts.on(
13
- "-v",
14
- "--version",
15
- "Version of template"
16
- ) do |input|
12
+ opts.on("-v", "--version", "Version of template") do |input|
17
13
  puts Code::Version
18
14
  exit
19
15
  end
@@ -48,10 +44,7 @@ OptionParser
48
44
  "Set timeout in seconds"
49
45
  ) { |timeout| options[:timeout] = timeout.to_f }
50
46
 
51
- opts.on(
52
- "--profile",
53
- "Profile Ruby code"
54
- ) do |timeout|
47
+ opts.on("--profile", "Profile Ruby code") do |timeout|
55
48
  require "ruby-prof"
56
49
  options[:profile] = true
57
50
  end
@@ -70,9 +63,7 @@ OptionParser
70
63
  input = options.fetch(:input, "")
71
64
  context = options.fetch(:context, "")
72
65
 
73
- if options[:profile]
74
- RubyProf.start
75
- end
66
+ RubyProf.start if options[:profile]
76
67
 
77
68
  if options[:parse]
78
69
  pp Code::Parser.parse(input).to_raw
data/code-ruby.gemspec CHANGED
@@ -16,4 +16,5 @@ Gem::Specification.new do |s|
16
16
 
17
17
  s.add_dependency "zeitwerk", "~> 2"
18
18
  s.add_dependency "language-ruby", "~> 0"
19
+ s.add_dependency "bigdecimal", "~> 3"
19
20
  end
@@ -214,7 +214,11 @@ class Code
214
214
 
215
215
  def code_delete(*arguments, index: Integer.new(0), **globals)
216
216
  default =
217
- (arguments.last if arguments.last.is_a?(Function) && arguments.size > 1)
217
+ (
218
+ if arguments.last.is_a?(Function) && arguments.size > 1
219
+ arguments.last
220
+ end
221
+ )
218
222
 
219
223
  arguments = arguments[..-2] if default
220
224
 
@@ -337,7 +341,11 @@ class Code
337
341
 
338
342
  def code_fetch(*arguments, index: Integer.new(0), **globals)
339
343
  default =
340
- (arguments.last if arguments.last.is_a?(Function) && arguments.size > 1)
344
+ (
345
+ if arguments.last.is_a?(Function) && arguments.size > 1
346
+ arguments.last
347
+ end
348
+ )
341
349
 
342
350
  arguments = arguments[..-2] if default
343
351
 
@@ -485,7 +493,11 @@ class Code
485
493
 
486
494
  def code_merge(*arguments, **globals)
487
495
  conflict =
488
- (arguments.last if arguments.last.is_a?(Function) && arguments.size > 1)
496
+ (
497
+ if arguments.last.is_a?(Function) && arguments.size > 1
498
+ arguments.last
499
+ end
500
+ )
489
501
 
490
502
  arguments = arguments[..-2] if conflict
491
503
 
@@ -36,9 +36,7 @@ class Code
36
36
  if parameter.regular_splat?
37
37
  context.code_set(
38
38
  parameter.name,
39
- List.new(
40
- arguments.select(&:regular?).map(&:value)
41
- )
39
+ List.new(arguments.select(&:regular?).map(&:value))
42
40
  )
43
41
  elsif parameter.keyword_splat?
44
42
  context.code_set(
@@ -53,7 +51,10 @@ class Code
53
51
  context.code_set(parameter.name, argument)
54
52
  end
55
53
  elsif parameter.keyword?
56
- argument = arguments.detect { |argument| argument.name == parameter.name }&.value
54
+ argument =
55
+ arguments
56
+ .detect { |argument| argument.name == parameter.name }
57
+ &.value
57
58
  argument = parameter.evaluate(**globals) if argument.nil?
58
59
  context.code_set(parameter.name, argument)
59
60
  else
@@ -71,6 +71,9 @@ class Code
71
71
  when "size"
72
72
  sig(args)
73
73
  code_size
74
+ when "sum"
75
+ sig(args)
76
+ code_sum
74
77
  when "uniq"
75
78
  sig(args)
76
79
  code_uniq
@@ -197,6 +200,10 @@ class Code
197
200
  List.new(raw.uniq)
198
201
  end
199
202
 
203
+ def code_sum
204
+ raw.inject(&:code_plus) || Nothing.new
205
+ end
206
+
200
207
  def inspect
201
208
  to_s
202
209
  end
@@ -33,7 +33,7 @@ class Code
33
33
  sig(args)
34
34
  code_downcase
35
35
  when "include?"
36
- sig(args) { String }
36
+ sig(args) { { a: String } }
37
37
  code_include?(value)
38
38
  when "reverse"
39
39
  sig(args)
@@ -6,7 +6,7 @@ class Code
6
6
  attr_reader :hash
7
7
 
8
8
  def initialize(hash)
9
- @hash = hash
9
+ @hash = hash.transform_keys { |key| Object::String.new(key) }
10
10
  end
11
11
 
12
12
  def valid?(argument)
@@ -19,15 +19,11 @@ class Code
19
19
  end
20
20
 
21
21
  def min_arguments
22
- hash.sum do |_, value|
23
- min_arguments_of(value)
24
- end
22
+ hash.sum { |_, value| min_arguments_of(value) }
25
23
  end
26
24
 
27
25
  def max_arguments
28
- hash.sum do |_, value|
29
- max_arguments_of(value)
30
- end
26
+ hash.sum { |_, value| max_arguments_of(value) }
31
27
  end
32
28
 
33
29
  def name
data/lib/code/type/sig.rb CHANGED
@@ -23,9 +23,10 @@ class Code
23
23
  attr_reader :args, :block, :object
24
24
 
25
25
  def expected_arguments
26
- @expected_arguments ||= Array(block&.call || []).map do |clazz|
27
- clazz.is_a?(::Hash) ? Hash.new(clazz) : clazz
28
- end
26
+ @expected_arguments ||=
27
+ wrap(block&.call || []).map do |clazz|
28
+ clazz.is_a?(::Hash) ? Hash.new(clazz) : clazz
29
+ end
29
30
  end
30
31
 
31
32
  def min_arguments_of(clazz)
@@ -53,7 +54,8 @@ class Code
53
54
  end
54
55
 
55
56
  def max_arguments
56
- max_arguments = expected_arguments.map { |clazz| max_arguments_of(clazz) }
57
+ max_arguments =
58
+ expected_arguments.map { |clazz| max_arguments_of(clazz) }
57
59
  max_arguments.include?(nil) ? nil : max_arguments.sum
58
60
  end
59
61
 
@@ -96,6 +98,10 @@ class Code
96
98
  expected.is_a?(Type) ? expected.valid?(actual) : actual.is_a?(expected)
97
99
  end
98
100
 
101
+ def wrap(object)
102
+ object.is_a?(Array) ? object : [object]
103
+ end
104
+
99
105
  def check_types_of_arguments!
100
106
  expected_index = 0
101
107
  repeat_index = 0
data/lib/code/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  require_relative "../code"
2
2
 
3
- Code::Version = Gem::Version.new("0.6.1")
3
+ Code::Version = Gem::Version.new("0.6.3")
@@ -19,8 +19,8 @@ RSpec.describe Code::Object::Function do
19
19
  context "valid" do
20
20
  [
21
21
  "f = () => {} f",
22
- "f = (x) => {} f(1)",
23
- # "f = (x:) => {} f(x: 1)"
22
+ "f = (x) => {} f(1)"
23
+ # "f = (x:) => {} f(x: 1)"
24
24
  ].each do |input|
25
25
  it "#{input} is valid" do
26
26
  Code.evaluate(input)
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe Code::Object::List do
6
+ [["[] == []", "true"], ["[1, 2, 3].sum", "6"]].each do |input, expected|
7
+ it "#{input} == #{expected}" do
8
+ expect(Code.evaluate(input)).to eq(Code.evaluate(expected))
9
+ end
10
+ end
11
+ end
data/spec/code_spec.rb CHANGED
@@ -109,7 +109,7 @@ RSpec.describe Code do
109
109
  ['"Hello \\{name}"', '"Hello \\{" + "name}"'],
110
110
  ["'Hello {1}'", '"Hello 1"'],
111
111
  ['"Hello {1}"', '"Hello 1"'],
112
- %w["Hello".include?("H") true],
112
+ [":Hello.include?(a: :H)", true],
113
113
  %w["Hello".downcase :hello],
114
114
  ["true ? 1", "1"],
115
115
  ["false ? 1", ""],
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.6.1
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-24 00:00:00.000000000 Z
11
+ date: 2024-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zeitwerk
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bigdecimal
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
41
55
  description: 'A programming language, like Code.evaluate("1 + 1") # => 2'
42
56
  email: dorian@dorianmarie.fr
43
57
  executables:
@@ -45,7 +59,10 @@ executables:
45
59
  extensions: []
46
60
  extra_rdoc_files: []
47
61
  files:
62
+ - ".gitignore"
48
63
  - ".rspec"
64
+ - ".ruby-version"
65
+ - ".tool-versions"
49
66
  - Gemfile
50
67
  - Gemfile.lock
51
68
  - README.md
@@ -154,6 +171,7 @@ files:
154
171
  - spec/code/object/dictionary_spec.rb
155
172
  - spec/code/object/function_spec.rb
156
173
  - spec/code/object/integer_spec.rb
174
+ - spec/code/object/list_spec.rb
157
175
  - spec/code/object/nothing_spec.rb
158
176
  - spec/code/object/range_spec.rb
159
177
  - spec/code/parser/boolean_spec.rb
@@ -188,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
188
206
  - !ruby/object:Gem::Version
189
207
  version: '0'
190
208
  requirements: []
191
- rubygems_version: 3.4.22
209
+ rubygems_version: 3.5.5
192
210
  signing_key:
193
211
  specification_version: 4
194
212
  summary: A programming language
@@ -199,6 +217,7 @@ test_files:
199
217
  - spec/code/object/dictionary_spec.rb
200
218
  - spec/code/object/function_spec.rb
201
219
  - spec/code/object/integer_spec.rb
220
+ - spec/code/object/list_spec.rb
202
221
  - spec/code/object/nothing_spec.rb
203
222
  - spec/code/object/range_spec.rb
204
223
  - spec/code/parser/boolean_spec.rb