rubyx/lib/sol/statements.rb

94 lines
2.1 KiB
Ruby
Raw Normal View History

module Sol
2017-04-01 20:28:57 +02:00
class Statements < Statement
attr_reader :statements
def initialize(statements)
case statements
when nil
@statements = []
when Array
@statements = statements
when Statement
@statements = statements.statements
when Statement
@statements = [statements]
else
raise "Invalid class, must be Statement or Array, not #{statements.class}"
end
end
def empty?
@statements.empty?
end
def single?
@statements.length == 1
end
def first
@statements.first
end
def last
@statements.last
end
def length
@statements.length
end
def [](i)
@statements[i]
end
def <<(o)
if(o.is_a?(Statements))
o.statements.each do |s|
raise "not a statement #{s}" unless s.is_a?(Statement)
@statements << s
end
else
raise "not a statement #{o}" unless o.is_a?(Statement)
@statements << o
end
self
end
2018-07-20 19:06:14 +02:00
def prepend(o)
raise "not a statement #{o}" unless o.is_a?(Statement)
2018-07-20 19:06:14 +02:00
@statements = [o] + @statements
end
def shift
@statements.shift
end
def pop
@statements.pop
end
# apply for all statements , return collection (for testing)
def to_parfait
@statements.collect{|s| s.to_parfait}
end
# to_slot all the statements. Append subsequent ones to the first, and return the
2018-11-03 01:27:46 +01:00
# first.
#
# For ClassStatements this creates and returns a SlotMachineCompiler
2018-11-03 01:27:46 +01:00
#
def to_slot( compiler )
raise "Empty list ? #{statements.length}" if empty?
stats = @statements.dup
first = stats.shift.to_slot(compiler)
while( nekst = stats.shift )
2019-10-03 20:07:55 +02:00
next_slot = nekst.to_slot(compiler)
first.append next_slot
end
2018-11-03 01:27:46 +01:00
first
2017-04-08 11:10:42 +02:00
end
2017-04-09 09:14:28 +02:00
def each(&block)
block.call(self)
@statements.each{|a| a.each(&block)}
end
2018-07-03 21:18:19 +02:00
def to_s(depth = 0)
2019-09-19 19:48:21 +02:00
at_depth(depth , @statements.collect{|st| st.to_s(depth)}.join("\n"))
2018-07-03 21:18:19 +02:00
end
end
class ScopeStatement < Statements
2017-04-01 20:28:57 +02:00
end
end