code-ruby 0.6.4 → 0.6.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/bin/code +24 -24
- data/lib/code/node/base_10.rb +1 -3
- data/lib/code/node/call.rb +5 -4
- data/lib/code/node/decimal.rb +1 -3
- data/lib/code/node/dictionary.rb +1 -1
- data/lib/code/node/if.rb +5 -5
- data/lib/code/object/decimal.rb +1 -3
- data/lib/code/object/dictionary.rb +3 -9
- data/lib/code/object/function.rb +1 -1
- data/lib/code/object/integer.rb +1 -9
- data/lib/code/object/ruby_function.rb +2 -2
- data/lib/code/object/string.rb +2 -2
- data/lib/code/object.rb +1 -3
- data/lib/code/ruby.rb +2 -17
- data/lib/code/version.rb +1 -1
- data/spec/code_spec.rb +3 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 22e265eb7980e078c895b92b71d1e5b0e3e3a35cb833537bdc0f1614ce2ac76b
|
4
|
+
data.tar.gz: 9d2f374bb8a627bc3b0fbb2325630a692fa01bef52a9a81750db643454163f60
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b43ac0ea5ad94cdf569518df455be616c0a1a21ba90ec548c278080eaf36b8466707960ff07bc90739a3ebb2cd9e31e481da4646eebd47ef9cd4e5d543b7c7a2
|
7
|
+
data.tar.gz: 33c41f911e6a9043f79a2c89a58e373fd200715f35bfcd8c0bea5554a10c2eca2c8b4fd145f4676c33a7723e5d5fb15179a6af568b079e43170fa0ce09eced15
|
data/Gemfile.lock
CHANGED
data/bin/code
CHANGED
@@ -1,24 +1,24 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
-
require
|
5
|
-
require_relative
|
4
|
+
require 'optparse'
|
5
|
+
require_relative '../lib/code-ruby'
|
6
6
|
|
7
|
-
options = { timeout: 0, profile: false, profiler:
|
7
|
+
options = { timeout: 0, profile: false, profiler: 'text' }
|
8
8
|
|
9
9
|
OptionParser
|
10
10
|
.new do |opts|
|
11
|
-
opts.banner =
|
11
|
+
opts.banner = 'Usage: template [options]'
|
12
12
|
|
13
|
-
opts.on(
|
13
|
+
opts.on('-v', '--version', 'Version of template') do |_input|
|
14
14
|
puts Code::Version
|
15
15
|
exit
|
16
16
|
end
|
17
17
|
|
18
18
|
opts.on(
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
'-i INPUT',
|
20
|
+
'--input=INPUT',
|
21
|
+
'Input in the code language (String or File)'
|
22
22
|
) do |input|
|
23
23
|
input = File.read(input) if File.exist?(input)
|
24
24
|
|
@@ -26,43 +26,43 @@ OptionParser
|
|
26
26
|
end
|
27
27
|
|
28
28
|
opts.on(
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
'-c CONTEXT',
|
30
|
+
'--context=CONTEXT',
|
31
|
+
'Context in the code language (String or File)'
|
32
32
|
) do |context|
|
33
33
|
context = File.read(context) if File.exist?(context)
|
34
34
|
|
35
35
|
options[:context] = context
|
36
36
|
end
|
37
37
|
|
38
|
-
opts.on(
|
38
|
+
opts.on('-p', '--parse', 'Get parser results for input') do |parse|
|
39
39
|
options[:parse] = parse
|
40
40
|
end
|
41
41
|
|
42
42
|
opts.on(
|
43
|
-
|
44
|
-
|
45
|
-
|
43
|
+
'-t TIMEOUT',
|
44
|
+
'--timeout=TIMEOUT',
|
45
|
+
'Set timeout in seconds'
|
46
46
|
) { |timeout| options[:timeout] = timeout.to_f }
|
47
47
|
|
48
|
-
opts.on(
|
49
|
-
require
|
48
|
+
opts.on('--profile', 'Profile Ruby code') do |_timeout|
|
49
|
+
require 'ruby-prof'
|
50
50
|
options[:profile] = true
|
51
51
|
end
|
52
52
|
|
53
53
|
opts.on(
|
54
|
-
|
55
|
-
|
54
|
+
'--profiler TYPE',
|
55
|
+
'Profiler output type (text (default) or html)'
|
56
56
|
) do |profiler|
|
57
|
-
require
|
57
|
+
require 'ruby-prof'
|
58
58
|
options[:profile] = true
|
59
59
|
options[:profiler] = profiler
|
60
60
|
end
|
61
61
|
end
|
62
62
|
.parse!
|
63
63
|
|
64
|
-
input = options.fetch(:input,
|
65
|
-
context = options.fetch(:context,
|
64
|
+
input = options.fetch(:input, '')
|
65
|
+
context = options.fetch(:context, '')
|
66
66
|
|
67
67
|
RubyProf.start if options[:profile]
|
68
68
|
|
@@ -74,10 +74,10 @@ end
|
|
74
74
|
|
75
75
|
if options[:profile]
|
76
76
|
result = RubyProf.stop
|
77
|
-
if options[:profiler] ==
|
77
|
+
if options[:profiler] == 'text'
|
78
78
|
printer = RubyProf::FlatPrinter.new(result)
|
79
79
|
printer.print($stdout)
|
80
|
-
elsif options[:profiler] ==
|
80
|
+
elsif options[:profiler] == 'html'
|
81
81
|
printer = RubyProf::GraphHtmlPrinter.new(result)
|
82
82
|
printer.print($stdout)
|
83
83
|
else
|
data/lib/code/node/base_10.rb
CHANGED
@@ -6,9 +6,7 @@ class Code
|
|
6
6
|
def initialize(parsed)
|
7
7
|
@whole = parsed.delete(:whole)
|
8
8
|
|
9
|
-
if parsed.key?(:exponent)
|
10
|
-
@exponent = Node::Statement.new(parsed.delete(:exponent))
|
11
|
-
end
|
9
|
+
@exponent = Node::Statement.new(parsed.delete(:exponent)) if parsed.key?(:exponent)
|
12
10
|
|
13
11
|
super(parsed)
|
14
12
|
end
|
data/lib/code/node/call.rb
CHANGED
@@ -40,13 +40,14 @@ class Code
|
|
40
40
|
@arguments.each do |argument|
|
41
41
|
if argument.keyword?
|
42
42
|
if arguments.last&.value.is_a?(Object::Dictionary)
|
43
|
-
arguments.last.value
|
44
|
-
|
45
|
-
|
43
|
+
arguments.last.value.code_set(
|
44
|
+
argument.name,
|
45
|
+
argument.evaluate(**args).value
|
46
|
+
)
|
46
47
|
else
|
47
48
|
arguments << Object::Argument.new(
|
48
49
|
Object::Dictionary.new(
|
49
|
-
{
|
50
|
+
{argument.name => argument.evaluate(**args).value}
|
50
51
|
)
|
51
52
|
)
|
52
53
|
end
|
data/lib/code/node/decimal.rb
CHANGED
@@ -6,9 +6,7 @@ class Code
|
|
6
6
|
def initialize(parsed)
|
7
7
|
@decimal = parsed.delete(:decimal)
|
8
8
|
|
9
|
-
if parsed.key?(:exponent)
|
10
|
-
@exponent = Node::Statement.new(parsed.delete(:exponent))
|
11
|
-
end
|
9
|
+
@exponent = Node::Statement.new(parsed.delete(:exponent)) if parsed.key?(:exponent)
|
12
10
|
|
13
11
|
super(parsed)
|
14
12
|
end
|
data/lib/code/node/dictionary.rb
CHANGED
@@ -8,7 +8,7 @@ class Code
|
|
8
8
|
if parsed.key?(:statement)
|
9
9
|
@key = Node::Statement.new(parsed.delete(:statement))
|
10
10
|
elsif parsed.key?(:name)
|
11
|
-
@key = Node::String.new([{
|
11
|
+
@key = Node::String.new([{text: parsed.delete(:name)}])
|
12
12
|
end
|
13
13
|
|
14
14
|
@value = Node::Code.new(parsed.delete(:value)) if parsed[:value]
|
data/lib/code/node/if.rb
CHANGED
@@ -32,21 +32,21 @@ class Code
|
|
32
32
|
|
33
33
|
def evaluate(**args)
|
34
34
|
if @first_operator == IF_KEYWORD &&
|
35
|
-
|
35
|
+
@first_statement.evaluate(**args).truthy?
|
36
36
|
@first_body.evaluate(**args)
|
37
37
|
elsif @first_operator == UNLESS_KEYWORD &&
|
38
|
-
|
38
|
+
@first_statement.evaluate(**args).falsy?
|
39
39
|
@first_body.evaluate(**args)
|
40
40
|
else
|
41
41
|
@elses.each do |elses|
|
42
42
|
if elses.operator == ELSIF_KEYWORD &&
|
43
|
-
|
43
|
+
elses.statement.evaluate(**args).truthy?
|
44
44
|
return elses.body.evaluate(**args)
|
45
45
|
elsif elses.operator == IF_KEYWORD &&
|
46
|
-
|
46
|
+
elses.statement.evaluate(**args).truthy?
|
47
47
|
return elses.body.evaluate(**args)
|
48
48
|
elsif elses.operator == UNLESS_KEYWORD &&
|
49
|
-
|
49
|
+
elses.statement.evaluate(**args).falsy?
|
50
50
|
return elses.body.evaluate(**args)
|
51
51
|
elsif elses.operator == ELSE_KEYWORD
|
52
52
|
return elses.body.evaluate(**args)
|
data/lib/code/object/decimal.rb
CHANGED
@@ -9,9 +9,7 @@ class Code
|
|
9
9
|
@raw = BigDecimal(decimal)
|
10
10
|
|
11
11
|
return unless exponent
|
12
|
-
unless exponent.is_a?(Number)
|
13
|
-
raise ::Code::Error::TypeError, "exponent is not a number"
|
14
|
-
end
|
12
|
+
raise ::Code::Error::TypeError, "exponent is not a number" unless exponent.is_a?(Number)
|
15
13
|
|
16
14
|
@raw *= 10**exponent.raw
|
17
15
|
end
|
@@ -215,9 +215,7 @@ class Code
|
|
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
|
218
|
+
arguments.last if arguments.last.is_a?(Function) && arguments.size > 1
|
221
219
|
)
|
222
220
|
|
223
221
|
arguments = arguments[..-2] if default
|
@@ -342,9 +340,7 @@ class Code
|
|
342
340
|
def code_fetch(*arguments, index: Integer.new(0), **globals)
|
343
341
|
default =
|
344
342
|
(
|
345
|
-
if arguments.last.is_a?(Function) && arguments.size > 1
|
346
|
-
arguments.last
|
347
|
-
end
|
343
|
+
arguments.last if arguments.last.is_a?(Function) && arguments.size > 1
|
348
344
|
)
|
349
345
|
|
350
346
|
arguments = arguments[..-2] if default
|
@@ -494,9 +490,7 @@ class Code
|
|
494
490
|
def code_merge(*arguments, **globals)
|
495
491
|
conflict =
|
496
492
|
(
|
497
|
-
if arguments.last.is_a?(Function) && arguments.size > 1
|
498
|
-
arguments.last
|
499
|
-
end
|
493
|
+
arguments.last if arguments.last.is_a?(Function) && arguments.size > 1
|
500
494
|
)
|
501
495
|
|
502
496
|
arguments = arguments[..-2] if conflict
|
data/lib/code/object/function.rb
CHANGED
data/lib/code/object/integer.rb
CHANGED
@@ -10,9 +10,7 @@ class Code
|
|
10
10
|
|
11
11
|
return unless exponent
|
12
12
|
|
13
|
-
unless exponent.is_a?(Number)
|
14
|
-
raise Code::Error::TypeError, "exponent is not a number"
|
15
|
-
end
|
13
|
+
raise Code::Error::TypeError, "exponent is not a number" unless exponent.is_a?(Number)
|
16
14
|
|
17
15
|
@raw *= 10**exponent.raw
|
18
16
|
end
|
@@ -40,15 +38,9 @@ class Code
|
|
40
38
|
when "**", "power"
|
41
39
|
sig(args) { Number }
|
42
40
|
code_power(value)
|
43
|
-
when "+", "plus"
|
44
|
-
sig(args) { Object.maybe }
|
45
|
-
value ? code_plus(value) : self
|
46
41
|
when "+", "plus", "self"
|
47
42
|
sig(args) { Object.maybe }
|
48
43
|
value ? code_plus(value) : code_self
|
49
|
-
when "-", "minus"
|
50
|
-
sig(args) { Number.maybe }
|
51
|
-
value ? code_minus(value) : code_unary_minus
|
52
44
|
when "-", "minus", "unary_minus"
|
53
45
|
sig(args) { Number.maybe }
|
54
46
|
value ? code_minus(value) : code_unary_minus
|
@@ -25,8 +25,8 @@ class Code
|
|
25
25
|
args
|
26
26
|
.select(&:keyword?)
|
27
27
|
.map do |argument|
|
28
|
-
|
29
|
-
|
28
|
+
[argument.name.to_sym, Ruby.from_code(argument.value)]
|
29
|
+
end
|
30
30
|
.to_h
|
31
31
|
|
32
32
|
Ruby.to_code(raw.call(*regular_arguments, **keyword_arguments))
|
data/lib/code/object/string.rb
CHANGED
@@ -68,7 +68,7 @@ class Code
|
|
68
68
|
[
|
69
69
|
{
|
70
70
|
function: {
|
71
|
-
parameters: [{
|
71
|
+
parameters: [{name: "_"}],
|
72
72
|
body: [
|
73
73
|
{
|
74
74
|
left_operation: {
|
@@ -78,7 +78,7 @@ class Code
|
|
78
78
|
}
|
79
79
|
},
|
80
80
|
others: [
|
81
|
-
{
|
81
|
+
{operator: ".", statement: {call: {name: raw}}}
|
82
82
|
]
|
83
83
|
}
|
84
84
|
}
|
data/lib/code/object.rb
CHANGED
@@ -149,9 +149,7 @@ class Code
|
|
149
149
|
end
|
150
150
|
|
151
151
|
def hash
|
152
|
-
unless respond_to?(:raw)
|
153
|
-
raise NotImplementedError, "#{self.class.name}#hash"
|
154
|
-
end
|
152
|
+
raise NotImplementedError, "#{self.class.name}#hash" unless respond_to?(:raw)
|
155
153
|
|
156
154
|
[self.class, raw].hash
|
157
155
|
end
|
data/lib/code/ruby.rb
CHANGED
@@ -54,19 +54,8 @@ class Code
|
|
54
54
|
|
55
55
|
def from_code
|
56
56
|
if code?
|
57
|
-
if code_nothing?
|
58
|
-
|
59
|
-
elsif code_boolean?
|
60
|
-
raw.raw
|
61
|
-
elsif code_decimal?
|
62
|
-
raw.raw
|
63
|
-
elsif code_integer?
|
64
|
-
raw.raw
|
65
|
-
elsif code_nothing?
|
66
|
-
raw.raw
|
67
|
-
elsif code_range?
|
68
|
-
raw.raw
|
69
|
-
elsif code_string?
|
57
|
+
if code_nothing? || code_boolean? || code_decimal? || code_integer? ||
|
58
|
+
code_range? || code_string?
|
70
59
|
raw.raw
|
71
60
|
elsif code_dictionnary?
|
72
61
|
raw
|
@@ -153,10 +142,6 @@ class Code
|
|
153
142
|
raw.is_a?(::Code::Object::Integer)
|
154
143
|
end
|
155
144
|
|
156
|
-
def code_nothing?
|
157
|
-
raw.is_a?(::Code::Object::Nothing)
|
158
|
-
end
|
159
|
-
|
160
145
|
def code_range?
|
161
146
|
raw.is_a?(::Code::Object::Range)
|
162
147
|
end
|
data/lib/code/version.rb
CHANGED
data/spec/code_spec.rb
CHANGED
@@ -141,7 +141,7 @@ RSpec.describe Code do
|
|
141
141
|
end
|
142
142
|
|
143
143
|
it "converts nil" do
|
144
|
-
ruby = Code::Ruby.from_code(Code.evaluate("a", ruby: {
|
144
|
+
ruby = Code::Ruby.from_code(Code.evaluate("a", ruby: {a: nil}))
|
145
145
|
|
146
146
|
expect(ruby).to eq(nil)
|
147
147
|
end
|
@@ -154,7 +154,7 @@ RSpec.describe Code do
|
|
154
154
|
|
155
155
|
it "works with nested objects" do
|
156
156
|
expect(
|
157
|
-
Code.evaluate("items.first.title", ruby: {
|
157
|
+
Code.evaluate("items.first.title", ruby: {items: [{title: "Hello"}]})
|
158
158
|
).to eq(Code.evaluate(":Hello"))
|
159
159
|
end
|
160
160
|
|
@@ -163,7 +163,7 @@ RSpec.describe Code do
|
|
163
163
|
Code.evaluate(
|
164
164
|
"items.map { |item| item.title }",
|
165
165
|
ruby: {
|
166
|
-
items: [{
|
166
|
+
items: [{title: "Hello"}]
|
167
167
|
}
|
168
168
|
)
|
169
169
|
).to eq(Code.evaluate("[:Hello]"))
|
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.
|
4
|
+
version: 0.6.5
|
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-
|
11
|
+
date: 2024-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bigdecimal
|