Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: Eval Squared

by truedfx (Monk)
on Apr 06, 2006 at 09:27 UTC ( [id://541606]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://541606]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-03-29 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found