Sha256: 0939b6d778342be452854415e91c358fe4a2d878296ac4ffcef39b4af7e3bbad

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

module PLang
  module IPS # = Interactive P Shell
    class Shell
      def initialize
        @expr_c = ""
        @interpreter = PLang::VM::Interpreter.new(nil)
        @env = @interpreter.send(:load_basic_environment)
      end

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

      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

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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
p-lang-0.3.1 lib/ips/shell.rb
p-lang-0.3.0 lib/ips/shell.rb