From 9fde950ff4a787e952fdc8432369eae1d313f1dc Mon Sep 17 00:00:00 2001 From: David Griffith Date: Sun, 18 Nov 2012 19:07:09 -0800 Subject: [PATCH] nki2inf.pl for preparing nki files from upstream into a form Inform can digest. --- nki2inf.pl | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 nki2inf.pl diff --git a/nki2inf.pl b/nki2inf.pl new file mode 100755 index 0000000..5b9f3c0 --- /dev/null +++ b/nki2inf.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl -w + +# Usage: nk2inf.pl foobar.nki > nki.inf + +# The Z-machine wants all of a program's data to be in the executable +# file. For the Inform edition of robotfindskitten, nki's are generated +# by a very long case statement. This script emits that case statement +# and a constant defining MESSAGE_NUM to the number of NKIs found. The +# result is then added to kitten.inf with an Include statement. +# +# This script automatically takes care of '`', '\', and '@' by replacing +# them with '@@126', '@@92', and '@@64' respectively. + +my $infile = $ARGV[0]; +my $count = 0; +my $line; + +if (!$infile) { die "Usage: $0 foobar.nki > nki.inf\n"; } + +open (INFILE, "< $infile") || die "$0: Cannot open $infile\n"; + +print "! The following code was automatically generated by nki2inf.pl\n"; +print "! Do not edit this file.\n"; +print "! Instead, edit your raw NKI list and run nki2inf.pl again.\n"; +print "!\n"; +print "! Because you can't directly use double quotes, backslashes,\n"; +print "! tildes, or atsigns in Inform, the following substitutions\n"; +print "! are used to generate those characters:\n"; +print "! \" --> ~\n"; +print "! ~ --> \@\@126\n"; +print "! \\ --> \@\@92\n"; +print "! \@ --> \@\@64\n\n"; + +print "[ lookup_msg num;\n"; +print "\tswitch(num) {\n"; + +while () { + $count++; + next if /^\s*($|#|!)/; + chomp; + $line = $_; + $line =~ s/"/~/g; + $line =~ s/@/\@\@64/g; + $line =~ s/\\/\@\@92/g; + print "$count:\treturn \"$line\";\n"; +} +print "default: return \"Unknown NKI (this should not happen)\";\n\t}\n];\n"; +print "Constant MESSAGE_NUM $count;\n"; +close (INFILE); -- 2.31.1