script/plotz.pl untested

filter: Statementstmt 0.0% 0 / 43 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 3 Podpod n/a totaltotal 0.0% 0 / 71 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl -w
2
3 =head1 NAME
4
5 plotz - Poly-Lingual Opcode Translator for the Z-machine
6
7 =head1 SYNOPSIS
8
9 plotz.pl storyfile[.z?|dat] [output_suffix]
10
11 If you don't give a suffix to the storyfile, plotz will look for a file
12 with that root, and suffix .z? or .dat.
13
14 output_suffix can be pl, pir, or xml. It tells plotz which language to
15 translate to. Perl is the default.
16
17 =head1 DESCRIPTION
18
19 plotz translates a Z-code story file into another language. That file can
20 then be (compiled and) run to replicate the functionality of running a
21 standard Z-code interpreter like Zip or Frotz on the story file.
22
23 Since the disassembling of the Z-file will be the same no matter what your
24 output language is, in theory you can add a new output language with only half
25 the work. (OK, 90% of the work.) The first attempts to make use of this will
26 be a Perl and a PIR (Parrot Intermediate Language) translation.
27
28 aka Perpetrating Linguistics On The Z-machine.
29
30 =cut
31
32 use strict;
33 use Language::Zcode::Parser; # parse Z-file
34 use Language::Zcode::Translator; # language-specific output routines
35
36 ################################
37 # Get args
38 my %e2l = (
39 "pl" => "Perl",
40 "imc" => "PIR",
41 "pir" => "PIR",
42 "xml" => "XML",
43 );
44 my $known = join "|", keys %e2l;
45
46 my $Usage = <<ENDUSAGE;
47 plotz.pl storyfile[.z?|dat] [$known]
48
49 The last arg is the extension for the output file, pl by default.
50 The extension tells plotz which language to translate to.
51 ENDUSAGE
52 die "$Usage\n" if @ARGV == 0 || @ARGV > 2;
53
54 my ($infile, $extension) = @ARGV;
55 die "$Usage\n" if defined $extension && !exists $e2l{$extension};
56 $extension ||= "pl"; # .pl is default output
57
58 ################################
59 # Read in the Z-file, store it in memory, parse the header
60 my $Parser = new Language::Zcode::Parser "Perl";
61 # If they didn't put ".z5" at the end, find it anyway
62 $infile = $Parser->find_zfile($infile) || exit;
63 $Parser->read_memory($infile);
64 $Parser->parse_header();
65
66 # Create a translator object that will write code in the correct
67 # language (e.g. Plotz::Translator::Perl).
68 my $language = $e2l{$extension}
69 or die "Unknown extension $extension. I know ",join(" ",keys %e2l);
70 my $T = new Language::Zcode::Translator $language;
71
72 ################################
73 # Start the output program
74 (my $outfile = $infile) =~ s/\.(z\d+|dat)$/.$extension/i;
75 open(POUT, ">$outfile");
76 print POUT $T->program_start();
77
78 # Get routines, each of which is made up of commands.
79 # Parse each Z-routine, then print its translation
80 print "Finding routines...";
81 my @subs = $Parser->find_subs($infile); # addr of each sub, commands not parsed
82 # TODO print only if -v? Or -b to be brief?
83 print scalar @subs, " routines to translate.\n";
84 my $i = 0;
85 print "Translating...";
86 for my $rtn (@subs) {
87 $rtn->parse();
88 print POUT $T->routine_start($rtn->address, $rtn->locals); # e.g."sub foo {"
89 print POUT $T->translate_command($_) for $rtn->commands;
90 print POUT $T->routine_end(); # e.g., "}"
91 print "$i.." unless ++$i % 100;
92 }
93
94 # Add in any language-specific support routines (possibly from other files)
95 print POUT $T->library();
96
97 # Print the code that stores the zfile (e.g. as uunecoded text) and
98 # can unpack it back to bytes.
99 print POUT $T->write_memory();
100
101 print POUT $T->program_end();
102
103 close(POUT);
104 print "Done!\nOutput file is $outfile\n";
105 exit;
106
107 =head1 AUTHOR
108
109 Amir Karger (akarger@cpan.org)
110
111 =head1 LICENSE
112
113 Copyright (c) 2003-4 Amir Karger. All rights reserved. This program is free
114 software; you can redistribute it and/or modify it under the same terms as
115 Perl itself.
116
117 =head1 NOTES
118
119 Plotz is a Yiddish word describing what your mother does when she finds out
120 you're wasting your life playing computer games instead of becoming a doctor.
121 And you couldn't call once in a while?
122
123 Plotz is the name of a machine company in Cleveland.
124
125 Plotz was used on the rec.arts.int-fiction newsgroup in 1999 to describe that
126 indescribable something that interactive fiction games have that draws you in
127 and keeps you playing even if there isn't an actual plot.
128
129 =cut