class Keisan::Repl
Constants
- COMMANDS
Attributes
calculator[R]
Public Class Methods
new()
click to toggle source
# File lib/keisan/repl.rb, line 16 def initialize @running = true initialize_completion_commands reset end
Public Instance Methods
get_command()
click to toggle source
# File lib/keisan/repl.rb, line 36 def get_command Readline.readline("keisan> ", true) end
output_error(error)
click to toggle source
# File lib/keisan/repl.rb, line 68 def output_error(error) puts CodeRay.encode(error.class.to_s, :ruby, :terminal) + ": " + CodeRay.encode("\"#{error.message}\"", :ruby, :terminal) end
output_functions()
click to toggle source
# File lib/keisan/repl.rb, line 78 def output_functions function_registry.locals.each do |name, function| puts CodeRay.encode("#{name}(#{function.arguments.join(', ')}) = #{function.expression.to_s}", :ruby, :terminal) end end
output_result(result)
click to toggle source
# File lib/keisan/repl.rb, line 64 def output_result(result) puts "=> " + CodeRay.encode(result.to_s, :ruby, :terminal) end
output_variables()
click to toggle source
# File lib/keisan/repl.rb, line 72 def output_variables variable_registry.locals.each do |name, variable| puts CodeRay.encode("#{name} = #{variable.value}", :ruby, :terminal) end end
process_command(command)
click to toggle source
# File lib/keisan/repl.rb, line 40 def process_command(command) command = command.strip return if command.empty? case command when /\Areset\z/i reset when /\Aquit\z/i @running = false when /\Avariables\z/i output_variables when /\Afunctions\z/i output_functions when /\Aallow_recursive\z/i calculator.allow_recursive! else begin output_result calculator.simplify(command) rescue StandardError => error output_error error end end end
reset()
click to toggle source
# File lib/keisan/repl.rb, line 22 def reset @calculator = Calculator.new end
start()
click to toggle source
# File lib/keisan/repl.rb, line 26 def start while @running command = get_command # ctrl-d should break out break if command.nil? process_command command end end
Private Instance Methods
context()
click to toggle source
# File lib/keisan/repl.rb, line 86 def context calculator.context end
function_registry()
click to toggle source
# File lib/keisan/repl.rb, line 94 def function_registry context.function_registry end
initialize_completion_commands()
click to toggle source
# File lib/keisan/repl.rb, line 98 def initialize_completion_commands completion_proc = Proc.new { |s| COMMANDS.grep(/^#{Regexp.escape(s)}/) } Readline.completion_append_character = " " Readline.completion_proc = completion_proc end
variable_registry()
click to toggle source
# File lib/keisan/repl.rb, line 90 def variable_registry context.variable_registry end