Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
Harmonize Perl scripts
Browse files Browse the repository at this point in the history
mkloader.pl and mkboot.pl encode a C string the same way. This job is
now done in the constant() subroutine, identical in both scripts.
  • Loading branch information
lassik authored and omasanori committed Mar 26, 2024
1 parent 1d60ccc commit 7b725f4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 43 deletions.
4 changes: 4 additions & 0 deletions lib/ext/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,10 @@ static const char boot_rom[][80] = {
" ,@(map (lambda (x)\n `(,(the 'define-syntax) ,(car x) ,(",
"cadr x)))\n formal)\n ,@body))))\n\n(define-macro let-syntax\n",
" (lambda (form env)\n `(,(the 'letrec-syntax) ,@(cdr form))))\n",
"",
};


#if PIC_USE_LIBRARY
static const char boot_library_rom[][80] = {
";;; There are two ways to name a library: (foo bar) or foo.bar\n;;; The former is",
Expand Down Expand Up @@ -367,7 +369,9 @@ static const char boot_library_rom[][80] = {
" ((e eval))\n (lambda (expr . lib)\n (let ((lib (if (null? lib",
") (current-library) (car lib))))\n (e expr (library-environment lib)",
")))))\n (make-library '(picrin user))\n (current-library '(picrin user)))\n\n",
"",
};

#endif

void
Expand Down
50 changes: 16 additions & 34 deletions tools/mkboot.pl
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,39 @@

use strict;

# The maximum length of a string literal is 509 characters in C89.
# That is why the boot_rom is split into short strings.
my $chunk = 80;

sub print_escape_char($) {
my $c = shift;
if ($c eq "\n") {
print "\\", "n";
} elsif (($c eq "\\") || ($c eq '"')) {
print "\\", $c;
} else {
print $c;
sub constant($$) {
# The maximum length of a string literal is 509 characters in C89.
# That is why src is split into short strings.
my ($var, $src) = @_;
print "static const char ${var}[][80] = {\n";
my @lines = $src =~ /.{0,80}/gs;
foreach (@lines) {
s/\\/\\\\/g;
s/"/\\"/g;
s/\n/\\n/g;
print "\"$_\",\n";
}
print "};\n\n";
}

local $/ = undef;

print <<EOL;
#include "picrin.h"
#include "picrin/extra.h"
static const char boot_rom[][$chunk] = {
EOL
print "\"";
my $len = 0;
open(IN, "piclib/boot.scm");
while (read(IN, my $c, 1)) {
if ($len && ($len % $chunk == 0)) { print "\",\n\""; }
print_escape_char($c);
$len++;
}
if ($!) { die "read error"; }
constant("boot_rom", <IN>);
close(IN);
print <<EOL;
",
};
#if PIC_USE_LIBRARY
static const char boot_library_rom[][$chunk] = {
EOL
print "\"";
my $len = 0;
open(IN, "piclib/library.scm");
while (read(IN, my $c, 1)) {
if ($len && ($len % $chunk == 0)) { print "\",\n\""; }
print_escape_char($c);
$len++;
}
if ($!) { die "read error"; }
constant("boot_library_rom", <IN>);
close(IN);
print <<EOL;
",
};
#endif
void
Expand Down
25 changes: 16 additions & 9 deletions tools/mkloader.pl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@
use strict;
use File::Basename qw/basename dirname/;

sub constant($$) {
# The maximum length of a string literal is 509 characters in C89.
# That is why src is split into short strings.
my ($var, $src) = @_;
print "static const char ${var}[][80] = {\n";
my @lines = $src =~ /.{0,80}/gs;
foreach (@lines) {
s/\\/\\\\/g;
s/"/\\"/g;
s/\n/\\n/g;
print "\"$_\",\n";
}
print "};\n\n";
}

print <<EOL;
/**
* !!NOTICE!!
Expand All @@ -18,21 +33,13 @@

foreach my $file (@ARGV) {
my $var = &escape_v($file);
print "static const char ${var}[][80] = {\n";

open IN, $file;
local $/ = undef;
my $src = <IN>;
close IN;

my @lines = $src =~ /.{0,80}/gs;
foreach (@lines) {
s/\\/\\\\/g;
s/"/\\"/g;
s/\n/\\n/g;
print "\"$_\",\n";
}
print "};\n\n";
constant($var, $src);
}

print <<EOL;
Expand Down

0 comments on commit 7b725f4

Please sign in to comment.