code-ruby 0.6.7 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/code/object/global.rb +7 -4
- data/lib/code/parser.rb +1 -1
- data/lib/code/version.rb +1 -1
- data/lib/code.rb +20 -14
- 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: f013740cc5bbbf2e3d0d364dc987ed62d34d85d38b4af848d4c74a7110e6ad16
|
4
|
+
data.tar.gz: adf22e7e5b35c2cda0e112797768af9f2840edd9425a2a4ea87d510698d27754
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc543960e9c707f39753e9931c85f35508cc97196ddee418165bf632ceea29a2f48c076e080ad3dba0ea4080a92ce17c262f24e5686083b6051d57da157b7f96
|
7
|
+
data.tar.gz: 965a1ecebb0486ab5bca33b9c35d0303b2a9204d079675516d10a52ccd10c6104003336dedd0f5d82f8e35ef805dd749ea805a5b4890c43b76f99903e3aa9d1f
|
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/parser.rb
CHANGED
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.1
|
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
|