Class: Rips::Assembler

Inherits:
Object
  • Object
show all
Defined in:
lib/rips/assembler.rb

Instance Method Summary collapse

Constructor Details

#initialize(debug) ⇒ Assembler

@debug: switch to show trace in console @input: array with assembly instructions @output: array with coded instructions @cmd: line split on tokens (name + arguments) @instruction: instruction instance @instructions: array with line for each instruction @labels: name and number line of label @line: number of file’s line



22
23
24
25
26
27
28
29
30
31
# File 'lib/rips/assembler.rb', line 22

def initialize (debug)
  @debug = debug
  @input = []
  @output = []
  @cmd = {}
  @instruction
  @instructions = [] 
  @labels = {}
  @line = 1  
end

Instance Method Details

#<<(value) ⇒ Object

Stores each new line of file



141
142
143
# File 'lib/rips/assembler.rb', line 141

def << (value)
  @input << value.strip
end

#run(path = "progfile.dat") ⇒ Object

Analyze and translate each instruction



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rips/assembler.rb', line 146

def run (path = "progfile.dat")
  find_instructions
  find_labels

  @input.each do |line|
    if (line.instruction?) || (line.comment?)
      parse_input(line)
      @instruction = nil
      # If it's a comment -> show but not work with it
      if line.instruction?
        
        exists_instruction?
        parse_label
        argument_size?
        argument_syntax?

        @instruction.set_arguments(@cmd[:arguments])
        @output << @instruction.code
      end
      show(@output.size-1) if @debug
    end
    @line += 1
  end
  generate(path)
end