code-ruby 0.6.7 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a8a60610e77a80b5fcd90d969b7d9e324670afad9809d5ce1334f0e0ce249f91
4
- data.tar.gz: 5910d5e6441c3ca32b57cb22bb282ef6c019df18256532db99ab3839931d892c
3
+ metadata.gz: 6c1825b3354247a5986c1eb78223a6b16bff33119f38b3f8a55ee1e7b7fb354d
4
+ data.tar.gz: 125688c0383d89e4801b847665952af382326764c3bda9e8e8f52942b65ced73
5
5
  SHA512:
6
- metadata.gz: e79009191406e846bad409795fee979aac6af54e9ae8befdbeb6260e59c5b62c2cc0f5c641179f8d1cdeec000f04734746da44d1821818f28e89e36b1dbf02fe
7
- data.tar.gz: c25b97130a21e805d593c4691a9baecef2b7e19130e3ef3fca945a149e755a5881a128211592658e719c243f7049c5114dfc61be93509c4ea27623b00071043c
6
+ metadata.gz: 8bd85f1991b52e9848e19ac1a019716148788d348f3fbc9d98dbd625f973a6f1a46ea43a853b2b0bdf30d1c611fe12c36c0e6572f29dbac0d4339574409eb03e
7
+ data.tar.gz: f126c93f039f0c8ef0f9229d744dd63e4a996b10847756dc5c67db3ea7cf340a01aea5062e2543df56b50c316205f407bcfe68e318d69e6db0a9c595d33dc621
@@ -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
- io = args.fetch(:io)
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
- io.puts(*arguments.map(&:value).map(&:inspect)) || Nothing.new
63
+ output.puts(*arguments.map(&:value).map(&:inspect))
64
+ Nothing.new
64
65
  when "print"
65
66
  sig(args) { Object.repeat }
66
- io.print(*arguments.map(&:value)) || Nothing.new
67
+ output.print(*arguments.map(&:value))
68
+ Nothing.new
67
69
  when "puts"
68
70
  sig(args) { Object.repeat }
69
- io.puts(*arguments.map(&:value)) || Nothing.new
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
@@ -2,4 +2,4 @@
2
2
 
3
3
  require_relative "../code"
4
4
 
5
- Code::Version = Gem::Version.new("0.6.7")
5
+ Code::Version = Gem::Version.new("0.7.0")
data/lib/code.rb CHANGED
@@ -2,45 +2,51 @@
2
2
 
3
3
  class Code
4
4
  EMPTY_STRING = ""
5
- GLOBALS = %i[io context object].freeze
5
+ GLOBALS = %i[output error context object].freeze
6
6
  DEFAULT_TIMEOUT = 0
7
7
 
8
- def initialize(input, io: StringIO.new, timeout: DEFAULT_TIMEOUT, ruby: {})
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(@input).to_raw }
11
- @io = io
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
- io: StringIO.new,
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, io:, timeout:, ruby:).evaluate(context)
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:, io:, ruby:).code_to_context
40
+ Code.evaluate(context, timeout:, output:, error:, ruby:).code_to_context
33
41
  end
34
42
 
35
- raise(Error::IncompatibleContext) unless context.is_a?(Object::Context)
36
-
37
- context = context.merge(ruby)
43
+ context = ruby.merge(context)
38
44
 
39
- Node::Code.new(parsed).evaluate(context:, io:)
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, :io, :ruby
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
- io = StringIO.new
138
- Code.evaluate(input, io:)
139
- expect(io.string).to eq(expected)
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.6.7
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-08 00:00:00.000000000 Z
11
+ date: 2024-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bigdecimal