diff --git a/lib/common.rb b/lib/common.rb new file mode 100644 index 00000000..89c2175e --- /dev/null +++ b/lib/common.rb @@ -0,0 +1,2 @@ +require_relative "common/list" +require_relative "common/statements" diff --git a/lib/common/list.rb b/lib/common/list.rb new file mode 100644 index 00000000..011e48fe --- /dev/null +++ b/lib/common/list.rb @@ -0,0 +1,55 @@ +module Common + module List + + # set the next instruction (also aliased as <<) + # throw an error if that is set, use insert for that use case + # return the instruction, so chaining works as one wants (not backwards) + def set_next nekst + raise "Next already set #{@next}" if @next + @next = nekst + nekst + end + alias :<< :set_next + + # during translation we replace one by one + def replace_next nekst + old = @next + @next = nekst + @next.append old.next if old + end + + # get the next instruction (without arg given ) + # when given an interger, advance along the line that many time and return. + def next( amount = 1) + (amount == 1) ? @next : @next.next(amount-1) + end + + # set the give instruction as the next, while moving any existing + # instruction along to the given ones's next. + # ie insert into the linked list that the instructions form + def insert instruction + instruction.set_next @next + @next = instruction + end + + # return last set instruction. ie follow the linked list until it stops + def last + code = self + code = code.next while( code.next ) + return code + end + + # set next for the last (see last) + # so append the given code to the linked list at the end + def append code + last.set_next code + end + + def length labels = [] + ret = 1 + ret += self.next.length( labels ) if self.next + ret + end + + end +end diff --git a/lib/mom/instruction.rb b/lib/mom/instruction.rb index ba66ec5f..4cf511f7 100644 --- a/lib/mom/instruction.rb +++ b/lib/mom/instruction.rb @@ -2,21 +2,21 @@ module Mom # Base class for MOM instructions class Instruction - attr :next_instruction + include Common::List # implement flatten as noop to avoid condition - def flatten + def flatten( options = {} ) + return self end end # A label with a name - class Label + class Label < Instruction attr_reader :name def initialize(name) @name = name end end - end require_relative "simple_call" diff --git a/lib/rubyx.rb b/lib/rubyx.rb index 5c68e676..cdbe0fd4 100644 --- a/lib/rubyx.rb +++ b/lib/rubyx.rb @@ -14,6 +14,6 @@ require "risc" require "risc/builtin/space" require "arm/arm_machine" require "arm/translator" -require "common/statements" +require "common" require "vool" require "mom"