code-ruby 0.6.6 → 0.7.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 +4 -4
- data/Gemfile.lock +1 -1
- data/code-ruby.gemspec +1 -1
- data/lib/code/object/global.rb +7 -4
- data/lib/code/version.rb +1 -1
- data/lib/code.rb +20 -14
- data/spec/code_spec.rb +3 -3
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6c1825b3354247a5986c1eb78223a6b16bff33119f38b3f8a55ee1e7b7fb354d
|
|
4
|
+
data.tar.gz: 125688c0383d89e4801b847665952af382326764c3bda9e8e8f52942b65ced73
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8bd85f1991b52e9848e19ac1a019716148788d348f3fbc9d98dbd625f973a6f1a46ea43a853b2b0bdf30d1c611fe12c36c0e6572f29dbac0d4339574409eb03e
|
|
7
|
+
data.tar.gz: f126c93f039f0c8ef0f9229d744dd63e4a996b10847756dc5c67db3ea7cf340a01aea5062e2543df56b50c316205f407bcfe68e318d69e6db0a9c595d33dc621
|
data/Gemfile.lock
CHANGED
data/code-ruby.gemspec
CHANGED
|
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
|
|
|
12
12
|
s.email = "dorian@dorianmarie.fr"
|
|
13
13
|
s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
|
14
14
|
s.require_paths = ["lib"]
|
|
15
|
-
s.homepage = "https://github.com/
|
|
15
|
+
s.homepage = "https://github.com/dorianmariecom/code-ruby"
|
|
16
16
|
s.license = "MIT"
|
|
17
17
|
s.executables = "code"
|
|
18
18
|
|
data/lib/code/object/global.rb
CHANGED
|
@@ -10,7 +10,7 @@ class Code
|
|
|
10
10
|
def call(**args)
|
|
11
11
|
operator = args.fetch(:operator, nil)
|
|
12
12
|
arguments = args.fetch(:arguments, [])
|
|
13
|
-
|
|
13
|
+
output = args.fetch(:output)
|
|
14
14
|
context = args.fetch(:context)
|
|
15
15
|
multi_fetch(args, *GLOBALS)
|
|
16
16
|
value = arguments.first&.value
|
|
@@ -60,13 +60,16 @@ class Code
|
|
|
60
60
|
Code.evaluate(value.to_s)
|
|
61
61
|
when "p"
|
|
62
62
|
sig(args) { Object.repeat }
|
|
63
|
-
|
|
63
|
+
output.puts(*arguments.map(&:value).map(&:inspect))
|
|
64
|
+
Nothing.new
|
|
64
65
|
when "print"
|
|
65
66
|
sig(args) { Object.repeat }
|
|
66
|
-
|
|
67
|
+
output.print(*arguments.map(&:value))
|
|
68
|
+
Nothing.new
|
|
67
69
|
when "puts"
|
|
68
70
|
sig(args) { Object.repeat }
|
|
69
|
-
|
|
71
|
+
output.puts(*arguments.map(&:value))
|
|
72
|
+
Nothing.new
|
|
70
73
|
else
|
|
71
74
|
context = context.lookup!(operator)
|
|
72
75
|
result = context.code_fetch(operator)
|
data/lib/code/version.rb
CHANGED
data/lib/code.rb
CHANGED
|
@@ -2,45 +2,51 @@
|
|
|
2
2
|
|
|
3
3
|
class Code
|
|
4
4
|
EMPTY_STRING = ""
|
|
5
|
-
GLOBALS = %i[
|
|
5
|
+
GLOBALS = %i[output error context object].freeze
|
|
6
6
|
DEFAULT_TIMEOUT = 0
|
|
7
7
|
|
|
8
|
-
def initialize(
|
|
8
|
+
def initialize(
|
|
9
|
+
input,
|
|
10
|
+
output: StringIO.new,
|
|
11
|
+
error: StringIO.new,
|
|
12
|
+
timeout: DEFAULT_TIMEOUT,
|
|
13
|
+
ruby: {}
|
|
14
|
+
)
|
|
9
15
|
@input = input
|
|
10
|
-
@parsed = Timeout.timeout(timeout) { ::Code::Parser.parse(
|
|
11
|
-
@
|
|
16
|
+
@parsed = Timeout.timeout(timeout) { ::Code::Parser.parse(input).to_raw }
|
|
17
|
+
@output = output
|
|
18
|
+
@error = error
|
|
12
19
|
@timeout = timeout || DEFAULT_TIMEOUT
|
|
13
20
|
@ruby = ::Code::Ruby.to_code(ruby || {}).code_to_context
|
|
14
21
|
end
|
|
15
22
|
|
|
16
23
|
def self.evaluate(
|
|
17
24
|
input,
|
|
18
|
-
context =
|
|
19
|
-
|
|
25
|
+
context = EMPTY_STRING,
|
|
26
|
+
output: StringIO.new,
|
|
27
|
+
error: StringIO.new,
|
|
20
28
|
timeout: DEFAULT_TIMEOUT,
|
|
21
29
|
ruby: {}
|
|
22
30
|
)
|
|
23
|
-
new(input,
|
|
31
|
+
new(input, output:, error:, timeout:, ruby:).evaluate(context)
|
|
24
32
|
end
|
|
25
33
|
|
|
26
|
-
def evaluate(context =
|
|
34
|
+
def evaluate(context = EMPTY_STRING)
|
|
27
35
|
Timeout.timeout(timeout) do
|
|
28
36
|
context =
|
|
29
37
|
if context == EMPTY_STRING
|
|
30
38
|
Object::Context.new
|
|
31
39
|
else
|
|
32
|
-
Code.evaluate(context, timeout:,
|
|
40
|
+
Code.evaluate(context, timeout:, output:, error:, ruby:).code_to_context
|
|
33
41
|
end
|
|
34
42
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
context = context.merge(ruby)
|
|
43
|
+
context = ruby.merge(context)
|
|
38
44
|
|
|
39
|
-
Node::Code.new(parsed).evaluate(context:,
|
|
45
|
+
Node::Code.new(parsed).evaluate(context:, output:, error:)
|
|
40
46
|
end
|
|
41
47
|
end
|
|
42
48
|
|
|
43
49
|
private
|
|
44
50
|
|
|
45
|
-
attr_reader :input, :parsed, :timeout, :
|
|
51
|
+
attr_reader :input, :parsed, :timeout, :output, :error, :ruby
|
|
46
52
|
end
|
data/spec/code_spec.rb
CHANGED
|
@@ -134,9 +134,9 @@ RSpec.describe Code do
|
|
|
134
134
|
|
|
135
135
|
[["puts(true)", "true\n"], %w[print(false) false]].each do |input, expected|
|
|
136
136
|
it "#{input} prints #{expected}" do
|
|
137
|
-
|
|
138
|
-
Code.evaluate(input,
|
|
139
|
-
expect(
|
|
137
|
+
output = StringIO.new
|
|
138
|
+
Code.evaluate(input, output:)
|
|
139
|
+
expect(output.string).to eq(expected)
|
|
140
140
|
end
|
|
141
141
|
end
|
|
142
142
|
|
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.
|
|
4
|
+
version: 0.7.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-02-
|
|
11
|
+
date: 2024-02-11 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bigdecimal
|
|
@@ -187,7 +187,7 @@ files:
|
|
|
187
187
|
- spec/code/type_spec.rb
|
|
188
188
|
- spec/code_spec.rb
|
|
189
189
|
- spec/spec_helper.rb
|
|
190
|
-
homepage: https://github.com/
|
|
190
|
+
homepage: https://github.com/dorianmariecom/code-ruby
|
|
191
191
|
licenses:
|
|
192
192
|
- MIT
|
|
193
193
|
metadata: {}
|