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



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



41
42
43
44
45
46
# File 'lib/rubyslim/statement.rb', line 41

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:



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

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
# 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
    [id, EXCEPTION_TAG + e.message + "\n" + e.backtrace.join("\n")]
  end

end

#get_args(index) ⇒ Object



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

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

#get_word(index) ⇒ Object



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

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

#idObject



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

def id
  get_word(0)
end

#operationObject



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

def operation
  get_word(1)
end

#slim_to_ruby_class(class_name) ⇒ Object



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

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



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

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