Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
* Make Prelude.pm self-sufficient, and re-introduce file2c.pl (from P…
…AR::Packer).
  • Loading branch information
audreyt committed Feb 18, 2012
1 parent 27f1db4 commit 36d2ba8
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Pugs/src/perl6/Prelude.pm
Expand Up @@ -26,7 +26,8 @@ the `is builtin` trait.

=cut

use Math::Basic :GLOBAL<pi>;
#use Math::Basic :GLOBAL<pi>;
sub pi() is export(:constants) {3.141592653589793}

class Process {
multi sub exec($prog, @args) returns Bool is builtin is primitive is unsafe {
Expand Down
76 changes: 76 additions & 0 deletions Pugs/utils/file2c.pl
@@ -0,0 +1,76 @@
#!/usr/bin/perl -w

# Copyright (c) 2002 Mattia Barbon.
# Copyright (c) 2002 Audrey Tang.
# This package is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.

use strict;
use FindBin;
use lib "$FindBin::Bin/../lib";
use File::Basename;
use Getopt::Long;

my $chunk_size = 0;
my $strip_pod = 0;
my $compress = 0;
my $name;

GetOptions(
"c|chunk-size=i" => \$chunk_size,
"s|strip" => \$strip_pod,
"z|compress" => \$compress,
"n|name=s" => \$name)
&& @ARGV == 3
or die "Usage: $0 [-c CHUNK][-n NAME][-s][-z] file.pl file.c c_variable\n";
my ($pl_file, $c_file, $c_var) = @ARGV;
$name = basename($pl_file) unless defined $name;

my $pl_text = do # a scalar reference
{
open my $in, "<", $pl_file or die "open input file '$pl_file': $!";
binmode $in;
local $/ = undef;
my $slurp = <$in>;
close $in;
\$slurp;
};

open my $out, ">", $c_file or die "open output file '$c_file': $!";
binmode $out;

my $len = length($$pl_text);

# add a NUL byte so that chunk_${c_var} may be used as C string
print_chunk( $$pl_text, "" );

print $out <<"...";
const char * text_$c_var () { return((const char *)$c_var); }
const int size_$c_var () { return $len; }
...

close $out;

exit 0;


sub print_chunk
{
my ($chunk, $suffix) = @_;

my $len = length($chunk);
print $out <<"...";
static unsigned char ${c_var}[] = {
...

for (my $i = 0; $i < $len; $i++) {
printf $out "0x%02x,", ord(substr($chunk, $i, 1));
print $out "\n" if $i % 16 == 15;
}

print $out "};\n";
}

# local variables:
# mode: cperl
# end:

0 comments on commit 36d2ba8

Please sign in to comment.