From: Ben Rho Date: 2010-05-20T09:43:40+09:00 Subject: Re: Interactive Mode BASIC Interpreter (#232) 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 Rho\'s solution to Ruby Quiz #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 -- Posted via http://www.ruby-forum.com/.