From d4ac9c7a32b454925aea5777caf27db9419f9c45 Mon Sep 17 00:00:00 2001 From: Joshua Bell Date: Sun, 25 Feb 2018 18:40:36 -0800 Subject: [PATCH] add some format/audit tools --- res/asmfmt.pl | 99 +++++++++++++++++++++++++++++++++++++++++++++++++ res/scopecat.pl | 14 +++++++ res/unscoped.pl | 24 ++++++++++++ 3 files changed, 137 insertions(+) create mode 100755 res/asmfmt.pl create mode 100755 res/scopecat.pl create mode 100755 res/unscoped.pl diff --git a/res/asmfmt.pl b/res/asmfmt.pl new file mode 100755 index 0000000..dcdcc08 --- /dev/null +++ b/res/asmfmt.pl @@ -0,0 +1,99 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +sub nospace($) { + my ($s) = @_; + $s =~ s/ //g; + return $s; +} + + +my $tab = 8; +my $comment_column = 32; + +# TODO: untabify input +# TODO: sigils to disable/enable formatting around blocks + +while () { + chomp; + my $orig = $_; + $_ =~ s/^\s+|\s+$//g; + + if (m/^$/) { + # empty line - ignore + + } elsif (m/^(;;;.*)/) { + + # full line comment - flush left + $_ = $1; + + } elsif (m/^(;;.*)/) { + + # indented comment - one tab stop + $_ = (' ' x $tab) . $1; + + } else { + + my $comment = ''; + if (m/^(.*?)(;.*)$/) { + $_ = $1; + $comment = $2; + } + + if (m/^(\w+)\s*:=\s*(.*)$/) { + + # equate - flush left (!!), spaced out + my ($identifier, $expression) = ($1 // '', $2 // '', $3 // ''); + + $_ = ''; + $_ .= $identifier . ' '; + $_ .= ' ' while length($_) % $tab; + $_ .= ':= ' . $expression . ' '; + + } elsif (m/^(\.(?:end)?(?:proc|scope|macro))\s*(.*)$/) { + + # scope - flush left + my ($opcode, $arguments) = ($1 // '', $2 // ''); + + $_ = $opcode . ' ' . $arguments; + + } elsif (m/^(\.(?:if|elseif|else|endif))\s*(.*)$/) { + + # conditional - half indent left + my ($opcode, $arguments) = ($1 // '', $2 // ''); + + $_ = ' ' x ($tab/2); + $_ .= $opcode . ' ' . $arguments; + + } elsif (m/^(\w*:)?\s*(\S+)?\s*(.*?)\s*(;.*)?$/) { + + # label / opcode / arguments / comment + my ($label, $opcode, $arguments, $comment) = ($1 // '', $2 // '', $3 // '', $4 // ''); + + $_ = ''; + $_ .= $label . ' '; + $_ .= ' ' while length($_) % $tab; + $_ .= $opcode . ' '; + if ($opcode =~ m/^([a-z]{3}\w*)$|^(\.(byte|word|addr|res))$/) { + $_ .= ' ' while length($_) % $tab; + } + $_ .= $arguments . ' '; + + } else { + die "Unexpected line: $_\n"; + } + + if ($comment ) { + $_ .= ' ' while length($_) < $comment_column; + $_ .= $comment; + } + } + + $_ =~ s/\s+$//; # trim right + + die unless nospace($_) eq nospace($orig); + + print $_, "\n"; +} diff --git a/res/scopecat.pl b/res/scopecat.pl new file mode 100755 index 0000000..84670d3 --- /dev/null +++ b/res/scopecat.pl @@ -0,0 +1,14 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +my $depth = 0; + +while () { + ++$depth if m/\.proc/ || m/\.scope/; + + print "$depth - $_"; + + --$depth if m/\.endproc/ || m/\.endscope/; +} diff --git a/res/unscoped.pl b/res/unscoped.pl new file mode 100755 index 0000000..150773a --- /dev/null +++ b/res/unscoped.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +my %terms; +my %unscoped; +my $depth = 0; + +while () { + ++$depth if m/\.proc/ || m/\.scope/; + --$depth if m/\.endproc/ || m/\.endscope/; + foreach my $term (split /\b/, $_) { + if ($term =~ /^L[0-9A-F]{4}$/) { + $terms{$term} = 0 unless defined $terms{$term}; + $terms{$term} += 1; + $unscoped{$term} = 1 if $depth < 2; + } + } +} + +foreach my $term (sort keys %unscoped) { + print "$term\n"; +}