Skip to content

Commit 26d37ae

Browse files
committed
Refactor eval()
* clbustos#17 is included (restoring original INT handler) * TODO: I18N (multi-byte string)
1 parent 5973181 commit 26d37ae

File tree

1 file changed

+36
-44
lines changed

1 file changed

+36
-44
lines changed

lib/rinruby.rb

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -218,54 +218,46 @@ def quit
218218

219219
def eval(string, echo_override=nil)
220220
raise EngineClosed if @engine.closed?
221-
echo_enabled = ( echo_override != nil ) ? echo_override : @echo_enabled
222-
if complete?(string)
223-
@writer.puts string
224-
@writer.puts "warning('#{RinRuby_Stderr_Flag}',immediate.=TRUE)" if @echo_stderr
225-
@writer.puts "print('#{RinRuby_Eval_Flag}')"
226-
else
227-
raise ParseError, "Parse error on eval:#{string}"
228-
end
229-
Signal.trap('INT') do
230-
@writer.print ''
221+
raise ParseError, "Parse error on eval:#{string}" unless complete?(string)
222+
223+
@writer.puts string
224+
@writer.puts "warning('#{RinRuby_Stderr_Flag}',immediate.=TRUE)" if @echo_stderr
225+
@writer.puts "print('#{RinRuby_Eval_Flag}')"
226+
227+
int_handler_orig = Signal.trap('INT'){
228+
@writer.print [0x03].pack('C')
231229
@reader.gets if @platform !~ /java/
232-
Signal.trap('INT') do
233-
end
234-
return true
230+
Signal.trap('INT', nil) # ignore signal
231+
}
232+
233+
echo_proc = proc{}
234+
if ((echo_override == nil) ? @echo_enabled : echo_override) then
235+
echo_proc = (@platform !~ /windows/) \
236+
? proc{|line| puts line; $stdout.flush} \
237+
: proc{|line| puts line}
235238
end
236-
found_eval_flag = false
237-
found_stderr_flag = false
238-
while true
239-
echo_eligible = true
240-
begin
241-
line = @reader.gets
242-
rescue
243-
return false
244-
end
245-
if ! line
246-
return false
247-
end
248-
while line.chomp!
249-
end
250-
line = line[8..-1] if line[0] == 27 # Delete escape sequence
251-
if line == "[1] \"#{RinRuby_Eval_Flag}\""
252-
found_eval_flag = true
253-
echo_eligible = false
254-
end
255-
if line == "Warning: #{RinRuby_Stderr_Flag}"
256-
found_stderr_flag = true
257-
echo_eligible = false
258-
end
259-
break if found_eval_flag && ( found_stderr_flag == @echo_stderr )
260-
return false if line == RinRuby_Exit_Flag
261-
if echo_enabled && echo_eligible
262-
puts line
263-
$stdout.flush if @platform !~ /windows/
239+
240+
res = false
241+
begin
242+
while (line = @reader.gets)
243+
# TODO I18N; force_encoding('origin').encode('UTF-8')
244+
while line.chomp!; end
245+
line = line[8..-1] if line[0] == 27 # delete escape sequence
246+
case line
247+
when "[1] \"#{RinRuby_Eval_Flag}\""
248+
res = true
249+
break
250+
when "Warning: #{RinRuby_Stderr_Flag}"
251+
next
252+
when RinRuby_Exit_Flag
253+
break
254+
end
255+
echo_proc.call(line)
264256
end
257+
ensure
258+
Signal.trap('INT', int_handler_orig)
265259
end
266-
Signal.trap('INT') do
267-
end
268-
true
260+
res
269261
end
270262

271263
#When sending code to Ruby using an interactive prompt, this method will change the prompt to an R prompt. From the R prompt commands can be sent to R exactly as if the R program was actually running. When the user is ready to return to Ruby, then the command exit() will return the prompt to Ruby. This is the ideal situation for the explorative programmer who needs to run several lines of code in R, and see the results after each command. This is also an easy way to execute loops without the use of a here document. It should be noted that the prompt command does not work in a script, just Ruby's interactive irb.

0 commit comments

Comments
 (0)