Class: Rus3::Repl
- Inherits:
-
Object
- Object
- Rus3::Repl
- Defined in:
- lib/rus3/repl.rb
Overview
Provides a very simple Read Eval Print Loop mechanism of Rus3.
Features:
Each evaluated value is recorded into the value history.
_, _last_value : retrieves the last evaluated value
_his(n), _history(n) : retrieves the n-th value in the
history.
_his, _history : prints all values in the history
Short usage:
require "rus3"
Rus3::Repl.start
Constant Summary collapse
- REPL_VERSION =
Indicates the version of the Repl class.
"0.2.1"- COMPONENTS =
Hods major component names of the REPL.
{ :parser => Rubasteme::Parser, :evaluator => Evaluator::DEFAULT_EVALUATOR, :printer => Printer::ChainPrinter, }
- PROMPT =
Prompt for input.
"Rus3> "- FAREWELL_MESSAGE =
A message to print at exitting.
"Bye!"- @@value_history =
:nodoc:
[]
Instance Attribute Summary collapse
-
#prompt ⇒ Object
:nodoc:.
-
#verbose ⇒ Object
:nodoc:.
Class Method Summary collapse
-
.start(evaluator: nil, verbose: false) ⇒ Object
Starts REPL.
Instance Method Summary collapse
-
#greeting ⇒ Object
Shows the greeting message.
-
#initialize(evaluator: nil, verbose: false) ⇒ Repl
constructor
A new instance of Repl.
- #loop ⇒ Object
Constructor Details
#initialize(evaluator: nil, verbose: false) ⇒ Repl
Returns a new instance of Repl.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rus3/repl.rb', line 54 def initialize(evaluator: nil, verbose: false) comps = COMPONENTS.dup comps[:evaluator] = Evaluator.const_get("#{evaluator.capitalize}Evaluator") if evaluator comps.each { |name, klass| instance_variable_set("@#{name}", klass.new) } @prompt = PROMPT @verbose = verbose @evaluator.verbose = verbose @printer.verbose = verbose define_constants define_help_feature define_history_feature define_load_feature greeting end |
Instance Attribute Details
#prompt ⇒ Object
:nodoc:
52 53 54 |
# File 'lib/rus3/repl.rb', line 52 def prompt @prompt end |
#verbose ⇒ Object
:nodoc:
51 52 53 |
# File 'lib/rus3/repl.rb', line 51 def verbose @verbose end |
Class Method Details
Instance Method Details
#greeting ⇒ Object
Shows the greeting message.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/rus3/repl.rb', line 103 def greeting puts "A simple REPL to run Rus3:" return unless @verbose vmsg = "(rus3 :version #{Rus3::VERSION} :release #{Rus3::RELEASE}\n" vmsg += " (repl :version #{REPL_VERSION}\n" comp_vmsgs = [] COMPONENTS.keys.each { |comp_name| comp_vmsgs << " (#{(comp_name)})" } vmsg += comp_vmsgs.join("\n") vmsg += "))" puts vmsg end |
#loop ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/rus3/repl.rb', line 77 def loop msg = Kernel.loop { # LOOP begin source = read(STDIN) # READ break FAREWELL_MESSAGE if source.nil? ast = @parser.parse(Rbscmlex::Lexer.new(source)) rescue SchemeSyntaxError => e puts "ERROR" + (@verbose ? "(READ)" : "") + ": %s" % e next end begin value = @evaluator.eval(ast) # EVAL rescue SyntaxError, StandardError => e puts "ERROR" + (@verbose ? "(EVAL)" : "") + ": %s" % e next end history_push(value) @printer.print(value) # PRINT } puts "#{msg}" unless msg.nil? end |