Class: Repl
- Inherits:
-
Object
- Object
- Repl
- Defined in:
- lib/calculo/repl.rb
Instance Attribute Summary collapse
-
#history ⇒ Object
Returns the value of attribute history.
-
#prompt ⇒ Object
Returns the value of attribute prompt.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #calculo_loop(output = "Welcome to calculo!") ⇒ Object
- #calculo_print(history) ⇒ Object
- #clear_screen ⇒ Object
-
#initialize(prompt, type) ⇒ Repl
constructor
A new instance of Repl.
- #repl_eval(input) ⇒ Object
Constructor Details
#initialize(prompt, type) ⇒ Repl
Returns a new instance of Repl.
9 10 11 12 13 14 15 16 |
# File 'lib/calculo/repl.rb', line 9 def initialize(prompt,type) @history = [] @og_prompt = prompt @prompt = "[#{@history.length}]" + @og_prompt + " " @type = type clear_screen() calculo_loop() end |
Instance Attribute Details
#history ⇒ Object
Returns the value of attribute history.
6 7 8 |
# File 'lib/calculo/repl.rb', line 6 def history @history end |
#prompt ⇒ Object
Returns the value of attribute prompt.
7 8 9 |
# File 'lib/calculo/repl.rb', line 7 def prompt @prompt end |
#type ⇒ Object
Returns the value of attribute type.
8 9 10 |
# File 'lib/calculo/repl.rb', line 8 def type @type end |
Instance Method Details
#calculo_loop(output = "Welcome to calculo!") ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/calculo/repl.rb', line 98 def calculo_loop(output="Welcome to calculo!") input = nil while output != "Exiting calculo!" puts(output) @prompt = "[#{@history.length}]" + @og_prompt + " " print(@prompt) input = gets.chomp output = repl_eval(input) end puts(output) exit(1) end |
#calculo_print(history) ⇒ Object
26 27 28 |
# File 'lib/calculo/repl.rb', line 26 def calculo_print(history) history.each_with_index.map{|x,i| puts("#{i} : #{x}\n")} end |
#clear_screen ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/calculo/repl.rb', line 18 def clear_screen if OS.linux? or OS.mac? or OS.bsd? system('clear') elsif OS.windows? system('cls') end end |
#repl_eval(input) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/calculo/repl.rb', line 30 def repl_eval(input) # syntax for changing prompt/type is: # set prompt/type = 'foo' # or set prompt/type 'foo' if input.start_with?("set","change") @history.push(input) input_array = input.split(' ') if input_array[1] == "prompt" and input_array.last != "prompt" @og_prompt = input_array.last @prompt = "[#{@history.length}]" + @og_prompt + " " return "prompt is now #{@prompt}" elsif input_array[1] == "type" and input_array.last != "type" @type = input_array.last return "type is now #{@type}" else return "Error: variable can not be changed" end # If input is simply history, print all of the history # can also use ruby range syntax, STARTING AT 0 # for example: history 0..-2 will print all but the # last entry of history elsif input.start_with?("history") @history.push(input) if input == "history" return calculo_print(@history) else input_array = input.split(' ') input_range = input_array[-1] if input_range.length == 1 return @history[input_range.to_i] elsif input_range.length == 4 return calculo_print(@history[(input_range.chars.first.to_i)..(input_range.chars.last.to_i)]) elsif input_range.length == 5 return calculo_print(@history[(input_range.chars.first.to_i)..(input_range.chars.drop(2).to_i)]) end end elsif input.start_with?("clear") @history.push(input) if input == "clear" return clear_screen() elsif input == "clear history" @history = [] return "History is now blank" end elsif input == "exit" or input == "quit" return "Exiting calculo!" else if @type == 'lisp' or @type == 'reverse-lisp' or @type == 'reverse' repl_class = Lisp.new(input,@type) calculo_output = "#{repl_class.string} : #{repl_class.result}" @history.push(calculo_output) return calculo_output elsif @type == 'postfix' or @type == 'infix' or @type == 'prefix' or @type == 'pn' or @type == 'rpn' repl_class = MathNotation.new(input,@type) calculo_output = "#{repl_class.string} : #{repl_class.result}" @history.push(calculo_output) return calculo_output end end end |