Class: Calvin::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/calvin/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



3
4
5
6
7
8
9
# File 'lib/calvin/cli.rb', line 3

def initialize(argv)
  if argv.any?
    puts "Argument passed: #{argv.inspect}"
  end
  @verbose = argv.include?("--verbose")
  repl
end

Instance Method Details

#replObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/calvin/cli.rb', line 11

def repl
  evaluator = Evaluator.new
  require 'readline'

  puts "Calvin Programming Language REPL."
  puts "Type exit or press Ctrl-D to exit."
  lines = ""
  loop do
    prompt = "> "
    line = Readline::readline(prompt)

    exit if line.nil? || line == "exit"

    Readline::HISTORY.push line

    begin
      ast = AST.new(line).ast

      if @verbose
        puts  "      AST: #{ast.inspect}"
        print "Evaluated: "
      end

      p evaluator.apply(ast)[0]
    rescue Exception => e
      puts e
      p e.message
      puts e.backtrace
    end
  end
end