Class: PLang::IPS::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/ips/shell.rb

Instance Method Summary collapse

Constructor Details

#initializeShell

Returns a new instance of Shell.



4
5
6
7
8
# File 'lib/ips/shell.rb', line 4

def initialize
  @expr_c = ""
  @interpreter = PLang::VM::Interpreter.new(nil)
  @env = @interpreter.send(:load_basic_environment)
end

Instance Method Details

#execute(expr) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ips/shell.rb', line 24

def execute(expr)
  expr = @expr_c + expr
  sa = PLang::Parser::SyntaxAnalyser.new(expr)
  begin
    ast = sa.parse
    result = nil
    ast.each do |a|
      result = @interpreter.send(:execute, a, @env)
    end
    @expr_c = ""
    show(result)
  rescue Exception => e
    if e.message =~ /sintax error: unexpected '\\n'/
      @expr_c = expr
    else
      @expr_c = ""
      raise e
    end
  end
end

#readObject



49
50
51
52
53
54
55
56
# File 'lib/ips/shell.rb', line 49

def read
  if @expr_c == ""
    print "p-lang > "
  else
    print "...... > "
  end
  gets
end

#show(out) ⇒ Object



45
46
47
# File 'lib/ips/shell.rb', line 45

def show(out)
  puts " => #{out.to_s}"
end

#startObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ips/shell.rb', line 10

def start
  while true
    begin
      execute(read)
    rescue Exception => e
      if e.class == SystemExit
        raise e
      else
        puts "ips:#{e}"
      end
    end
  end
end