Class: TracLang::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/trac_lang/executor.rb

Overview

Executable TRAC commands

Instance Method Summary collapse

Constructor Details

#initialize(d = nil) ⇒ Executor

Initialize executor, giving block to store forms in



8
9
10
11
# File 'lib/trac_lang/executor.rb', line 8

def initialize(d = nil)
  @parser = Parser.new
  @dispatch = d || Dispatch.new
end

Instance Method Details

#execute(str) ⇒ Object

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



83
84
85
86
87
88
89
90
91
92
93
# 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) ⇒ Object

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.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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) ⇒ Object

Executes TRAC from a file.



34
35
36
37
38
39
40
41
42
43
44
45
# 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

#promptObject

Executes TRAC from an interactive prompt.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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_dirObject

Restores saved directory.



55
56
57
# File 'lib/trac_lang/executor.rb', line 55

def restore_dir()
  @dispatch.save_dir = @save_save_dir
end

#save_dir(filename) ⇒ Object

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



49
50
51
52
# 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