Adding the official vanilla.nki
[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 my $infile = $ARGV[0];
15 my $count = 0;
16 my $line;
17
18 if (!$infile) { die "Usage: $0 foobar.nki > nki.inf\n"; }
19
20 open (INFILE, "< $infile") || die "$0: Cannot open $infile\n";
21
22 print "! The following code was automatically generated by nki2inf.pl\n";
23 print "! Do not edit this file.\n";
24 print "! Instead, edit your raw NKI list and run nki2inf.pl again.\n";
25 print "!\n";
26 print "! Because you can't directly use double quotes, backslashes,\n";
27 print "! tildes, or atsigns in Inform, the following substitutions\n";
28 print "! are used to generate those characters:\n";
29 print "!    \"  -->   ~\n";
30 print "!    ~  --> \@\@126\n";
31 print "!    \\  --> \@\@92\n";
32 print "!    \@  --> \@\@64\n\n";
33
34 print "[ lookup_msg num;\n";
35 print "\tswitch(num) {\n";
36
37 while (<INFILE>) {
38         $count++;
39         next if /^\s*($|#|!)/;
40         chomp;
41         $line = $_;
42         $line =~ s/"/~/g;
43         $line =~ s/@/\@\@64/g;
44         $line =~ s/\\/\@\@92/g;
45         print "$count:\treturn \"$line\";\n";
46 }
47 print "default: return \"Unknown NKI (this should not happen)\";\n\t}\n];\n";
48 print "Constant MESSAGE_NUM $count;\n";
49 close (INFILE);