New NKIs!
[rfk-inform.git] / nki2inf.pl
1 #!/usr/bin/perl -w
2
3 # Usage: nk2inf.pl foobar.nki > nki.inf
4
5 # The Z-machine wants all of a program's data to be in the executable 
6 # file.  For the Inform edition of robotfindskitten, nki's are generated 
7 # by a very long case statement.  This script emits that case statement 
8 # and a constant defining MESSAGE_NUM to the number of NKIs found.  The 
9 # result is then added to kitten.inf with an Include statement.
10 #
11 # This script automatically takes care of '`', '\', and '@' by replacing 
12 # them with '@@126', '@@92', and '@@64' respectively.
13
14 foreach my $infile (@ARGV) {
15         if (! -r $infile) { die "$0: Unable to read $infile\n"; }
16 }
17
18 my $count = 0;
19 my $line;
20
21 if (!$ARGV[0]) { die "Usage: $0 <files> > nki.inf\n"; }
22
23
24 print "! The following code was automatically generated by nki2inf.pl\n";
25 print "! Do not edit this file.\n";
26 print "! Instead, edit your raw NKI list and run nki2inf.pl again.\n";
27 print "!\n";
28 print "! Because you can't directly use double quotes, backslashes,\n";
29 print "! tildes, or atsigns in Inform, the following substitutions\n";
30 print "! are used to generate those characters:\n";
31 print "!    \"  -->   ~\n";
32 print "!    ~  --> \@\@126\n";
33 print "!    \\  --> \@\@92\n";
34 print "!    \@  --> \@\@64\n\n";
35
36 print "[ lookup_msg num;\n";
37 print "\tswitch(num) {\n";
38
39 foreach my $infile (@ARGV) {
40         open (INFILE, "< $infile") || die "$0: Cannot open $infile\n";
41
42         while (<INFILE>) {
43                 $count++;
44                 next if /^\s*($|#|!)/;
45                 chomp;
46                 $line = $_;
47                 $line =~ s/"/~/g;
48                 $line =~ s/@/\@\@64/g;
49                 $line =~ s/\\/\@\@92/g;
50                 print "$count:\treturn \"$line\";\n";
51         }
52 }
53 print "default: return \"Unknown NKI (this should not happen)\";\n\t}\n];\n";
54 print "Constant MESSAGE_NUM $count;\n";
55 close (INFILE);