Class: Tcl::Ruby::Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/tcl/ruby/util.rb,
lib/tcl/ruby/parser.rb,
lib/tcl/ruby/command.rb,
lib/tcl/ruby/commands/list.rb,
lib/tcl/ruby/commands/array.rb,
lib/tcl/ruby/commands/basic.rb,
lib/tcl/ruby/commands/inout.rb,
lib/tcl/ruby/commands/string.rb

Constant Summary collapse

EX_CHAR_CHECK =
lambda do |s, t|
  (t && !s.check(/\s|\z/)) || (!t && !s.check(/\s|\z|;/))
end.curry

Instance Method Summary collapse

Constructor Details

#initializeInterpreter

Returns a new instance of Interpreter.



4
5
6
7
8
9
10
11
# File 'lib/tcl/ruby/util.rb', line 4

def initialize
  @variables = {}
  @global = @variables
  @v_stack = []
  @hooks = {}
  @proc = {}
  @files = {}
end

Instance Method Details

#add_hook(name, &block) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
# File 'lib/tcl/ruby/util.rb', line 19

def add_hook(name, &block)
  raise(ArgumentError, 'block is not given') unless block_given?
  @hooks[name.to_s] = block
end

#commandsObject



28
29
30
31
32
33
34
35
# File 'lib/tcl/ruby/util.rb', line 28

def commands
  r = []
  r.concat private_methods.select { |e| e.match(/^___/) }
                          .map { |e| e[3..-1] }
  r.concat @proc.keys
  r.concat @hooks.keys
  r
end

#delete_hook(name) ⇒ Object



24
25
26
# File 'lib/tcl/ruby/util.rb', line 24

def delete_hook(name)
  @hooks.delete(name.to_s)
end

#parse(str, to_list = false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tcl/ruby/parser.rb', line 10

def parse(str, to_list = false)
  str2 = (str + "\n").gsub(/\\\n\s*/, ' ') # replace \ & \n & extra ws
  @s = StringScanner.new(str2)
  @list_array = ListArray.new
  @pstack = [] # stack for brace, bracket, quote
  @buffer = '' # ListElement.new('')'' # buffer for list element
  @commands = []
  until @s.empty?
    if @s.scan(/\\./) then @buffer << @s[0]
    elsif @s.scan(/\#/) then parse_comments
    elsif !to_list && @s.scan(/\r\n|\r|\n|;/) then parse_command_ends
    elsif @s.scan(/\s/) then parse_blanks
    elsif @s.scan(/\S/)
      @buffer << @s[0]
      analyze_parentheses(to_list, EX_CHAR_CHECK[@s]) if
        @buffer.parenthesis?
    end
  end
  check_pstack
  to_list ? @list_array.to_string : command(@commands)
end

#variables(arg) ⇒ Object



13
14
15
16
17
# File 'lib/tcl/ruby/util.rb', line 13

def variables(arg)
  raise TclVariableNotFoundError.new(arg, 'no such variables') unless
    @variables.key?(arg)
  @variables[arg]
end