2015-05-01 21:26:31 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
# SEE ALSO THE DOCUMENTATION IN port.sh
|
|
|
|
#
|
|
|
|
# This perl script is for porting files from the standard library to the HoTT library
|
|
|
|
#
|
2015-05-07 22:43:10 +00:00
|
|
|
# To use: first make it executable (chmod u+x port.pl). Then type
|
2015-05-01 21:26:31 +00:00
|
|
|
#
|
2015-05-07 22:43:10 +00:00
|
|
|
# ./port.pl ../library/path/to/source.lean ../hott/path/to/destination.hlean ["from1" "to1" "from2" "to2" ...]
|
2015-05-01 21:26:31 +00:00
|
|
|
#
|
2015-05-07 22:43:10 +00:00
|
|
|
# This will port the file ../library/path/to/source.lean to ../hott/path/to/destination.hlean
|
|
|
|
# renaming core definitions form the standard library to core definitions in the HoTT library.
|
|
|
|
# These renamings are specified in port.txt. See the documentation in rename.pl for the syntax.
|
|
|
|
# The arguments "fromi" and "toi" are optional, but should be provided in pairs.
|
|
|
|
# These arguments will replace "fromi" by "toi" in the specified file,
|
|
|
|
# before doing any other renamings.
|
|
|
|
|
2015-05-01 21:26:31 +00:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use Cwd 'abs_path';
|
|
|
|
use File::Basename;
|
|
|
|
use File::Spec::Functions;
|
|
|
|
use File::Copy;
|
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
|
|
|
# the global list of renamings
|
|
|
|
my %renamings = ();
|
|
|
|
my %literalrenamings = ();
|
|
|
|
my %literalrenamings2 = ();
|
|
|
|
|
|
|
|
# get the list of renamings from the file
|
|
|
|
sub get_renamings {
|
|
|
|
if (scalar(@ARGV)%2==1) {die "ERROR: odd number of arguments provided"}
|
|
|
|
%literalrenamings2 = @ARGV;
|
|
|
|
my $fullname = catfile(dirname(abs_path($0)), "port.txt");
|
|
|
|
open (my $renaming_file, "<", $fullname) or die $!;
|
|
|
|
while (<$renaming_file>) {
|
|
|
|
if (/([\w'.]+)[:]([\w'.]+)\n/) {
|
|
|
|
$renamings{$1} = $2;
|
|
|
|
} elsif (/(.+)[;](.+)\n/) {
|
|
|
|
$literalrenamings{$1} = $2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close $renaming_file or die $!;
|
|
|
|
}
|
|
|
|
|
|
|
|
# print them out - for debugging
|
|
|
|
sub show_renamings {
|
|
|
|
foreach my $key (keys %renamings) {
|
|
|
|
print $key, " => ", $renamings{$key}, "\n";
|
|
|
|
}
|
|
|
|
print "\n";
|
|
|
|
foreach my $lkey (keys %literalrenamings2) {
|
|
|
|
print $lkey, " -> ", $literalrenamings2{$lkey}, "\n";
|
|
|
|
}
|
|
|
|
foreach my $lkey (keys %literalrenamings) {
|
|
|
|
print $lkey, " -> ", $literalrenamings{$lkey}, "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# rename all identifiers a file; original goes in file.orig
|
|
|
|
sub rename_in_file {
|
|
|
|
my $filename = shift;
|
|
|
|
local($^I, @ARGV) = ('.orig', $filename);
|
|
|
|
while (<>) {
|
|
|
|
foreach my $lkey (keys %literalrenamings2) {
|
|
|
|
# replace all instances of lkey
|
2015-05-05 21:25:35 +00:00
|
|
|
# if (/$lkey/) {print STDOUT "renamed ", $lkey, "\n"; }
|
2015-05-01 21:26:31 +00:00
|
|
|
s/$lkey/$literalrenamings2{$lkey}/g
|
|
|
|
}
|
|
|
|
foreach my $key (keys %renamings) {
|
|
|
|
# replace instances of key, not preceeded by a letter, and not
|
|
|
|
# followed by a letter, number, or '
|
|
|
|
s/(?<![a-zA-z])$key(?![\w'])/$renamings{$key}/g;
|
|
|
|
}
|
|
|
|
foreach my $lkey (keys %literalrenamings) {
|
|
|
|
# replace all instances of lkey
|
|
|
|
s/$lkey/$literalrenamings{$lkey}/g;
|
|
|
|
}
|
|
|
|
print;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my $oldfile = shift;
|
|
|
|
my $newfile = shift;
|
2015-05-07 22:43:10 +00:00
|
|
|
if (-e $newfile) {move($newfile,$newfile.".orig") or die "Move failed: $!"; }
|
2015-05-01 21:26:31 +00:00
|
|
|
print "copying ", $oldfile, " to ",$newfile, ".\n";
|
|
|
|
copy($oldfile,$newfile) or die "Copy failed: $!";
|
|
|
|
get_renamings;
|
|
|
|
# show_renamings;
|
|
|
|
rename_in_file $newfile;
|
|
|
|
unlink $newfile.".orig";
|