Class: Taoism::Console
- Inherits:
-
Object
- Object
- Taoism::Console
- Defined in:
- lib/taoism/console.rb
Constant Summary collapse
- PS1 =
'taoism:%03d> '- PS2 =
'taoism:%03d* '
Instance Method Summary collapse
- #evaluate(input) ⇒ Object
-
#initialize ⇒ Console
constructor
A new instance of Console.
- #read_input ⇒ Object
- #run ⇒ Object
- #unclosed?(input) ⇒ Boolean
Constructor Details
Instance Method Details
#evaluate(input) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/taoism/console.rb', line 50 def evaluate(input) lexer = Lexer.new(input) parser = Parser.new(lexer) ast = parser.parse if parser.errors.empty? result = @evaluator.evaluate(ast) puts Runtime.repr(result) else parser.errors.each do |err| $stderr.puts "Error: #{err}" end end rescue Runtime::Error, Runtime::Return, Runtime::Leave => err $stderr.puts "Error: #{err.}" end |
#read_input ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/taoism/console.rb', line 23 def read_input ps1 = PS1 % @line_no ps2 = PS2 % @line_no input = Readline.readline(ps1, true) return if input.nil? input = input.chomp had_continuation = false while unclosed?(input) had_continuation = true line = Readline.readline(ps2, false) break unless line input << "\n" << line.chomp end if had_continuation Readline::HISTORY.pop Readline::HISTORY << input end @line_no += 1 input end |
#run ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/taoism/console.rb', line 11 def run require 'readline' loop do input = read_input break unless input break if input.strip == 'exit' evaluate(input) end end |
#unclosed?(input) ⇒ Boolean
68 69 70 |
# File 'lib/taoism/console.rb', line 68 def unclosed?(input) input.count('{') > input.count('}') end |