Skip to content

Commit

Permalink
* irb 0.9
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2627 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
keiju authored and aycabta committed Jun 5, 2019
1 parent 4639f6c commit 6806669
Show file tree
Hide file tree
Showing 35 changed files with 1,267 additions and 482 deletions.
136 changes: 79 additions & 57 deletions lib/irb.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# irb.rb - irb main module
# $Release Version: 0.7.4 $
# $Release Version: 0.9 $
# $Revision$
# $Date$
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
Expand All @@ -14,7 +14,7 @@
require "irb/init"
require "irb/context"
require "irb/extend-command"
require "irb/workspace"
#require "irb/workspace"

require "irb/ruby-lex"
require "irb/input-method"
Expand Down Expand Up @@ -43,13 +43,15 @@ def IRB.version
@CONF[:VERSION] = format("irb %s(%s)", rv, @LAST_UPDATE_DATE)
end

def IRB.CurrentContext
IRB.conf[:MAIN_CONTEXT]
end

# initialize IRB and start TOP_LEVEL irb
def IRB.start(ap_path = nil)
$0 = File::basename(ap_path, ".rb") if ap_path

IRB.initialize(ap_path)
IRB.parse_opts
IRB.load_modules

if @CONF[:SCRIPT]
irb = Irb.new(nil, @CONF[:SCRIPT])
Expand All @@ -67,7 +69,7 @@ def IRB.start(ap_path = nil)
catch(:IRB_EXIT) do
irb.eval_input
end
print "\n"
# print "\n"
end

def IRB.irb_exit(irb, ret)
Expand All @@ -88,7 +90,7 @@ def IRB.irb_abort(irb, exception = Abort)
class Irb
def initialize(workspace = nil, input_method = nil)
@context = Context.new(self, workspace, input_method)
@context.main.extend ExtendCommand
@context.main.extend ExtendCommandBundle
@signal_status = :IN_IRB

@scanner = RubyLex.new
Expand All @@ -98,20 +100,6 @@ def initialize(workspace = nil, input_method = nil)
attr_accessor :scanner

def eval_input
@scanner.set_input(@context.io) do
signal_status(:IN_INPUT) do
unless l = @context.io.gets
if @context.ignore_eof? and @context.io.readable_atfer_eof?
l = "\n"
if @context.verbose?
printf "Use \"exit\" to leave %s\n", @context.ap_name
end
end
end
l
end
end

@scanner.set_prompt do
|ltype, indent, continue, line_no|
if ltype
Expand All @@ -122,7 +110,11 @@ def eval_input
f = @context.prompt_i
end
f = "" unless f
@context.io.prompt = p = prompt(f, ltype, indent, line_no)
if @context.prompting?
@context.io.prompt = p = prompt(f, ltype, indent, line_no)
else
@context.io.prompt = p = ""
end
if @context.auto_indent_mode
unless ltype
ind = prompt(@context.prompt_i, ltype, indent, line_no).size +
Expand All @@ -133,22 +125,28 @@ def eval_input
end
end

@scanner.set_input(@context.io) do
signal_status(:IN_INPUT) do
if l = @context.io.gets
print l if @context.verbose?
else
if @context.ignore_eof? and @context.io.readable_atfer_eof?
l = "\n"
if @context.verbose?
printf "Use \"exit\" to leave %s\n", @context.ap_name
end
end
end
l
end
end

@scanner.each_top_level_statement do
|line, line_no|
signal_status(:IN_EVAL) do
begin
trace_in do
@context._ = @context.workspace.evaluate(line,
@context.irb_path,
line_no)
# @context._ = irb_eval(line, @context.bind, @context.irb_path, line_no)
end

if @context.inspect?
printf @context.return_format, @context._.inspect
else
printf @context.return_format, @context._
end
@context.evaluate(line, line_no)
output_value if @context.echo?
rescue StandardError, ScriptError, Abort
$! = RuntimeError.new("unknown exception raised") unless $!
print $!.type, ": ", $!, "\n"
Expand Down Expand Up @@ -186,19 +184,44 @@ def eval_input
end
end

# def irb_eval(line, bind, path, line_no)
# id, str = catch(:IRB_TOPLEVEL_EVAL){
# return eval(line, bind, path, line_no)
# }
# case id
# when :EVAL_TOPLEVEL
# eval(str, bind, "(irb_internal)", 1)
# when :EVAL_CONTEXT
# @context.instance_eval(str)
# else
# IRB.fail IllegalParameter
# end
# end
def suspend_name(path = nil, name = nil)
@context.irb_path, back_path = path, @context.irb_path if path
@context.irb_name, back_name = name, @context.irb_name if name
begin
yield back_path, back_name
ensure
@context.irb_path = back_path if path
@context.irb_name = back_name if name
end
end

