Skip to content

Instantly share code, notes, and snippets.

@niratama
Last active October 23, 2024 09:06
Show Gist options
  • Save niratama/c3d5b82b1a84f27420f20a3e2389557b to your computer and use it in GitHub Desktop.
Save niratama/c3d5b82b1a84f27420f20a3e2389557b to your computer and use it in GitHub Desktop.
ラベルつきBASICリストを行番号になおす
#!/usr/bin/env perl
# Usage: cat program.lbas | perl ennum.pl > program.bas
# Example:
# `@loop
# PRINT "nya-n"
# GOTO @loop
use strict;
use warnings;
use utf8;
my $start = 10;
my $step = 10;
my %labels;
my $lineno = $start;
my @lines;
while (my $line = <>) {
chomp $line;
$line =~ s/^\d+\s+//;
next if $line eq '' || $line eq "'" || uc($line) eq "REM";
if ($line =~ m/^'\@([-_a-zA-Z0-9]+)/) {
$labels{$1} = $lineno;
next;
}
push @lines, $line;
$lineno += $step;
}
$lineno = $start;
for my $line (@lines) {
if ($line !~ m/^('|[rR][eE][mM])/) {
$line =~ s!([^"]*)("[^"]*")?!$1 =~ s/\@([-_a-zA-Z0-9]+)/$labels{$1}/gr . ($2 || '')!ge;
# $line =~ s/\@([-_a-zA-Z0-9]+)/$labels{$1}/g if $line !~ m/^'\@/;
}
print "$lineno $line\n";
$lineno += $step;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment