rubyx-debugger/lib/views/source_view.rb

149 lines
3.8 KiB
Ruby
Raw Normal View History

2015-10-29 15:49:14 +01:00
class SourceView < ElementView
def initialize interpreter
@interpreter = interpreter
@interpreter.register_event(:instruction_changed, self)
end
def draw
2015-11-01 17:11:38 +01:00
@text = div(".text")
2015-10-29 15:53:46 +01:00
@ticker = div
@element = div(".source_view") << div("h4.source" , "Class.Method") << @ticker << @text
2015-10-29 15:49:14 +01:00
@element
end
def instruction_changed
i = @interpreter.instruction
return "" unless i
if( i.is_a?(Register::Label) and i.name.include?("."))
2015-11-01 12:03:03 +01:00
update_method
end
2015-10-29 15:49:14 +01:00
case i.source
when AST::Node
2015-11-01 17:11:38 +01:00
id = i.source.object_id
text = i.source.type + ":#{id}"
if e = @text.at_css("#i#{id}")
if (old = @text.at_css(".fade_in"))
old.remove_class("fade_in")
end
text += " found"
e.add_class "fade_in"
end
@ticker.text = text
2015-10-29 15:49:14 +01:00
when String
2015-10-29 15:53:46 +01:00
@ticker.text = i.source
2015-10-29 15:49:14 +01:00
else
raise i.source.class.name
end
end
2015-11-01 12:03:03 +01:00
def update_method
@element.at_css(".source").text = @interpreter.instruction.name
cl_name , method_name = *@interpreter.instruction.name.split(".")
clazz = Register.machine.space.get_class_by_name cl_name
method = clazz.get_instance_method( method_name)
2015-11-01 17:11:38 +01:00
@text.inner_html = ToCode.new.process( method.source )
2015-11-01 12:03:03 +01:00
end
2015-10-29 18:10:02 +01:00
def update_code
@text.inner_html = ToCode.new.process( @interpreter.instruction.source)
end
end
2015-11-01 17:11:38 +01:00
class ToCode < AST::Processor
2015-10-29 15:49:14 +01:00
2015-11-01 17:11:38 +01:00
alias :old_process :process
def process s
return "" unless s
old_process(s)
end
2015-10-29 18:10:02 +01:00
def handler_missing s
puts "Missing: " + s.type
end
2015-11-01 12:36:00 +01:00
def div statement , html
2015-11-01 17:11:38 +01:00
"<div class='statement' id='i#{statement.object_id}'>" + html + "</div>"
2015-11-01 12:36:00 +01:00
end
def span statement , html
2015-11-01 17:11:38 +01:00
"<span class='expression' id='i#{statement.object_id}'>" + html + "</span>"
2015-11-01 12:36:00 +01:00
end
2015-11-01 12:03:03 +01:00
def on_function statement
return_type , name , parameters, kids , receiver = *statement
2015-11-01 12:36:00 +01:00
str = return_type + " "
str += receiver + "." if receiver
str += name.to_a.first + "("
2015-11-01 12:03:03 +01:00
str += process(parameters) + ")<br>"
str += process(kids) + "end<br>"
2015-11-01 12:36:00 +01:00
div(statement,str)
2015-11-01 12:03:03 +01:00
end
def on_parameters statement
process_all(statement.children).join(",")
end
def on_parameter p
type , name = *p
2015-11-01 12:36:00 +01:00
span(type,type) + " " + span(name,name)
2015-11-01 12:03:03 +01:00
end
2015-10-29 18:10:02 +01:00
def on_string s
2015-11-01 12:36:00 +01:00
span(s, "'" + s.first + "'")
2015-10-29 18:10:02 +01:00
end
def on_field_def statement
type , name , value = *statement
2015-11-01 12:36:00 +01:00
str = span(type, type) + " " + span(name,name)
2015-10-29 18:10:02 +01:00
str += " = #{process(value)}" if value
2015-11-01 17:11:38 +01:00
div(statement,str)
2015-10-29 18:10:02 +01:00
end
def on_return statement
2015-11-01 12:36:00 +01:00
str = "return " + process(statement.first )
div(statement,str)
2015-10-29 18:10:02 +01:00
end
def on_false_statements s
on_statements s
end
def on_true_statements s
on_statements s
end
def on_statements s
str = ""
s.children.each do |c|
str += process(c).to_s
end
2015-11-01 12:36:00 +01:00
div(s,str)
2015-10-29 18:10:02 +01:00
end
def on_if_statement statement
branch_type , condition , if_true , if_false = *statement
condition = condition.first
ret = "if_#{branch_type}(" + process(condition) + ")<br>" + process(if_true)
ret += "else" + "<br>" + process(if_false) if if_false
2015-11-01 12:03:03 +01:00
ret += "end"
2015-11-01 12:36:00 +01:00
div(statement,ret)
2015-10-29 18:10:02 +01:00
end
def on_assignment statement
name , value = *statement
2015-11-01 17:11:38 +01:00
name = process(name)
2015-10-29 18:10:02 +01:00
v = process(value)
2015-11-01 12:36:00 +01:00
str = name + " = " + v
div(statement,str)
2015-10-29 18:10:02 +01:00
end
def on_call c
name , arguments , receiver = *c
ret = process(name)
2015-11-01 17:11:38 +01:00
ret = process(receiver.first) + "." + ret if receiver
2015-10-29 18:10:02 +01:00
ret += "("
2015-11-01 12:36:00 +01:00
ret += process(arguments).join(",")
2015-10-29 18:10:02 +01:00
ret += ")"
2015-11-01 12:36:00 +01:00
span(c,ret)
2015-10-29 18:10:02 +01:00
end
def on_operator_value statement
operator , left_e , right_e = *statement
left_reg = process(left_e)
right_reg = process(right_e)
2015-11-01 12:36:00 +01:00
span(statement , left_reg + " " + operator + " " + right_reg )
2015-10-29 18:10:02 +01:00
end
def on_arguments args
args.children.collect{|c| process(c)}
end
def on_name name
2015-11-01 12:36:00 +01:00
span(name,name.first)
2015-10-29 18:10:02 +01:00
end
def on_int i
2015-11-01 12:36:00 +01:00
span(i , i.first.to_s)
2015-10-29 18:10:02 +01:00
end
2015-10-29 15:49:14 +01:00
end