2018-02-26 02:40:36 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
sub nospace($) {
|
|
|
|
my ($s) = @_;
|
2018-05-26 01:49:36 +00:00
|
|
|
$s =~ s/\s//g;
|
2018-02-26 02:40:36 +00:00
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
|
2018-12-10 00:15:48 +00:00
|
|
|
sub respace_comment($) {
|
|
|
|
my ($s) = @_;
|
|
|
|
$s =~ s/^(;+)\s+(.*)/$1 $2/g;
|
|
|
|
return $s;
|
|
|
|
}
|
2018-02-26 02:40:36 +00:00
|
|
|
|
|
|
|
my $tab = 8;
|
|
|
|
my $comment_column = 32;
|
|
|
|
|
|
|
|
# TODO: untabify input
|
|
|
|
# TODO: sigils to disable/enable formatting around blocks
|
|
|
|
|
|
|
|
while (<STDIN>) {
|
|
|
|
chomp;
|
|
|
|
my $orig = $_;
|
|
|
|
$_ =~ s/^\s+|\s+$//g;
|
|
|
|
|
|
|
|
if (m/^$/) {
|
|
|
|
# empty line - ignore
|
|
|
|
|
|
|
|
} elsif (m/^(;;;.*)/) {
|
|
|
|
|
|
|
|
# full line comment - flush left
|
2018-12-10 00:15:48 +00:00
|
|
|
$_ = respace_comment($1);
|
2018-02-26 02:40:36 +00:00
|
|
|
|
|
|
|
} elsif (m/^(;;.*)/) {
|
|
|
|
|
|
|
|
# indented comment - one tab stop
|
2018-12-10 00:15:48 +00:00
|
|
|
$_ = (' ' x $tab) . respace_comment($1);
|
2018-02-26 02:40:36 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
my $comment = '';
|
|
|
|
if (m/^(.*?)(;.*)$/) {
|
|
|
|
$_ = $1;
|
2018-12-10 00:15:48 +00:00
|
|
|
$comment = respace_comment($2);
|
2018-02-26 02:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m/^(\w+)\s*:=\s*(.*)$/) {
|
|
|
|
|
|
|
|
# equate - flush left (!!), spaced out
|
|
|
|
my ($identifier, $expression) = ($1 // '', $2 // '', $3 // '');
|
|
|
|
|
|
|
|
$_ = '';
|
|
|
|
$_ .= $identifier . ' ';
|
|
|
|
$_ .= ' ' while length($_) % $tab;
|
|
|
|
$_ .= ':= ' . $expression . ' ';
|
|
|
|
|
2018-11-18 04:34:17 +00:00
|
|
|
} elsif (m/^(\.(?:end)?(?:proc|scope|macro|struct|enum)\b)\s*(.*)$/) {
|
2018-02-26 02:40:36 +00:00
|
|
|
|
|
|
|
# scope - flush left
|
|
|
|
my ($opcode, $arguments) = ($1 // '', $2 // '');
|
|
|
|
|
|
|
|
$_ = $opcode . ' ' . $arguments;
|
|
|
|
|
2018-03-05 16:41:22 +00:00
|
|
|
} elsif (m/^(\.(?:if\w*|elseif|else|endif)\b)\s*(.*)$/) {
|
2018-02-26 02:40:36 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2018-05-26 01:49:36 +00:00
|
|
|
die "Mismatch:\n> $orig\n<$_\n"unless nospace($_) eq nospace($orig);
|
2018-02-26 02:40:36 +00:00
|
|
|
|
|
|
|
print $_, "\n";
|
|
|
|
}
|