From 7be8e040bb3da5971c029e2717afd6ec0b33dd2e Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 27 Dec 2016 20:31:41 +0200 Subject: [PATCH] add code options this time inline (not server generated) into the switch view --- config/initializers/server/salama.rb | 1 - lib/debugger.rb | 8 +---- lib/views/switch_view.rb | 46 ++++++++++++++++++---------- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/config/initializers/server/salama.rb b/config/initializers/server/salama.rb index 6771268..d4c8080 100644 --- a/config/initializers/server/salama.rb +++ b/config/initializers/server/salama.rb @@ -1,2 +1 @@ Opal.use_gem "salama" -Opal.use_gem "salama-arm" diff --git a/lib/debugger.rb b/lib/debugger.rb index 676473c..4c7bb9b 100644 --- a/lib/debugger.rb +++ b/lib/debugger.rb @@ -38,13 +38,7 @@ class MainView < ListView include AST::Sexp def initialize - machine = Register.machine.boot - code = s(:statements, s(:call, - s(:name, :set_internal_byte), - s(:arguments, s(:int, 1), s(:int, 104)), - s(:receiver, s(:string, "Hello")))) - Typed.compile( code ) - machine.collect + Register.machine.boot @interpreter = Register::Interpreter.new super( [SwitchView.new(@interpreter) , SourceView.new(@interpreter) , diff --git a/lib/views/switch_view.rb b/lib/views/switch_view.rb index b1ded89..bce9800 100644 --- a/lib/views/switch_view.rb +++ b/lib/views/switch_view.rb @@ -22,44 +22,56 @@ class SelectView < ElementView def draw @element = div("h4", "Code") << (list = div("ul.nav!")) list << (div("li.code_list") << div("a.selected" , "none selected")) - get_codes unless @codes + selection_codes unless @codes @element << div("br") @element << div("br") end - def get_codes - @codes = ["set_internal_byte"] - add_selection - end - - def add_selection + def selection_codes + @codes = get_codes.keys list = div "ul" - @codes << @codes.first if @codes.length == 1 + @codes << @codes.first if @codes.length == 1 #otherwise unselectable @codes.each do |c| code = div("li") << div("a" , c ) code.on("click"){ select(c) } list << code end - Promise.new.then{ - select(@codes.first) - } @element.at_css(".code_list") << list end def select code + puts "selecting #{code}" @interpreter.set_state :stopped @element.at_css(".selected").text = code - input = s(:statements, s(:call, - s(:name, :set_internal_byte), - s(:arguments, s(:int, 1), s(:int, 104)), - s(:receiver, s(:string, "Hello")))) + main , clean = get_codes[code] machine = Register.machine.boot - #do_clean_compile - Typed.compile( input ) + clean_compile(*clean) if clean + Typed.compile( main ) machine.collect puts "starting" @interpreter.start machine.init end + + def get_codes + {"set_internal_byte" => [s(:statements, s(:call, + s(:name, :set_internal_byte), + s(:arguments, s(:int, 1), s(:int, 104)), + s(:receiver, s(:string, "Hello")))) , nil ] , + "called_if" => [s(:statements, s(:call, s(:name, :itest), s(:arguments, s(:int, 20)))) , + [:Space , :itest , {:n => :Integer} , + s(:statements, s(:if_statement, :zero, s(:condition, s(:operator_value, :-, s(:name, :n), s(:int, 12))), + s(:true_statements, s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "then")))), + s(:false_statements, s(:call, s(:name, :putstring), s(:arguments), s(:receiver, s(:string, "else"))))))]], + "hello world" => [ s(:statements, s(:return, s(:call, s(:name, :putstring), s(:arguments), + s(:receiver, s(:string, "Hello again\\n"))))), + nil], + } + end + def clean_compile(clazz_name , method_name , args , statements) + compiler = Typed::MethodCompiler.new.create_method(clazz_name,method_name,args ).init_method + compiler.process( Typed.ast_to_code( statements ) ) + end + end