2015-10-21 13:04:42 +02:00
|
|
|
require_relative "classes_view"
|
|
|
|
|
|
|
|
class SwitchView < ListView
|
|
|
|
def initialize interpreter
|
|
|
|
super([ SelectView.new(interpreter) , ClassesView.new(interpreter) ])
|
|
|
|
end
|
|
|
|
|
|
|
|
def draw
|
|
|
|
super(".classes")
|
|
|
|
end
|
|
|
|
end
|
2015-10-27 11:47:17 +01:00
|
|
|
# opal eval seems to get the scope wrong and evals in object (not where its called)
|
|
|
|
include AST::Sexp
|
2015-10-21 13:04:42 +02:00
|
|
|
|
|
|
|
class SelectView < ElementView
|
2015-10-27 11:47:17 +01:00
|
|
|
|
2015-10-21 13:04:42 +02:00
|
|
|
def initialize interpreter
|
|
|
|
@interpreter = interpreter
|
|
|
|
@codes = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def draw
|
|
|
|
@element = div("h4", "Code") << (list = div("ul.nav!"))
|
|
|
|
list << (div("li.code_list") << div("a.selected" , "none selected"))
|
2015-11-08 23:01:44 +01:00
|
|
|
get_parfait unless @parfait
|
2015-10-21 13:04:42 +02:00
|
|
|
get_codes unless @codes
|
|
|
|
@element << div("br")
|
|
|
|
@element << div("br")
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_codes
|
|
|
|
promise = Browser::HTTP.get "/codes.json"
|
|
|
|
promise.then do |response|
|
|
|
|
@codes = response.text.split("----")
|
|
|
|
add_selection
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-08 23:01:44 +01:00
|
|
|
def get_parfait
|
|
|
|
promise = Browser::HTTP.get "/parfait.json"
|
|
|
|
promise.then do |response|
|
|
|
|
@parfait = decode response.text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-21 13:04:42 +02:00
|
|
|
def add_selection
|
|
|
|
list = div "ul"
|
|
|
|
@codes << @codes.first if @codes.length == 1
|
|
|
|
@codes.each do |c|
|
|
|
|
code = div("li") << div("a" , c )
|
|
|
|
code.on("click"){ select(c) }
|
|
|
|
list << code
|
|
|
|
end
|
|
|
|
select(@codes.first)
|
|
|
|
@element.at_css(".code_list") << list
|
|
|
|
end
|
|
|
|
|
2015-10-27 11:47:17 +01:00
|
|
|
def decode code
|
|
|
|
begin
|
2015-11-08 23:01:44 +01:00
|
|
|
val = Kernel.eval(code)
|
|
|
|
return val
|
2015-10-27 11:47:17 +01:00
|
|
|
rescue => e
|
|
|
|
@element.at_css(".selected").text = "error, #{e}"
|
2015-11-08 23:01:44 +01:00
|
|
|
puts e.backtrace
|
2015-10-27 11:47:17 +01:00
|
|
|
end
|
|
|
|
s(:statements, s(:class, :Foo, s(:derives, nil), s(:statements, s(:class_field, :Integer, :x))))
|
|
|
|
end
|
|
|
|
|
2015-10-21 13:04:42 +02:00
|
|
|
def select code
|
|
|
|
@interpreter.set_state :stopped
|
|
|
|
@element.at_css(".selected").text = code
|
|
|
|
promise = Browser::HTTP.get "/#{code}.json"
|
|
|
|
promise.then do |response|
|
2015-10-27 11:47:17 +01:00
|
|
|
code = decode( response.text)
|
|
|
|
machine = Register.machine.boot
|
2015-11-08 23:01:44 +01:00
|
|
|
@parfait.each do |part|
|
|
|
|
begin
|
|
|
|
Soml.compile( part )
|
|
|
|
rescue => e
|
|
|
|
puts e.backtrace
|
|
|
|
end
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
Soml.compile( code )
|
|
|
|
rescue => e
|
|
|
|
puts e.backtrace
|
|
|
|
raise e
|
|
|
|
end
|
2015-10-21 13:04:42 +02:00
|
|
|
machine.collect
|
2015-11-08 23:01:44 +01:00
|
|
|
puts "starting"
|
2015-10-21 13:04:42 +02:00
|
|
|
@interpreter.start machine.init
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|