#!/bin/perl # Thutu2 (Thutu wimpmode) to Perl reference compiler. # Preamble. print << "EOF"; #!/bin/perl # Perl output generated from a Thutu2 file. EOF print << 'EOF'; $_="=1"; $row=""; $ninequit=0; while(!$ninequit) { $row =~ s/=/=q/g; $row =~ s/\t/=t/g; $row =~ s/\n/=x/g; # The newline at the end of the line becomes =x $row =~ s/\r/=r/g; $row =~ s/\f/=f/g; $row =~ s/\a/=a/g; $row =~ s/\e/=e/g; $row =~ s/([!-\/:-<>-@[-`{-~])/=$1/g; #` defined($row) or $row = '=9'; $_ = $row . $_; while(1) { EOF # The main loop. $ilo=0; $ilocheck=0; while(<>) { chomp; # An input newline will be =n, representing a newline is \n s/\t/ /; /^ *\#/ and next; # Ignore comments (lines starting with #). s/^( *)//; print $1; # Format the output with the same indentation as the input. $ilo > length $1 and print "last;}};\n"; # Indentation shows looping. $ilo < length $1 and $ilocheck and die "Indentation increased illegaly."; $ilo = length $1; $ilocheck = 1; @regexps = split /(?$2;' . '$c=chr($1);$e=$1==$2;$q=0;$2 and $q=$1/$2;$v=int($q);};'."\n"; } elsif($lastexp eq "\$") # String function library { print 'do{$c=ord($1);$e=$1eq$2;$g=$1gt$2;$u=uc($1);$l=lc($1);'. '$g=length($1);};'."\n"; } elsif($lastexp eq "|") # Immediately output $1, no de-escaping { print 'print $1;' . "\n"; } elsif($lastexp eq "<") # Jump back to the start of the block { print "next;\n"; } elsif($lastexp eq ">") # Jump out of this block { print "last;\n"; } elsif($lastexp eq "@") # If the guards are met, loop within this block. { # The guards are only checked at the start. $ilo++; $ilocheck = 0; print "do {while(1) {\n"; } elsif($lastexp eq "^") # If no guards are met, loop within this block. { $ilo++; $ilocheck = 0; print "0 or do {while(1) {\n"; } elsif($lastexp eq "!") # Loop while a replacement but no guards match. { $ilo++; $ilocheck = 0; print "0)) {do {\n"; } elsif($lastexp eq "*") # Loop while the guards and a replacement match. { $ilo++; $ilocheck = 0; print "1) {do {\n"; } elsif($lastexp eq ".") # Dedentation marker for multiple dedents {} # Do nothing. else {die "Unrecognized row modifier."}; }; # Finishing off. print << 'EOF'; last; } s/=9// and $ninequit=1; if(s/(.*?)=x//) # =x marks the end of what to print out. { $orow=$1; $orow =~ s/=t/\t/g; $orow =~ s/=n/\n/g; # =n converts back to newline. $orow =~ s/=r/\r/g; $orow =~ s/=f/\f/g; $orow =~ s/=a/\a/g; $orow =~ s/=e/\e/g; $orow =~ s/=([!-\/:-<>-@[-`{-~])/$1/g; #` $orow =~ s/=q/=/g; # print "\n"; # DEBUG print $orow; } # Perl changes input source on EOF, but we want to continue in an EOF # condition after EOF is reached. $ninequit or ($row eq '=9' and scalar($row = undef, 1)) or $row=<>; }; EOF