mirror of
https://github.com/safiire/n65.git
synced 2024-12-13 06:29:16 +00:00
Linted parser.rb
This commit is contained in:
parent
1b57c0a04d
commit
9acca01731
@ -1,6 +1,6 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module N65
|
module N65
|
||||||
|
|
||||||
require_relative 'instruction'
|
require_relative 'instruction'
|
||||||
require_relative 'directives/ines_header'
|
require_relative 'directives/ines_header'
|
||||||
require_relative 'directives/org'
|
require_relative 'directives/org'
|
||||||
@ -15,71 +15,53 @@ module N65
|
|||||||
require_relative 'directives/exit_scope'
|
require_relative 'directives/exit_scope'
|
||||||
require_relative 'directives/space'
|
require_relative 'directives/space'
|
||||||
|
|
||||||
|
# This class determines what sort of line of code we
|
||||||
|
# are dealing with, parses one line, and returns an
|
||||||
####
|
# object deriving from InstructionBase
|
||||||
## This class determines what sort of line of code we
|
|
||||||
## are dealing with, parses one line, and returns an
|
|
||||||
## object deriving from InstructionBase
|
|
||||||
class Parser
|
class Parser
|
||||||
|
|
||||||
#### Custom Exceptions
|
|
||||||
class CannotParse < StandardError; end
|
class CannotParse < StandardError; end
|
||||||
|
|
||||||
|
DIRECTIVES = [INESHeader, Org, Segment, IncBin, Inc, DW, Bytes, ASCII, EnterScope, ExitScope, Space].freeze
|
||||||
|
|
||||||
Directives = [INESHeader, Org, Segment, IncBin, Inc, DW, Bytes, ASCII, EnterScope, ExitScope, Space]
|
# Parses a line of program source into an object
|
||||||
|
# deriving from base class InstructionBase
|
||||||
####
|
|
||||||
## Parses a line of program source into an object
|
|
||||||
## deriving from base class InstructionBase
|
|
||||||
def self.parse(line)
|
def self.parse(line)
|
||||||
sanitized = sanitize_line(line)
|
sanitized = sanitize_line(line)
|
||||||
return nil if sanitized.empty?
|
return nil if sanitized.empty?
|
||||||
|
|
||||||
## First check to see if we have a label.
|
# First check to see if we have a label.
|
||||||
label = Label.parse(sanitized)
|
label = Label.parse(sanitized)
|
||||||
unless label.nil?
|
return label unless label.nil?
|
||||||
return label
|
|
||||||
end
|
|
||||||
|
|
||||||
## Now check if we have a directive
|
# Now check if we have a directive
|
||||||
directive = parse_directive(sanitized)
|
directive = parse_directive(sanitized)
|
||||||
unless directive.nil?
|
return directive unless directive.nil?
|
||||||
return directive
|
|
||||||
end
|
|
||||||
|
|
||||||
## Now, surely it is an asm instruction?
|
# Now, surely it is an asm instruction?
|
||||||
instruction = Instruction.parse(sanitized)
|
instruction = Instruction.parse(sanitized)
|
||||||
unless instruction.nil?
|
return instruction unless instruction.nil?
|
||||||
return instruction
|
|
||||||
end
|
|
||||||
|
|
||||||
## Guess not, we have no idea
|
# Guess not, we have no idea
|
||||||
fail(CannotParse, sanitized)
|
raise(CannotParse, sanitized)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Sanitize one line of program source
|
||||||
private
|
|
||||||
####
|
|
||||||
## Sanitize one line of program source
|
|
||||||
def self.sanitize_line(line)
|
def self.sanitize_line(line)
|
||||||
code = line.split(';').first || ""
|
code = line.split(';').first || ''
|
||||||
code.strip.chomp
|
code.strip.chomp
|
||||||
end
|
end
|
||||||
|
private_class_method :sanitize_line
|
||||||
|
|
||||||
|
|
||||||
####
|
|
||||||
## Try to Parse a directive
|
## Try to Parse a directive
|
||||||
def self.parse_directive(line)
|
def self.parse_directive(line)
|
||||||
if line.start_with?('.')
|
if line.start_with?('.')
|
||||||
Directives.each do |directive|
|
DIRECTIVES.each do |directive|
|
||||||
object = directive.parse(line)
|
object = directive.parse(line)
|
||||||
return object unless object.nil?
|
return object unless object.nil?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
private_class_method :parse_directive
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user