picrin/tools/mkloader.pl

74 lines
1.3 KiB
Perl
Raw Permalink Normal View History

2014-05-18 19:40:22 -04:00
#!/usr/bin/perl
2014-05-18 23:58:19 -04:00
use strict;
2014-08-10 22:12:32 -04:00
use File::Basename qw/basename dirname/;
2014-05-18 23:58:19 -04:00
2014-05-18 19:40:22 -04:00
print <<EOL;
/**
* !!NOTICE!!
* This file was automatically generated by mkloader.pl, and includes all of
* the prelude files required by Picrin. PLEASE DO NOT EDIT THIS FILE, changes
* will be overwritten the next time the script runs.
*/
2014-05-18 19:40:22 -04:00
#include "picrin.h"
2016-02-20 10:58:58 -05:00
#include "picrin/extra.h"
2014-05-18 19:40:22 -04:00
EOL
2014-05-19 00:39:28 -04:00
foreach my $file (@ARGV) {
2014-05-18 19:40:22 -04:00
my $var = &escape_v($file);
2015-01-31 07:14:14 -05:00
print "static const char ${var}[][80] = {\n";
2014-05-18 19:40:22 -04:00
open IN, $file;
2015-01-31 07:14:14 -05:00
local $/ = undef;
my $src = <IN>;
close IN;
my @lines = $src =~ /.{0,80}/gs;
foreach (@lines) {
2014-05-18 19:40:22 -04:00
s/\\/\\\\/g;
s/"/\\"/g;
2015-01-31 07:14:14 -05:00
s/\n/\\n/g;
print "\"$_\",\n";
2014-05-18 19:40:22 -04:00
}
2015-01-31 07:14:14 -05:00
print "};\n\n";
2014-05-18 19:40:22 -04:00
}
print <<EOL;
void
pic_load_piclib(pic_state *pic)
{
pic_value e;
2014-05-18 19:40:22 -04:00
EOL
2014-05-19 00:39:28 -04:00
foreach my $file (@ARGV) {
print <<EOL;
pic_try {
EOL
2014-05-18 19:40:22 -04:00
my $var = &escape_v($file);
2014-08-10 22:12:32 -04:00
my $basename = basename($file);
my $dirname = basename(dirname($file));
2015-01-31 07:14:14 -05:00
print " pic_load_cstr(pic, &${var}[0][0]);\n";
2014-08-10 22:12:32 -04:00
print<<EOL
2014-05-18 19:40:22 -04:00
}
pic_catch(e) {
2014-05-18 19:40:22 -04:00
/* error! */
2016-06-19 15:49:01 -04:00
pic_fputs(pic, "fatal error: failure in loading $dirname/$basename\\n", pic_stderr(pic));
pic_raise(pic, e);
2014-05-18 19:40:22 -04:00
}
2014-08-10 22:12:32 -04:00
EOL
}
print <<EOL;
2014-05-18 19:40:22 -04:00
}
EOL
sub escape_v {
2014-05-18 23:58:11 -04:00
($_) = @_;
2014-05-18 19:40:22 -04:00
s/\.scm$//g;
2014-05-19 00:39:28 -04:00
s/[^[A-Za-z0-9_]/_/g;
"piclib_src_" . $_;
2014-05-18 19:40:22 -04:00
}