diff --git a/lib/directives/ascii.rb b/lib/directives/ascii.rb index 64be930..cce8d07 100644 --- a/lib/directives/ascii.rb +++ b/lib/directives/ascii.rb @@ -13,7 +13,7 @@ module Assembler6502 def self.parse(line) match_data = line.match(/^\.ascii\s+"([^"]+)"$/) return nil if match_data.nil? - ASCII.new($1) + ASCII.new(match_data[1]) end diff --git a/lib/directives/enter_scope.rb b/lib/directives/enter_scope.rb new file mode 100644 index 0000000..bb1cd28 --- /dev/null +++ b/lib/directives/enter_scope.rb @@ -0,0 +1,42 @@ +require_relative '../instruction_base' + +module Assembler6502 + + + #### + ## This directive to include bytes + class EnterScope < InstructionBase + + + #### + ## Try to parse an incbin directive + def self.parse(line) + match_data = line.match(/^\.scope\s+([a-zA-Z][a-zA-Z0-9_]+)$/) + return nil if match_data.nil? + EnterScope.new(match_data[1]) + end + + + #### + ## Initialize with filename + def initialize(name) + @name = name + end + + + #### + ## Execute on the assembler + def exec(assembler) + assembler.symbol_table.enter_scope(@name) + end + + + #### + ## Display + def to_s + ".scope #{@name}" + end + + end + +end diff --git a/lib/directives/exit_scope.rb b/lib/directives/exit_scope.rb new file mode 100644 index 0000000..ee497fc --- /dev/null +++ b/lib/directives/exit_scope.rb @@ -0,0 +1,35 @@ +require_relative '../instruction_base' + +module Assembler6502 + + + #### + ## This directive to include bytes + class ExitScope < InstructionBase + + + #### + ## Try to parse an incbin directive + def self.parse(line) + match_data = line.match(/^\.$/) + return nil if match_data.nil? + ExitScope.new + end + + + #### + ## Execute on the assembler + def exec(assembler) + assembler.symbol_table.exit_scope + end + + + #### + ## Display + def to_s + "." + end + + end + +end diff --git a/lib/parser.rb b/lib/parser.rb index 3b81a95..6e1bb78 100644 --- a/lib/parser.rb +++ b/lib/parser.rb @@ -10,6 +10,8 @@ module Assembler6502 require_relative 'directives/bytes' require_relative 'directives/ascii' require_relative 'directives/label' + require_relative 'directives/enter_scope' + require_relative 'directives/exit_scope' #### @@ -22,7 +24,7 @@ module Assembler6502 class CannotParse < StandardError; end - Directives = [INESHeader, Org, Segment, IncBin, DW, Bytes, ASCII] + Directives = [INESHeader, Org, Segment, IncBin, DW, Bytes, ASCII, EnterScope, ExitScope] #### ## Parses a line of program source into an object diff --git a/scope.asm b/scope.asm new file mode 100644 index 0000000..1dd04fa --- /dev/null +++ b/scope.asm @@ -0,0 +1,31 @@ +;;;; +; Create an iNES header +.ines {"prog": 1, "char": 0, "mapper": 0, "mirror": 0} + +;;;; +;; Start a prog segment number 0 +.segment prog 0 +.org $8000 + +.scope yay + main: + sei + cld + loop: + ldx $00 + inx + stx $00 + jmp loop +. + +vblank: +irq: + rti + +;;;; +;; Vector table +.org $FFFA + +.dw vblank +.dw yay.main +.dw irq