Class: Statement
- Inherits:
-
Object
- Object
- Statement
- Defined in:
- lib/rubyslim/statement.rb
Constant Summary collapse
- EXCEPTION_TAG =
"__EXCEPTION__:"
Class Method Summary collapse
Instance Method Summary collapse
- #call_method_at_index(index) ⇒ Object
- #check_index(index) ⇒ Object
- #exec(executor) ⇒ Object
- #get_args(index) ⇒ Object
- #get_word(index) ⇒ Object
- #id ⇒ Object
-
#initialize(statement) ⇒ Statement
constructor
A new instance of Statement.
- #operation ⇒ Object
- #slim_to_ruby_class(class_name) ⇒ Object
- #slim_to_ruby_method(method_name) ⇒ Object
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
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
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.] rescue Exception => e = EXCEPTION_TAG + e. + "\n" + e.backtrace.join("\n") if e.class.to_s =~ /StopTest/ [id, "STOP" + ] else [id, ] 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 |
#id ⇒ Object
64 65 66 |
# File 'lib/rubyslim/statement.rb', line 64 def id get_word(0) end |
#operation ⇒ Object
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 |