From 1152bcd84dd6921d2a1fa8519db2e3c6692fe558 Mon Sep 17 00:00:00 2001 From: Leeland Heins Date: Wed, 19 Dec 2018 23:09:16 -0600 Subject: [PATCH] Initial version Utility to format 6502 assembler source files for as65 --- asfmt.pl | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 asfmt.pl diff --git a/asfmt.pl b/asfmt.pl new file mode 100644 index 0000000..ca7895a --- /dev/null +++ b/asfmt.pl @@ -0,0 +1,148 @@ +#!/usr/bin/perl -w + +# +# asfmt.pl +# +# Simple 65C02 assembler source formatter. +# +# 20181219 LSH +# + +use strict; + +sub usage { + print "Usage:\n"; + print "$0 [-h] \n"; + print " -h : This help\n"; +} + +# Process command line arguments. +while (defined $ARGV[0] && $ARGV[0] =~ /^-/) { + # Help. + if ($ARGV[0] eq '-h') { + usage(); + exit; + } else { + die "Invalid argument $ARGV[0]\n"; + } +} + +my $input_file = shift; + +die "Must supply input filename\n" unless defined $input_file && $input_file; + +sub parse_line { + my ($line, $lineno) = @_; + + my ($label, $mnemonic, $operand, $comment) = ('', '', '', ''); + if ($line =~ /^(\S+)\s+(\S+)\s+(\S+)\s+(;.*)$/) { + $label = $1; + $mnemonic = $2; + $operand = $3; + $comment = $4; + } elsif ($line =~ /^(\S+)\s+(\S+)\s+(\S+)\s*$/) { + $label = $1; + $mnemonic = $2; + $operand = $3; + $comment = ''; + } elsif ($line =~ /^\s+(\S+)\s+(\S+)\s+(;.*)$/) { + $label = ''; + $mnemonic = $1; + $operand = $2; + $comment = $3; + } elsif ($line =~ /^\s+(\S+)\s+(\S+)\s*$/) { + $label = ''; + $mnemonic = $1; + $operand = $2; + $comment = ''; + } elsif ($line =~ /^\s+(\S+)\s+(;.*)$/) { + $label = ''; + $mnemonic = $1; + $operand = ''; + $comment = $2; + } elsif ($line =~ /^\s+(\S+)\s*$/) { + $label = ''; + $mnemonic = $1; + $operand = ''; + $comment = ''; + } elsif ($line =~ /^(\S+)\s*$/) { + $label = $1; + $mnemonic = ''; + $operand = ''; + $comment = ''; + } elsif ($line =~ /^(\S+)\s+(\S+)\s*$/) { + $label = $1; + $mnemonic = $2; + $operand = ''; + $comment = ''; + } elsif ($line =~ /^\s+(\S+)\s+(;.*)$/) { + $label = ''; + $mnemonic = $1; + $operand = ''; + $comment = $2; + } elsif ($line =~ /^(\S+)\s+(\S+)\s+(;.*)$/) { + $label = $1; + $mnemonic = $2; + $operand = ''; + $comment = $3; + } elsif ($line =~ /^(\S+)\s+([Aa][Ss][Cc])\s+(".+")\s+(;.*)$/) { + $label = $1; + $mnemonic = $2; + $operand = $3; + $comment = $4; + } elsif ($line =~ /^\s+([Aa][Ss][Cc])\s+(".+")\s+(;.*)$/) { + $label = ''; + $mnemonic = $1; + $operand = $2; + $comment = $3; + } elsif ($line =~ /^(\S+)\s+([Aa][Ss][Cc])\s+(".+")\s*$/) { + $label = $1; + $mnemonic = $2; + $operand = $3; + $comment = ''; + } elsif ($line =~ /^\s+([Aa][Ss][Cc])\s+(".+")\s*$/) { + $label = ''; + $mnemonic = $1; + $operand = $2; + $comment = ''; + } else { + print "SYNTAX ERROR! $lineno : $line\n"; + } + + $label = '' unless defined $label; + $comment = '' unless defined $comment; + $mnemonic = '' unless defined $mnemonic; + $operand = '' unless defined $operand; + + return ($label, $mnemonic, $operand, $comment); +} + +my $ifh; + +# Open the input file. +if (open($ifh, "<$input_file")) { + my $lineno = 0; + + while (my $line = readline $ifh) { + chomp $line; + + $lineno++; + + if ($line =~ /^\s*\*|^\s*;/) { + print "$line\n"; + next; + } + + # Parse input lines. + my ($label, $mnemonic, $operand, $comment) = parse_line($line, $lineno); + + print sprintf("%-8s %-4s %-12s %s\n", $label, $mnemonic, $operand, $comment); + } + + close $ifh; +} else { + die "Can't open $input_file\n"; +} + +1; +