in reply to Eval Squared

So, it looks good. First pass, can't parse. Fine. perl -MO=Deparse doesn't help either. So I try a new trick from a fellow monk (instead of performing the intermediary steps manually):

$ sed s/eval/print/ zshzn.pl | perl

which yields:

open$:,shift;$/=undef;$_=<$:>;><+-.,cd;s!(.)!$1 !g;s!((?:<\s) {2,})!'$ +b-='.(length($1)/2).";"!gex;s!((?:>\s) {2,})!'$b+='.(length($1)/2)."; +"!gex;s!((?:\+\s){2,})!'$a[$b]+='.(length($1)/2).";"!ge;s!((?:\-\s){2 +,})!'$a[$b]-='.(length($1)/2).";"!ge;s!>\s !\$b++;!gx;s!<\s !\$b--;!g +x;s!([+-])\s!\$a[\$b]$1$1;\n!g;s!\.\s!print chr(\$a[\$b]);!g;s!,\s !\ +$a[\$b]=ord(substr(<>,0,1));!gx;s!\[\s!while(\$a[\$b]){!g;s!(?<=\s)\] +\s!}!g;print;

Now, I don't particularly feel like formatting all that by hand, so I:

$ s/eval/print/ | perl | perl -MO=Deparse

which yields:

syntax error at - line 1, near ";>" - had compilation errors.

So I fail challenge #1.

So I try the following:

$ perl zshzn.pl $ perl zshzn.pl file.txt $ cat file.txt | perl zshzn.pl $ perl zshzn.pl 10

All of which do nothing.

So the answer to #1 changes to: you're just doing an expensive noop that compiles cleanly the first time around. You produce no output by default, or when given input by a number of means. So I think that makes the answer to #2: the mistake in the implementation is that you've forgotten to do anything!!! :-)

So as far as what this member of the monastery thinks, it reminds of the dirty joke about the man who goes to the doctor looking awful but feeling great... But in this case, the appearance is quite titillating, but serves no useful practical purpose to me, which almost makes me expect to see another obfuscation, equally as appealing, abreast this one.



--chargrill
$,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s +plit//=>$* ){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$# +C]=$/;($#C >$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^ +$$C[$%++]}

Replies are listed 'Best First'.
Re^2: Eval Squared
by truedfx (Monk) on Apr 06, 2006 at 09:27 UTC
    syntax error at - line 1, near ";>"
    - had compilation errors.

    That's odd. What I get is:

    sed s/eval/print/ t.pl | perl | perl -MO=Deparse open $:, shift @ARGV; $/ = undef; $_ = <$:>; tr/+-.<>[]//cd; s/(.)/$1 /g; s[((?:<\s) {2,})]['$b-=' . length($1) / 2 . ';';]egx; s[((?:>\s) {2,})]['$b+=' . length($1) / 2 . ';';]egx; s[((?:\+\s){2,})]['$a[$b]+=' . length($1) / 2 . ';';]eg; s[((?:\-\s){2,})]['$a[$b]-=' . length($1) / 2 . ';';]eg; s/>\s /\$b++;/gx; s/<\s /\$b--;/gx; s/([+-])\s/\$a[\$b]$1$1;\n/g; s/\.\s/print chr(\$a[\$b]);/g; s/,\s /\$a[\$b]=ord(substr(<>,0,1));/gx; s/\[\s/while(\$a[\$b]){/g; s/(?<=\s)\]\s/}/g; eval $_;

    This looks like a simple Brainfuck interpreter. Which makes me think one mistake is s/(?<=\s)\]\s/}/g; -- the (?<=\s) should be dropped. I first thought tr/+-.<>[]//cd; was bad too, but that I misread; +-. of course means "everything from + to .", which in ASCII means "+ , - .".

    Update: The behaviour for , doesn't look right either. That should probably be done with something like

    $/ = undef; $_ = <$:>; $/ = \1; [...] s/,\s /\$a[\$b]=ord(<>);/gx; [...]

    The current behaviour is not, strictly speaking, a bug. You can enter complete strings by entering the EOF marker (^D or ^Z) after each character. It is needlessly complicated though.

      As the original code is changing char-by-char the character for it's chr(ord - 1)... It actually only works when the user has the same character encoding of the OP...

      It seems that the line you deparsed as

      tr/+-.<>[]//cd;

      Came to me just as

      ><+-.,cd;

      I'll try to get more about it...

      daniel
Re^2: Eval Squared
by zshzn (Hermit) on Apr 06, 2006 at 12:20 UTC
    It doesn't do anything until you know how to tell it what to do. Input is indeed broken, that's the one limitation I didn't go much into. Not only is it not characteristic brainfuck user input, but it's not very effective as-is anyways.