code-ruby 0.6.2 → 0.6.4
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 +4 -4
- data/.gitignore +2 -0
- data/.ruby-version +1 -0
- data/.tool-versions +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +5 -2
- data/bin/code +4 -12
- data/code-ruby.gemspec +6 -4
- data/lib/code/node/nothing.rb +2 -1
- data/lib/code/object/dictionary.rb +21 -9
- data/lib/code/object/function.rb +5 -4
- data/lib/code/object.rb +1 -1
- data/lib/code/parser/string.rb +1 -1
- data/lib/code/type/hash.rb +5 -7
- data/lib/code/type/repeat.rb +1 -1
- data/lib/code/type/sig.rb +10 -4
- data/lib/code/version.rb +3 -1
- data/lib/code-ruby.rb +2 -0
- data/spec/code/node/call_spec.rb +31 -33
- data/spec/code/object/function_spec.rb +12 -14
- data/spec/code/object/list_spec.rb +1 -4
- data/spec/code_spec.rb +1 -1
- data/spec/spec_helper.rb +2 -0
- metadata +13 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2885af9560ee132a0d9ab0be653fadd74e54e04975a1673ff98d929e14040088
|
4
|
+
data.tar.gz: 68450cd4d425cc61bccd154f32b7786a2e7f0778ca567a2e19346b017f6e40c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f090263fb431fa7d6ab1193bd5735b8ef8c425783eea75c8495537a2f2c80576dbe8a71ecf3d41202eea787687cdf9f2d6ef0b4dcec931e38be9cc28baa2ed50
|
7
|
+
data.tar.gz: d2fbf7f1e6534a1f15e9fca4271b9505fb0229984a53e85d9adbfaaeba5b94535cfde135eede3289f564f06949d0f4bb3c2ff3ac384ffa56596bb802eef2be7e
|
data/.gitignore
ADDED
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
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
code-ruby (0.6.
|
4
|
+
code-ruby (0.6.3)
|
5
5
|
bigdecimal (~> 3)
|
6
6
|
language-ruby (~> 0)
|
7
7
|
zeitwerk (~> 2)
|
@@ -11,7 +11,7 @@ GEM
|
|
11
11
|
specs:
|
12
12
|
bigdecimal (3.1.6)
|
13
13
|
diff-lcs (1.5.0)
|
14
|
-
language-ruby (0.6.
|
14
|
+
language-ruby (0.6.1)
|
15
15
|
zeitwerk (~> 2)
|
16
16
|
rspec (3.12.0)
|
17
17
|
rspec-core (~> 3.12.0)
|
@@ -38,5 +38,8 @@ DEPENDENCIES
|
|
38
38
|
rspec
|
39
39
|
ruby-prof
|
40
40
|
|
41
|
+
RUBY VERSION
|
42
|
+
ruby 3.3.0p0
|
43
|
+
|
41
44
|
BUNDLED WITH
|
42
45
|
2.5.5
|
data/bin/code
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
require "optparse"
|
4
5
|
require_relative "../lib/code-ruby"
|
@@ -9,11 +10,7 @@ OptionParser
|
|
9
10
|
.new do |opts|
|
10
11
|
opts.banner = "Usage: template [options]"
|
11
12
|
|
12
|
-
opts.on(
|
13
|
-
"-v",
|
14
|
-
"--version",
|
15
|
-
"Version of template"
|
16
|
-
) do |input|
|
13
|
+
opts.on("-v", "--version", "Version of template") do |_input|
|
17
14
|
puts Code::Version
|
18
15
|
exit
|
19
16
|
end
|
@@ -48,10 +45,7 @@ OptionParser
|
|
48
45
|
"Set timeout in seconds"
|
49
46
|
) { |timeout| options[:timeout] = timeout.to_f }
|
50
47
|
|
51
|
-
opts.on(
|
52
|
-
"--profile",
|
53
|
-
"Profile Ruby code"
|
54
|
-
) do |timeout|
|
48
|
+
opts.on("--profile", "Profile Ruby code") do |_timeout|
|
55
49
|
require "ruby-prof"
|
56
50
|
options[:profile] = true
|
57
51
|
end
|
@@ -70,9 +64,7 @@ OptionParser
|
|
70
64
|
input = options.fetch(:input, "")
|
71
65
|
context = options.fetch(:context, "")
|
72
66
|
|
73
|
-
if options[:profile]
|
74
|
-
RubyProf.start
|
75
|
-
end
|
67
|
+
RubyProf.start if options[:profile]
|
76
68
|
|
77
69
|
if options[:parse]
|
78
70
|
pp Code::Parser.parse(input).to_raw
|
data/code-ruby.gemspec
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "English"
|
1
4
|
require_relative "lib/code/version"
|
2
5
|
|
3
6
|
Gem::Specification.new do |s|
|
@@ -7,14 +10,13 @@ Gem::Specification.new do |s|
|
|
7
10
|
s.description = 'A programming language, like Code.evaluate("1 + 1") # => 2'
|
8
11
|
s.authors = ["Dorian Marié"]
|
9
12
|
s.email = "dorian@dorianmarie.fr"
|
10
|
-
s.files = `git ls-files`.split(
|
11
|
-
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
13
|
+
s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
12
14
|
s.require_paths = ["lib"]
|
13
15
|
s.homepage = "https://github.com/dorianmariefr/code-ruby"
|
14
16
|
s.license = "MIT"
|
15
17
|
s.executables = "code"
|
16
18
|
|
17
|
-
s.add_dependency "zeitwerk", "~> 2"
|
18
|
-
s.add_dependency "language-ruby", "~> 0"
|
19
19
|
s.add_dependency "bigdecimal", "~> 3"
|
20
|
+
s.add_dependency "language-ruby", "~> 0"
|
21
|
+
s.add_dependency "zeitwerk", "~> 2"
|
20
22
|
end
|
data/lib/code/node/nothing.rb
CHANGED
@@ -214,7 +214,11 @@ class Code
|
|
214
214
|
|
215
215
|
def code_delete(*arguments, index: Integer.new(0), **globals)
|
216
216
|
default =
|
217
|
-
(
|
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
|
|
@@ -303,8 +307,8 @@ class Code
|
|
303
307
|
self
|
304
308
|
end
|
305
309
|
|
306
|
-
def code_dig(*
|
307
|
-
raw.dig(*
|
310
|
+
def code_dig(*)
|
311
|
+
raw.dig(*) || Nothing.new
|
308
312
|
end
|
309
313
|
|
310
314
|
def code_each(argument, **globals)
|
@@ -331,13 +335,17 @@ class Code
|
|
331
335
|
Boolean.new(raw.empty?)
|
332
336
|
end
|
333
337
|
|
334
|
-
def code_except(*
|
335
|
-
self.class.new(raw.except(*
|
338
|
+
def code_except(*)
|
339
|
+
self.class.new(raw.except(*))
|
336
340
|
end
|
337
341
|
|
338
342
|
def code_fetch(*arguments, index: Integer.new(0), **globals)
|
339
343
|
default =
|
340
|
-
(
|
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
|
|
@@ -386,8 +394,8 @@ class Code
|
|
386
394
|
end
|
387
395
|
end
|
388
396
|
|
389
|
-
def code_fetch_values(*
|
390
|
-
List.new(raw.fetch_values(*
|
397
|
+
def code_fetch_values(*)
|
398
|
+
List.new(raw.fetch_values(*))
|
391
399
|
end
|
392
400
|
|
393
401
|
def code_five?
|
@@ -485,7 +493,11 @@ class Code
|
|
485
493
|
|
486
494
|
def code_merge(*arguments, **globals)
|
487
495
|
conflict =
|
488
|
-
(
|
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
|
|
data/lib/code/object/function.rb
CHANGED
@@ -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 =
|
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
|
data/lib/code/object.rb
CHANGED
data/lib/code/parser/string.rb
CHANGED
data/lib/code/type/hash.rb
CHANGED
@@ -6,28 +6,26 @@ 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)
|
13
13
|
return false unless argument.is_a?(Object::Dictionary)
|
14
|
+
|
14
15
|
argument = argument.raw
|
15
16
|
(argument.keys + hash.keys).uniq.all? do |key|
|
16
17
|
next false unless hash[key] && argument[key]
|
18
|
+
|
17
19
|
valid_for?(expected: hash[key], actual: argument[key])
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
21
23
|
def min_arguments
|
22
|
-
hash.sum
|
23
|
-
min_arguments_of(value)
|
24
|
-
end
|
24
|
+
hash.sum { |_, value| min_arguments_of(value) }
|
25
25
|
end
|
26
26
|
|
27
27
|
def max_arguments
|
28
|
-
hash.sum
|
29
|
-
max_arguments_of(value)
|
30
|
-
end
|
28
|
+
hash.sum { |_, value| max_arguments_of(value) }
|
31
29
|
end
|
32
30
|
|
33
31
|
def name
|
data/lib/code/type/repeat.rb
CHANGED
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 ||=
|
27
|
-
|
28
|
-
|
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 =
|
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
data/lib/code-ruby.rb
CHANGED
data/spec/code/node/call_spec.rb
CHANGED
@@ -3,37 +3,35 @@
|
|
3
3
|
require "spec_helper"
|
4
4
|
|
5
5
|
RSpec.describe Code::Node::Call do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
[
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
end
|
38
|
-
=end
|
6
|
+
# let(:context) { <<~CONTEXT }
|
7
|
+
# render = (*args, **kargs, &block) => {
|
8
|
+
# [args, kargs, block&.call(:dorian)]
|
9
|
+
# }
|
10
|
+
#
|
11
|
+
# { render:, user: { name: :Dorian } }"
|
12
|
+
# CONTEXT
|
13
|
+
#
|
14
|
+
# [
|
15
|
+
# ["render", "[[], {}, nothing]"],
|
16
|
+
# ["render()", "[[], {}, nothing]"],
|
17
|
+
# ["render(user)", "[[user], {}, nothing]"],
|
18
|
+
# [
|
19
|
+
# "render(user, first_name: :Dorian)",
|
20
|
+
# "[[user], { first_name: :Dorian }, nothing]"
|
21
|
+
# ],
|
22
|
+
# ["render { }", "[[], {}, nothing]"],
|
23
|
+
# ["render do end", "[[], {}, nothing]"],
|
24
|
+
# ["render { |name| name.upcase }", "[[], {}, :Dorian]"],
|
25
|
+
# ["render(user) { |name| name.upcase }", "[[user], {}, :Dorian]"],
|
26
|
+
# ["render(user) do |name| name.upcase end", "[[user], {}, :Dorian]"],
|
27
|
+
# ["render { :Dorian }", "[[], {}, :Dorian]"],
|
28
|
+
# ["render(user) { :Dorian }", "[[user], {}, :Dorian]"],
|
29
|
+
# ["render(user) do :Dorian end", "[[user], {}, :Dorian]"]
|
30
|
+
# ].each do |input, expected|
|
31
|
+
# it "#{input} == #{expected}" do
|
32
|
+
# expect(Code.evaluate(input, context)).to eq(
|
33
|
+
# Code.evaluate(expected, context)
|
34
|
+
# )
|
35
|
+
# end
|
36
|
+
# end
|
39
37
|
end
|
@@ -3,24 +3,22 @@
|
|
3
3
|
require "spec_helper"
|
4
4
|
|
5
5
|
RSpec.describe Code::Object::Function do
|
6
|
-
|
7
|
-
[
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
17
|
-
=end
|
6
|
+
# [
|
7
|
+
# ["even? = (i) => { i.even? } even?(2)", "true"],
|
8
|
+
# ["even? = (i:) => { i.even? } even?(i: 2)", "true"],
|
9
|
+
# ["add = (a, b) => { a + b } add(1, 2)", "3"],
|
10
|
+
# ["minus = (a:, b:) => { a - b } minus(b: 1, a: 2)", "1"]
|
11
|
+
# ].each do |input, expected|
|
12
|
+
# it "#{input} == #{expected}" do
|
13
|
+
# expect(Code.evaluate(input)).to eq(Code.evaluate(expected))
|
14
|
+
# end
|
15
|
+
# end
|
18
16
|
|
19
17
|
context "valid" do
|
20
18
|
[
|
21
19
|
"f = () => {} f",
|
22
|
-
"f = (x) => {} f(1)"
|
23
|
-
# "f = (x:) => {} f(x: 1)"
|
20
|
+
"f = (x) => {} f(1)"
|
21
|
+
# "f = (x:) => {} f(x: 1)"
|
24
22
|
].each do |input|
|
25
23
|
it "#{input} is valid" do
|
26
24
|
Code.evaluate(input)
|
@@ -3,10 +3,7 @@
|
|
3
3
|
require "spec_helper"
|
4
4
|
|
5
5
|
RSpec.describe Code::Object::List do
|
6
|
-
[
|
7
|
-
["[] == []", "true"],
|
8
|
-
["[1, 2, 3].sum", "6"],
|
9
|
-
].each do |input, expected|
|
6
|
+
[["[] == []", "true"], ["[1, 2, 3].sum", "6"]].each do |input, expected|
|
10
7
|
it "#{input} == #{expected}" do
|
11
8
|
expect(Code.evaluate(input)).to eq(Code.evaluate(expected))
|
12
9
|
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[
|
112
|
+
%w[:Hello.include?(:H) true],
|
113
113
|
%w["Hello".downcase :hello],
|
114
114
|
["true ? 1", "1"],
|
115
115
|
["false ? 1", ""],
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: code-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.4
|
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-01-
|
11
|
+
date: 2024-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: bigdecimal
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '3'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: language-ruby
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -39,19 +39,19 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: zeitwerk
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '2'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '2'
|
55
55
|
description: 'A programming language, like Code.evaluate("1 + 1") # => 2'
|
56
56
|
email: dorian@dorianmarie.fr
|
57
57
|
executables:
|
@@ -59,7 +59,10 @@ executables:
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
+
- ".gitignore"
|
62
63
|
- ".rspec"
|
64
|
+
- ".ruby-version"
|
65
|
+
- ".tool-versions"
|
63
66
|
- Gemfile
|
64
67
|
- Gemfile.lock
|
65
68
|
- README.md
|
@@ -203,30 +206,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
203
206
|
- !ruby/object:Gem::Version
|
204
207
|
version: '0'
|
205
208
|
requirements: []
|
206
|
-
rubygems_version: 3.5.
|
209
|
+
rubygems_version: 3.5.3
|
207
210
|
signing_key:
|
208
211
|
specification_version: 4
|
209
212
|
summary: A programming language
|
210
|
-
test_files:
|
211
|
-
- spec/code/node/call_spec.rb
|
212
|
-
- spec/code/object/boolean_spec.rb
|
213
|
-
- spec/code/object/decimal_spec.rb
|
214
|
-
- spec/code/object/dictionary_spec.rb
|
215
|
-
- spec/code/object/function_spec.rb
|
216
|
-
- spec/code/object/integer_spec.rb
|
217
|
-
- spec/code/object/list_spec.rb
|
218
|
-
- spec/code/object/nothing_spec.rb
|
219
|
-
- spec/code/object/range_spec.rb
|
220
|
-
- spec/code/parser/boolean_spec.rb
|
221
|
-
- spec/code/parser/chained_call.rb
|
222
|
-
- spec/code/parser/dictionary_spec.rb
|
223
|
-
- spec/code/parser/function_spec.rb
|
224
|
-
- spec/code/parser/group_spec.rb
|
225
|
-
- spec/code/parser/if_modifier_spec.rb
|
226
|
-
- spec/code/parser/list_spec.rb
|
227
|
-
- spec/code/parser/number_spec.rb
|
228
|
-
- spec/code/parser/string_spec.rb
|
229
|
-
- spec/code/parser_spec.rb
|
230
|
-
- spec/code/type_spec.rb
|
231
|
-
- spec/code_spec.rb
|
232
|
-
- spec/spec_helper.rb
|
213
|
+
test_files: []
|