# Interactive Mode BASIC Interpreter (#232)

**URL:** <https://www.ruby-forum.com/t/interactive-mode-basic-interpreter-232/188232>\
**Category:** Ruby\
**Created:** [May 8, 2010, 2:51am UTC](https://www.ruby-forum.com/t/interactive-mode-basic-interpreter-232/188232 "2010-05-08T02:51:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [May 8, 2010, 2:51am UTC](https://www.ruby-forum.com/t/interactive-mode-basic-interpreter-232/188232/1 "2010-05-08T02:51:16Z")

</div>

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The three rules of Ruby Q.:

1. Please do not post any solutions or spoiler discussion for this  
quiz until 48 hours have elapsed from the time this message was  
sent.

2. Support Ruby Q. by submitting ideas and responses  
as often as you can.

3. 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](http://rubyquiz.strd6.com/quizzes.rss)

Suggestions?: [http://rubyquiz.strd6.com/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](http://rubyquiz.strd6.com/quizzes/228_Ruby_Basic.zip)

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [May 20, 2010, 2:43am UTC](https://www.ruby-forum.com/t/interactive-mode-basic-interpreter-232/188232/2 "2010-05-20T02:43:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [May 20, 2010, 9:47am UTC](https://www.ruby-forum.com/t/interactive-mode-basic-interpreter-232/188232/3 "2010-05-20T09:47:51Z")

</div>

On Thu, May 20, 2010 at 2:43 AM, Ben R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [May 22, 2010, 3:01am UTC](https://www.ruby-forum.com/t/interactive-mode-basic-interpreter-232/188232/4 "2010-05-22T03:01:52Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [May 23, 2010, 7:11pm UTC](https://www.ruby-forum.com/t/interactive-mode-basic-interpreter-232/188232/5 "2010-05-23T19:11:23Z")

</div>

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
