in reply to Re: Eval Squared
in thread Eval Squared

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.

Replies are listed 'Best First'.
Re^3: Eval Squared
by ruoso (Curate) on Apr 10, 2006 at 22:39 UTC

    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