Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Brainfunk. With Regex.

by oiskuu (Hermit)
on Dec 10, 2013 at 19:06 UTC ( #1066494=perlmeditation: print w/replies, xml ) Need Help??


my teacher assigned us a brainfuck interpreter[1]. Here's what I've arrived at.

#! /usr/bin/perl ($dpos, $data, $/) = (0, ""); sub data :lvalue { vec($data, $dpos, 8) } <> =~ m{ (?: [>] (?{++$dpos}) | [+] (?{++(data)}) | [.] (?{print chr data}) | [<] (?{--$dpos}) | [-] (?{--(data)}) | [,] (?{data= ord getc}) | [[] (?(?{data})(?0)|(?1)) []] | [^][<>.,+-] )*+\z | (?(?{data})(?0)|(?1)) | \z((?: [^][] | [[] (?1) []])*) }x; BEGIN { *ARGV = *DATA unless @ARGV } __DATA__ +++++[>+++++++++<-],[[>--.++>+<<-]>+.->[<.>-]<<,]

For timing and testing I have used 392quine.b. This, and some other bf programs found here.

I'd say this regex based interpreter is fairly legible and uncomplicated (*cough*), though couple of snags remain.
First and foremost, the unfortunate "recursion" limit is encountered when long (but non-recursing) code is run:
$ perl -w <(perl < Complex regular subexpression recursion limit (32766) exceeded at bf line 5, <> chunk 1. ...
It should output whatever it was fed ( itself), but goes apeshit midway through. The workaround is to use (?:(?:__)*)*; however, I find this just too disgraceful. In this thread, ikegami hints at possible upcoming fix. Well, that was four years ago, and it sure isn't fixed in v5.18.1, let alone v5.12.3. I wonder if this is solved in more recent perls?

Another annoyance is with the data sub. It can be written in several ways

sub data :lvalue { vec($data,$dpos,8) } # v5.16.1--OK; v5.12.3--Fail sub data :lvalue { vec($data,$dpos,8) //= 0 } # OK my @data; sub data :lvalue { $data[$dpos] &= 255 } # OK
The first one appears more expressive, though the array variant is no slower.

Finally, there may well be some important optimizations I've missed or somesuch. Suggestions are welcome, of course.

Replies are listed 'Best First'.
Re: Brainfunk. With Regex.
by choroba (Bishop) on Dec 10, 2013 at 21:08 UTC
    Get your reward!
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Brainfunk. With Regex.
by ambrus (Abbot) on Dec 11, 2013 at 10:36 UTC
Re: Brainfunk. With Regex.
by oiskuu (Hermit) on Jul 01, 2015 at 20:55 UTC

    Shrewd brainfuckers will have noticed that the aforegiven artistry has seemingly run afoul with recent perl incarnations. Versions 5.20+ exhibit severe indigestion upon encountering this following construct:

    $ perl5.20.2 -e '/(?{ ++vec($x,0,1) })/'
    Segmentation fault

    But worry not, my brethren, for the remedy is at hand. Leveraging the remarkable prowess of a technology known as Inline::C, we are proud to have solutioned the next generation, industrial-grade tooling that is bound to meet, if not exceed, the demands of the hardcore brainfucker elite. Here it is, for your perusal, for your enjoyment:

    #! /usr/bin/perl use Inline C => Config => CC => 'clang'; use Digest::SHA q(sha1_hex); open(CODE, '>', \my $code); my $ind = 0; sub o { print CODE " " x $ind, @_, "\n" } sub br { o 'while(*d) {'; ++$ind } sub be { --$ind; o '}' } sub pre { o << ''; ++$ind } #include <stdio.h> static void ich(unsigned char *d) { int c = getc(stdin); c < 0 || (*d += c); } static void och(unsigned char *d) { putc(*d, stdout); } static unsigned char data[30000]; void run() { unsigned char *d = data; undef $/; <> =~ m{ \A (?{pre}) ((?: [>] (?{o '++d;'}) | [+] (?{o '++*d;'}) | [.] (?{o 'och(d);'}) | [<] (?{o '--d;'}) | [-] (?{o '--*d;'}) | [,] (?{o 'ich(d);'}) | [[] (?{br}) (?1) []] (?{be}) | [^][<>.,+-] )*+) \z (?{be}) }x or die "Alzheimer"; Inline->bind(C => $code, NAME => '_'.sha1_hex($code)); run();

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1066494]
Approved by hdb
[choroba]: And regarding SQL, I'm usually generating SQL directly from queries supplied by the user, so I don't see how DBIx::Class can help me.
[1nickt]: Yes, in my (very) limited experience with it, it seems better suited to API building

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (13)
As of 2017-11-24 14:08 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (349 votes). Check out past polls.