MyBF - Interpretador de BrainFuck

Publicado por Danillo Souza (última atualização em 26/08/2010)

[ Hits: 5.127 ]

Download mybf




Pequeno interpretador da linguagem BrainFuck que eu criei, implementando mais 2 comandos adicionais (a descrição se encontra nos comentários do início). Para quem não conhece BrainFuck: http://pt.wikipedia.org/wiki/Brainfuck

  



Esconder código-fonte

#!/usr/bin/perl -w

# Author: Danillo Souza
# Email:  danillo012@gmail.com
# Version: 0.1
#
# This script implements the original syntax of BrainF*ck, with few modifications,
# only two additional commands actually. The first is the '!' serving
# to write the value of the current cell as an integer, and the second is '$'
# used to read a number (actually, one digit) and store your value in the current cell.
# 
# If you extend this script with new functionalities, please send me the
# changes via email. I would be happy if I saw someone improving this little
# interpreter and adding new stuff.
# 
# And do not forget. if you extend this interpreter, do not use modules not
# are patterns of Perl. The goal is to ensure that the interpreter is 100% portable,
# not force people to install new modules.

use strict;
use diagnostics;

# Opening file
die "Usage: mybf.pl <scriptfile>\n" if scalar(@ARGV) < 1;
open my $file, '<', $ARGV[0] or die "Can't open file.\n";

# Variables
my $top = 30000;
my $ptr = 0;
my $cou = 0;
my $src = '';
my @segment = (1..$top); foreach (@segment){$_ = 0;}
my @loops = ();

# Getting the source code
while (<$file>) {$src .= $_;}
close($file);

# Eliminating non-bf characters
$src =~ s/[^\+\-\.\!\[\],\$<>]//g;

# Verifying the brackets equivalency
my $lbrackets = scalar(grep(/[\[]/g, $src));
my $rbrackets = scalar(grep(/[\]]/g, $src));
print "Missing brackets -> ([: $lbrackets | ]: $lbrackets)\n" if ($lbrackets != $rbrackets);

# Execution loop
while ($cou < length($src)) {
   my $ch = substr($src, $cou, 1);$cou++;
   if ($ch eq '>') {$ptr++;if ($ptr>256){$ptr=0}}
   if ($ch eq '<') {$ptr--;if ($ptr<0){$ptr=256}}
   if ($ch eq '+') {$segment[$ptr]++}
   if ($ch eq '-') {$segment[$ptr]--}
   if ($ch eq '.') {print chr($segment[$ptr]);}
   if ($ch eq '!') {print int($segment[$ptr]);}
   if ($ch eq ',') {my $y=<STDIN>;$segment[$ptr]=ord(substr($y,0,1));}
   if ($ch eq '$') {my $y=<STDIN>;$segment[$ptr]=int(substr($y,0,1));}
   if ($ch eq '[') {push(@loops, $cou)}
   if ($ch eq ']') {if ($segment[$ptr]!=0){$cou=$loops[$#loops]}else{pop(@loops)}}
}

Scripts recomendados

Mega Sena

Diminuir ou aumentar o brilho de notebook

Configurar ADB no linux

Verificação se utilizador existe ou não

Índice (Logaritmo Discreto) em Perl


  

Comentários
[1] Comentário enviado por removido em 03/03/2016 - 21:06h

Bem legível.
Muito bom.

----------------------------------------------------------------------------------------------------------------
http://24.media.tumblr.com/tumblr_m62bwpSi291qdlh1io1_250.gif

# apt-get purge systemd (não é prá digitar isso!)

Encryption works. Properly implemented strong crypto systems are one of the few things that you can rely on. Unfortunately, endpoint security is so terrifically weak that NSA can frequently find ways around it. — Edward Snowden


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts