class TracLang::Executor

Executable TRAC commands

Public Class Methods

new(d = nil) click to toggle source

Initialize executor, giving block to store forms in

# File lib/trac_lang/executor.rb, line 8
def initialize(d = nil)
  @parser = Parser.new
  @dispatch = d || Dispatch.new
end

Public Instance Methods

execute(str) click to toggle source

Executes a string of TRAC. If we are in trace mode, wait for user input after executing string.

# File lib/trac_lang/executor.rb, line 83
def execute(str)
  @parser.parse(str) do |to_call|
    if @dispatch.trace
      puts to_call
      c = ImmediateRead.new.getch
      throw :reset unless c == "\n"
      puts
    end
    @dispatch.dispatch(to_call)
  end
end
load(filename, lineno, line) click to toggle source

Executes a line of TRAC loaded from a file. If an error occurs, an error message will be printed with the line number and filename.

# File lib/trac_lang/executor.rb, line 61
def load(filename, lineno, line)
  @code ||= ''
  to_exe = ''
  catch :reset do
    @code += line
    i = @code.index(@dispatch.meta)
    # explanation of odd indexing:
    # slice everything off code including meta character
    # then execute that slice, without the meta character
    if i
      to_exe = @code.slice!(0..i)[0...-1]
      execute(to_exe)
    end
    return true
  end
  puts to_exe
  puts "Error on or before line #{lineno} of #{filename}"
  return false
end
load_file(filename) click to toggle source

Executes TRAC from a file.

# File lib/trac_lang/executor.rb, line 34
def load_file(filename)
  full_file = File.expand_path(filename, @dispatch.save_dir)
  save_dir(full_file)
  begin
    File.new(full_file, 'r').each do |line|
      break unless load(full_file, $., line)
    end
  rescue
    puts "Error loading file #{full_file}"
  end
  restore_dir
end
prompt() click to toggle source

Executes TRAC from an interactive prompt.

# File lib/trac_lang/executor.rb, line 14
def prompt
  puts "TRAC Emulator #{VERSION}"
  puts
  catch :done do
    loop do
      idle = "#(PS,#(RS)(\n))"
      catch :reset do
        execute(idle)
      end
      if @dispatch.trace
        @dispatch.trace = false
        puts 'Exiting trace...'
      end
    end
  end
  puts 'Exiting...'
  puts
end
restore_dir() click to toggle source

Restores saved directory.

# File lib/trac_lang/executor.rb, line 55
def restore_dir()
  @dispatch.save_dir = @save_save_dir
end
save_dir(filename) click to toggle source

Saves original save_dir from dispatch, set dispatch save_dir to dir of given filename.

# File lib/trac_lang/executor.rb, line 49
def save_dir(filename)
  @save_save_dir = @dispatch.save_dir
  @dispatch.save_dir = File.dirname(filename)
end