def suspend_workspace(workspace)
@context.workspace, back_workspace = workspace, @context.workspace
begin
yield back_workspace
ensure
@context.workspace = back_workspace
end
end

def suspend_input_method(input_method)
back_io = @context.io
@context.instance_eval{@io = input_method}
begin
yield back_io
ensure
@context.instance_eval{@io = back_io}
end
end

def suspend_context(context)
@context, back_context = context, @context
begin
yield back_context
ensure
@context = back_context
end
end

def signal_handle
unless @context.ignore_sigint?
Expand Down Expand Up @@ -233,15 +256,6 @@ def signal_status(status)
end
end

def trace_in
Tracer.on if @context.use_tracer?
begin
yield
ensure
Tracer.off if @context.use_tracer?
end
end

def prompt(prompt, ltype, indent, line_no)
p = prompt.dup
p.gsub!(/%([0-9]+)?([a-zA-Z])/) do
Expand Down Expand Up @@ -273,6 +287,14 @@ def prompt(prompt, ltype, indent, line_no)
p
end

def output_value
if @context.inspect?
printf @context.return_format, @context.last_value.inspect
else
printf @context.return_format, @context.last_value
end
end

def inspect
ary = []
for iv in instance_variables
Expand All @@ -296,8 +318,8 @@ def @CONF.inspect
array = []
for k, v in sort{|a1, a2| a1[0].id2name <=> a2[0].id2name}
case k
when :MAIN_CONTEXT
next
when :MAIN_CONTEXT, :__TMP__EHV__
array.push format("CONF[:%s]=...myself...", k.id2name)
when :PROMPT
s = v.collect{
|kk, vv|
Expand Down
33 changes: 33 additions & 0 deletions lib/irb/cmd/chws.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# change-ws.rb -
# $Release Version: 0.9$
# $Revision$
# $Date$
# by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd)
#
# --
#
#
#

require "irb/cmd/nop.rb"
require "irb/ext/change-ws.rb"

module IRB
module ExtendCommand

class CurrentWorkingWorkspace<Nop
def execute(*obj)
irb_context.main
end
end

class ChangeWorkspace<Nop
def execute(*obj)
irb_context.change_workspace(*obj)
irb_context.main
end
end
end
end

25 changes: 25 additions & 0 deletions lib/irb/cmd/fork.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

module IRB
module ExtendCommand
class Fork<Nop
def execute(&block)
pid = send ExtendCommand.irb_original_method_name("fork")
unless pid
class<<self
alias_method :exit, ExtendCommand.irb_original_method_name('exit')
end
if iterator?
begin
yield
ensure
exit
end
end
end
pid
end
end
end
end


67 changes: 67 additions & 0 deletions lib/irb/cmd/load.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#
# load.rb -
# $Release Version: 0.9$
# $Revision$
# $Date$
# by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd)
#
# --
#
#
#

require "irb/cmd/nop.rb"
require "irb/ext/loader"

module IRB
module ExtendCommand
class Load<Nop
include IrbLoader

def execute(file_name, priv = nil)
# return ruby_load(file_name) unless IRB.conf[:USE_LOADER]
return irb_load(file_name, priv)
end
end

class Require<Nop
include IrbLoader

def execute(file_name)
# return ruby_require(file_name) unless IRB.conf[:USE_LOADER]

rex = Regexp.new("#{Regexp.quote(file_name)}(\.o|\.rb)?")
return false if $".find{|f| f =~ rex}

case file_name
when /\.rb$/
begin
if irb_load(file_name)
$".push file_name
return true
end
rescue LoadError
end
when /\.(so|o|sl)$/
return ruby_require(file_name)
end

begin
irb_load(f = file_name + ".rb")
$".push f
return true
rescue LoadError
return ruby_require(file_name)
end
end
end

class Source<Nop
include IrbLoader
def execute(file_name)
source_file(file_name)
end
end
end

end
39 changes: 39 additions & 0 deletions lib/irb/cmd/nop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#
# nop.rb -
# $Release Version: 0.9$
# $Revision$
# $Date$
# by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd)
#
# --
#
#
#
module IRB
module ExtendCommand
class Nop

@RCS_ID='-$Id$-'

def self.execute(conf, *opts)
command = new(conf)
command.execute(*opts)
end

def initialize(conf)
@irb_context = conf
end

attr_reader :irb_context

def irb
@irb_context.irb
end

def execute(*opts)
#nop
end
end
end
end

Loading

0 comments on commit 6806669

Please sign in to comment.