-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
The three rules of Ruby Q.:
-
Please do not post any solutions or spoiler discussion for this
quiz until 48 hours have elapsed from the time this message was
sent.
-
Support Ruby Q. by submitting ideas and responses
as often as you can.
-
Enjoy!
Suggestion: A [QUIZ] in the subject of emails about the problem
helps everyone on Ruby T. follow the discussion. Please reply to
the original quiz message, if you can.
RSS Feed: http://rubyquiz.strd6.com/quizzes.rss
Suggestions?: http://rubyquiz.strd6.com/suggestions
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Interactive Mode BASIC Interpreter (#232)
Ellohay Rubyists,
This quiz is courtesy of Jean L…
This week’s quiz is to extend the BASIC interpreter from quiz
#228[1][2] so that it can start in interactive mode.
In interactive mode, we should be able to type following commands:
- ‘load’ a program,
- ‘list’ the loaded file,
- ‘run’ it,
- ‘quit’ the interpreter,
- ‘debug’ the program.
Once debugging, the user should be able to set breakpoints, display
the active breakpoints, display a variable value or all the variables,
remove breakpoints and step the program.
Optionally, breakpoints could be conditional, user could change the
variable values, and more.
Have fun!
Code:
[2]: http://rubyquiz.strd6.com/quizzes/228_Ruby_Basic.zip
Daniel X Moore wrote:
- ‘list’ the loaded file
Not quite sure what this means, but I took it to mean “display the path
of the loaded file”
I’m almost done, just working on debug now.
Right now it only has the commands run, list, help, debug, quit, unload,
and load - should it be like irb? That’d be a lot harder…
Either way, I’ll repost it when debug is done (or maybe/probably I’ll
never finish it, about to move to Canada for a while)
Delete everything after the “class BasicString” section and append the
following code:
if $0 == FILE
basic = BasicInterpreter.new
if ARGV[0]
basic.run_file ARGV[0]
else
command_list = {‘help’ => “receive ‘help’”, ‘quit’ => “‘quit’ the
interpreter”, ‘load’ => “‘load’ a program”, ‘unload’ => “‘unload’ a
program”, ‘run’ => “‘run’ the loaded file”, ‘list’ => “‘list’ the loaded
file”, ‘debug’ => “‘debug’ the program”}
puts ‘Ruby BASIC - Interactive Mode’
loop do
print '>> ’
if((command = gets.chomp) == ‘’) then
next
end
if((command[/ /].nil?)) then
if(!(command_list.has_key?(command))) then
puts ‘Invalid command - type “help commands” for help’
next
end
else
if(!(command_list.has_key?(command[/.+? /][/[^ ]+/]))) then
puts ‘Invalid command - type “help commands” for help’
next
end
end
if(command[/.+? /] == 'help ’ || command == ‘help’) then
if(command == ‘help’) then
puts 'Ben R.‘s solution to Ruby Q. #232 - Interactive
BASIC Interpreter’
next
end
if(command == ‘help commands’) then
print ‘The commands are: ’
command_list.each_key{|a|print a+’ '}
puts ‘(type "help " for more info)’
next
end
if(command_list.has_key?(command[/ (.)/][/[^ ]+/])) then
puts command_list[command[/ (.)/][/[^ ]+/]]
next
end
puts ‘That command does not exist.’
next
end
if(command[/.+? /] == 'quit ’ || command == ‘quit’) then
puts ‘Goodbye.’
pause >nul
Kernel.exit!
end
if(command[/.+? /] == 'load ’ || command == ‘load’) then
if(command[/ /].nil?) then
begin
print 'Which file should I load? ’
loaded_file = gets.chomp
end until(File.exists?(loaded_file))
puts “File ‘#{loaded_file}’ loaded.”
else
if(File.exists?(command[/ .+/][/[^ ]+ ?([^ ]* ?)/])) then
loaded_file = command[/ .+/][/[^ ]+ ?([^ ] ?)/]
puts “File ‘#{loaded_file}’ loaded.”
else
puts ‘File not found.’
puts command[/ .+/][/[^ ]+ ?([^ ] ?)*/]
end
end
next
end
if(command[/.+? /] == 'unload ’ || command == ‘unload’) then
if(command[/ /].nil?) then
loaded_file = nil
else
if(command[/.+? /][/[^ ]+/] == loaded_file) then
loaded_file = nil
else
puts “I don’t have that file loaded!”
puts "Loaded file: "+loaded_file
next
end
end
next
end
if(command[/.+? /] == 'run ’ || command == ‘run’) then
if(command[/ /].nil? && loaded_file.nil?) then
begin
print 'Which file should I load and run? ’
loaded_file = gets.chomp
end until(File.exists?(loaded_file))
elsif(loaded_file.nil?)
loaded_file = command[/.+? /][/[^ ]+/]
end
puts “Running file ‘#{loaded_file}’.”
basic.run_file(loaded_file)
next
end
if(command[/.+? /] == 'list ’ || command == ‘list’) then
puts(loaded_file || ‘No file loaded’)
end
if(command[/.+? /] == 'debug ’ || command == ‘debug’) then
puts ‘Unsupported.’
next
end
end
end
end
On Thu, May 20, 2010 at 2:43 AM, Ben R. [email protected] wrote:
Daniel X Moore wrote:
- ‘list’ the loaded file
Not quite sure what this means, but I took it to mean “display the path
of the loaded file”
I think it could mean “show the source”, maybe?
Jesus.
Jesús Gabriel y Galán wrote:
I think it could mean “show the source”, maybe?
I sort of thought that too, but I’ll just stick with what I have -
someone can open their text editor of choice and view it there. Any
input about irb/non-irb?
Thanks for replying 
Ben R. wrote:
Jesús Gabriel y Galán wrote:
I think it could mean “show the source”, maybe?
I sort of thought that too, but I’ll just stick with what I have -
someone can open their text editor of choice and view it there. Any
input about irb/non-irb?
Thanks for replying 
The meaning of the ‘list’ command is indeed: show the code of the
currently load basic script.
JEan