Class: NCPP::NCPPFileInterpreter

Inherits:
Interpreter show all
Defined in:
lib/ncpp/interpreter.rb

Instance Method Summary collapse

Methods inherited from Interpreter

#call, #commands, #def_command, #def_variable, #eval_expr, #eval_str, #get_binding, #get_cacheable_cache, #get_command, #get_new_commands, #get_new_variables, #get_variable, #node_impure?, #unknown_command_error, #unknown_variable_error, #variables

Constructor Details

#initialize(cmd_prefix = COMMAND_PREFIX, extra_cmds = {}, extra_vars = {}, safe: false, puritan: false, no_cache: false, cmd_cache: {}) ⇒ NCPPFileInterpreter

Returns a new instance of NCPPFileInterpreter.



848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
# File 'lib/ncpp/interpreter.rb', line 848

def initialize(cmd_prefix = COMMAND_PREFIX, extra_cmds = {}, extra_vars = {}, safe: false, puritan: false,
               no_cache: false, cmd_cache: {})
  super(cmd_prefix, extra_cmds, extra_vars, safe: safe, puritan: puritan, no_cache: no_cache, cmd_cache: cmd_cache)

  @running = true

  @exit_code = 0

  @commands.merge!({
    write: ->(x, filename)  {
      File.open(filename, "w") do |file|
        @out_stack << x unless x.nil?
        file.write(@out_stack.join("\n"))
        @out_stack.clear
        nil
      end
    }.impure
      .describe(
      "Writes the contents of the out-stack and the given argument to the file specified. " \
      "This command is unique to the REPL interpreter."
    ),

    read: ->(filename) {
      raise "File not found: #{filename}" unless File.exist? filename
      File.read(filename)
    }.returns(String).impure
      .describe('Reads the file given and returns its contents as a String.'),

    read_lines: ->(filename) {
      raise "File not found: #{filename}" unless File.exist? filename
      File.readlines(filename)
    }.returns(Array).impure
      .describe('Reads the file specified and returns an Array containing each line.'),

    read_bytes: ->(filename) {
      raise "File not found: #{filename}" unless File.exist? filename
      File.binread(filename).bytes
    }.returns(Array).impure
      .describe('Reads the file specified and returns an Array containing each byte.'),

     exit: ->(exit_code = 0) { @exit_code = exit_code; @running = false }.impure
      .describe('Exits the program.')
  })

end

Instance Method Details

#run(ncpp_file, debug: false) ⇒ Object



894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
# File 'lib/ncpp/interpreter.rb', line 894

def run(ncpp_file, debug: false)
  File.readlines(ncpp_file).each do |line|
    begin
      line = line.strip
      next if line.empty? || line.start_with?('//')

      eval_str(line)

      break unless @running

    rescue Parslet::ParseFailed => e
      puts 'ERROR'.underline_red + ": #{e.parse_failure_cause.ascii_tree}".red
      @exit_code = 1
      break
    rescue Exception => e
      puts 'ERROR'.underline_red + ": #{debug ? e.detailed_message : e.to_s}".red
      @exit_code = 1
      break
    end
  end

  @exit_code
end