From 9a1e4a6f278bc967646d31414240db5d6b59c1a7 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 6 Sep 2017 12:33:46 +0300 Subject: [PATCH] own statements class for mom MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit so we don’t have to deal with arrays (as a special case) and use method sending as is good oo --- lib/common/statements.rb | 27 +++++++++++++++++++++++++++ lib/mom/instruction.rb | 11 +---------- lib/mom/statement.rb | 21 +++++++++++++++++++++ lib/rubyx.rb | 1 + lib/vool/statements/statements.rb | 26 +++----------------------- test/vool/to_mom/test_send.rb | 2 -- 6 files changed, 53 insertions(+), 35 deletions(-) create mode 100644 lib/common/statements.rb create mode 100644 lib/mom/statement.rb delete mode 100644 test/vool/to_mom/test_send.rb diff --git a/lib/common/statements.rb b/lib/common/statements.rb new file mode 100644 index 00000000..a8b9218c --- /dev/null +++ b/lib/common/statements.rb @@ -0,0 +1,27 @@ +module Common + #extracted to resuse + module Statements + attr_reader :statements + def initialize(statements) + @statements = statements + end + + def empty? + @statements.empty? + end + def single? + @statements.length == 1 + end + def first + @statements.first + end + def length + @statements.length + end + + def collect(arr) + @statements.each { |s| s.collect(arr) } + super + end + end +end diff --git a/lib/mom/instruction.rb b/lib/mom/instruction.rb index 630062e4..ba66ec5f 100644 --- a/lib/mom/instruction.rb +++ b/lib/mom/instruction.rb @@ -17,20 +17,11 @@ module Mom end end - class Statement - # flattening will change the structure from a tree to a linked list (and use - # next_instruction to do so) - def flatten - raise "not implemented" - end - end - end require_relative "simple_call" -require_relative "if_statement" -require_relative "while_statement" require_relative "truth_check" require_relative "jump" require_relative "slot_load" require_relative "return_sequence" +require_relative "statement" diff --git a/lib/mom/statement.rb b/lib/mom/statement.rb new file mode 100644 index 00000000..a8a75443 --- /dev/null +++ b/lib/mom/statement.rb @@ -0,0 +1,21 @@ +module Mom + class Statement + # flattening will change the structure from a tree to a linked list (and use + # next_instruction to do so) + def flatten + raise "not implemented for #{self}" + end + end + + class Statements < Statement + include Common::Statements + + def flatten + @statements.each{ |s| s.flatten } + end + end + +end + +require_relative "if_statement" +require_relative "while_statement" diff --git a/lib/rubyx.rb b/lib/rubyx.rb index 6ef600d1..5c68e676 100644 --- a/lib/rubyx.rb +++ b/lib/rubyx.rb @@ -14,5 +14,6 @@ require "risc" require "risc/builtin/space" require "arm/arm_machine" require "arm/translator" +require "common/statements" require "vool" require "mom" diff --git a/lib/vool/statements/statements.rb b/lib/vool/statements/statements.rb index a9d2388f..07201079 100644 --- a/lib/vool/statements/statements.rb +++ b/lib/vool/statements/statements.rb @@ -1,33 +1,13 @@ module Vool class Statements < Statement - attr_reader :statements - def initialize(statements) - @statements = statements - end + include Common::Statements # create machine instructions def to_mom( method ) - @statements.collect do |statement| + all = @statements.collect do |statement| statement.to_mom( method ) end - end - - def empty? - @statements.empty? - end - def single? - @statements.length == 1 - end - def first - @statements.first - end - def length - @statements.length - end - - def collect(arr) - @statements.each { |s| s.collect(arr) } - super + Mom::Statements.new(all) end def create_objects diff --git a/test/vool/to_mom/test_send.rb b/test/vool/to_mom/test_send.rb deleted file mode 100644 index f5e5a5bc..00000000 --- a/test/vool/to_mom/test_send.rb +++ /dev/null @@ -1,2 +0,0 @@ -require_relative "send/test_send_simple" -require_relative "send/test_send_simple_args"