Class: Rus3::Repl

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#promptObject

:nodoc:



52
53
54
# File 'lib/rus3/repl.rb', line 52

def prompt
  @prompt
end

#verboseObject

:nodoc:



51
52
53
# File 'lib/rus3/repl.rb', line 51

def verbose
  @verbose
end

Class Method Details

.start(evaluator: nil, verbose: false) ⇒ Object

Starts REPL.



29
30
31
32
# File 'lib/rus3/repl.rb', line 29

def start(evaluator: nil, verbose: false)
  repl = Repl.new(evaluator: evaluator, verbose: verbose)
  repl.loop
end

Instance Method Details

#greetingObject

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 << "    (#{version_message(comp_name)})"
  }
  vmsg += comp_vmsgs.join("\n")
  vmsg += "))"

  puts vmsg
end

#loopObject



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