Class: Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyslim/statement.rb

Constant Summary collapse

EXCEPTION_TAG =
"__EXCEPTION__:"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(statement) ⇒ Statement

Returns a new instance of Statement.



9
10
11
# File 'lib/rubyslim/statement.rb', line 9

def initialize(statement)
  @statement = statement
end

Class Method Details

.execute(statement, executor) ⇒ Object



5
6
7
# File 'lib/rubyslim/statement.rb', line 5

def self.execute(statement, executor)
  Statement.new(statement).exec(executor)
end

Instance Method Details

#call_method_at_index(index) ⇒ Object



46
47
48
49
50
51
# File 'lib/rubyslim/statement.rb', line 46

def call_method_at_index(index)
  instance_name = get_word(index)
  method_name = slim_to_ruby_method(get_word(index+1))
  args = get_args(index+2)
  [id, @executor.call(instance_name, method_name, *args)]
end

#check_index(index) ⇒ Object

Raises:



81
82
83
# File 'lib/rubyslim/statement.rb', line 81

def check_index(index)
  raise SlimError.new("message:<<MALFORMED_INSTRUCTION #{@statement.inspect}.>>") if index >= @statement.length
end

#exec(executor) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rubyslim/statement.rb', line 13

def exec(executor)
  @executor = executor
  begin
    case(operation)
    when "make"
      instance_name = get_word(2)
      class_name = slim_to_ruby_class(get_word(3))
      [id, @executor.create(instance_name, class_name, get_args(4))]
    when "import"
      @executor.add_module(slim_to_ruby_class(get_word(2)))
      [id, "OK"]
    when  "call"
      call_method_at_index(2)
    when "callAndAssign"
      result = call_method_at_index(3)
      @executor.set_symbol(get_word(2), result[1])
      result
    else
      [id, EXCEPTION_TAG + "message:<<INVALID_STATEMENT: #{@statement.inspect}.>>"]
    end
  rescue SlimError => e
    [id, EXCEPTION_TAG + e.message]
  rescue Exception => e
    message = EXCEPTION_TAG + e.message + "\n" + e.backtrace.join("\n")
    if e.class.to_s =~ /StopTest/
      [id, "STOP" + message]
    else
      [id, message]
    end
  end

end

#get_args(index) ⇒ Object



77
78
79
# File 'lib/rubyslim/statement.rb', line 77

def get_args(index)
  @statement[index..-1]
end

#get_word(index) ⇒ Object



72
73
74
75
# File 'lib/rubyslim/statement.rb', line 72

def get_word(index)
  check_index(index)
  @statement[index]
end

#idObject



64
65
66
# File 'lib/rubyslim/statement.rb', line 64

def id
  get_word(0)
end

#operationObject



68
69
70
# File 'lib/rubyslim/statement.rb', line 68

def operation
  get_word(1)
end

#slim_to_ruby_class(class_name) ⇒ Object



53
54
55
56
57
# File 'lib/rubyslim/statement.rb', line 53

def slim_to_ruby_class(class_name)
  parts = class_name.split(/\.|\:\:/)
  converted = parts.collect {|part| part[0..0].upcase+part[1..-1]}
  converted.join("::")
end

#slim_to_ruby_method(method_name) ⇒ Object



59
60
61
62
# File 'lib/rubyslim/statement.rb', line 59

def slim_to_ruby_method(method_name)
  value = method_name[0..0].downcase + method_name[1..-1]
  value.gsub(/[A-Z]/) { |cap| "_#{cap.downcase}" }
end