own statements class for mom

so we don’t have to deal with arrays (as a special case)
and use method sending as is good oo
This commit is contained in:
Torsten Ruger 2017-09-06 12:33:46 +03:00
parent 0e51492430
commit 9a1e4a6f27
6 changed files with 53 additions and 35 deletions

27
lib/common/statements.rb Normal file
View File

@ -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

View File

@ -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"

21
lib/mom/statement.rb Normal file
View File

@ -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"

View File

@ -14,5 +14,6 @@ require "risc"
require "risc/builtin/space"
require "arm/arm_machine"
require "arm/translator"
require "common/statements"
require "vool"
require "mom"

View File

@ -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

View File

@ -1,2 +0,0 @@
require_relative "send/test_send_simple"
require_relative "send/test_send_simple_args"