class N65::Parser

This class determines what sort of line of code we are dealing with, parses one line, and returns an object deriving from InstructionBase

Constants

Directives

Public Class Methods

parse(line) click to toggle source

Parses a line of program source into an object deriving from base class InstructionBase

# File lib/n65/parser.rb, line 35
def self.parse(line)
  sanitized = sanitize_line(line)
  return nil if sanitized.empty?

  ##  First check to see if we have a label.
  label = Label.parse(sanitized)
  unless label.nil?
    return label
  end

  ##  Now check if we have a directive
  directive = parse_directive(sanitized)
  unless directive.nil?
    return directive
  end

  ##  Now, surely it is an asm instruction?
  instruction = Instruction.parse(sanitized)
  unless instruction.nil?
    return instruction
  end

  ##  Guess not, we have no idea
  fail(CannotParse, sanitized)
end

Private Class Methods

parse_directive(line) click to toggle source

Try to Parse a directive

# File lib/n65/parser.rb, line 73
def self.parse_directive(line)
  if line.start_with?('.')
    Directives.each do |directive|
      object = directive.parse(line)
      return object unless object.nil?
    end
  end
  nil
end
sanitize_line(line) click to toggle source

Sanitize one line of program source

# File lib/n65/parser.rb, line 65
def self.sanitize_line(line)
  code = line.split(';').first || ""
  code.strip.chomp
end