Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- -- BFI - BrainFuck Interpreter
- -- Copyright (C) 2009 Jonathan Tremesaygues
- --
- -- ToDo List :
- -- - stocker les instructions du programme dans une liste doublement chainée
- -- - résoudre le bug de la dernière ligne vide
- -- - bouclage de la mémoire (?)
- -- + Liste doublement chainée bouclé
- -- + ou correspondance (si case 30000 demandée, aller en 0; si case -1 demandée, aller en 29999
- --
- -- Licence :
- -- This program is free software; you can redistribute it and/or modify
- -- it under the terms of the GNU General Public License as published by
- -- the Free Software Foundation; either version 2 of the License, or
- -- (at your option) any later version.
- --
- -- This program is distributed in the hope that it will be useful,
- -- but WITHOUT ANY WARRANTY; without even the implied warranty of
- -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- -- GNU General Public License for more details.
- --
- -- You should have received a copy of the GNU General Public License
- -- along with this program; if not, write to the Free Software
- -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- --
- --
- -- Program Name: BFI
- -- Version: 0.0.1
- -- Date: 2009-08-13
- -- Description: Interpreter for the Brainfuck Programming Language
- -- License: GPL
- -- Web page: None
- -- Download: None
- -- Source Info: None
- -- Latest Ver: None
- -- Documentation: None
- -- Help: killruana@gmail.com
- -- Developement: killruana@gmail.com
- -- Bugs: killruana@gmail.com
- -- Maintainer: Jonathan Tremesaygues <killruana@gmail.com>
- -- Developer: Jonathan Tremesaygues <killruana@gmail.com>
- -- Interfaces: Command Line
- -- Source Lang: Ada
- -- Build Prereq: None
- -- Related Progs: None
- -- Category: Software Development > Programming languages
- --
- with Ada.Text_Io; use Ada.Text_Io;
- with Ada.Command_Line; use Ada.Command_Line;
- procedure bfi is
- source : File_Type;
- val : Character;
- l : Integer := 0;
- p : array (0..999999) of Character; -- Tableau des instructions
- pc : Integer := 0; -- "Pointeur" sur l'instruction actuelle
- pl : Integer := 0; -- Nombres d'instructions
- x : array (0..29999) of Integer := (others=>0); -- Ruban de la mémoire
- xc : Integer := 0; -- "Pointeur" sur la case mémoire actuelle
- begin
- if (Argument_Count = 1) then -- Bon nombre d'arguments ?
- Open(source, In_File, Argument(1)); -- Ouverture du fichier
- -- Copie de la source en mémoire
- while (not End_Of_File(source)) loop
- Get(source, val); -- Lecture d'un caractère
- if (val = '+' or val = '-' or val = '.' or val = ',' or val = '>'
- or val = '<' or val = '[' or val = ']') then -- Filtre pour récupérer instructions
- p(pl) := val;
- pl := pl + 1;
- end if;
- end loop;
- Close(source); -- Fermeture du fichier
- -- Tant qu'il y a des instructions à traiter...
- while (pc < pl) loop
- if (p(pc) = '+') then -- Incrémente la case mémoire
- x(xc) := x(xc) + 1;
- elsif (p(pc) = '-') then -- Décrémente la case mémoire
- x(xc) := x(xc) - 1;
- elsif (p(pc) = '.') then -- Affichage du contenu la case mémoire
- Put(Character'Val(x(xc)));
- elsif (p(pc) = ',') then -- Met une valeur dans la case mémoire
- Put(">");
- Get(val);
- x(xc) := Character'Pos(val);
- elsif (p(pc) = '>') then -- Avance d'une case mémoire
- xc := xc + 1;
- elsif (p(pc) = '<') then -- Recule d'une case mémoire
- xc := xc - 1;
- elsif (p(pc) = '[') then
- if (x(xc) = 0) then
- pc := pc + 1;
- while (l > 0 or p(pc) /= ']') loop
- if (p(pc) = '[') then l := l + 1; end if;
- if (p(pc) = ']') then l := l - 1; end if;
- pc := pc + 1;
- end loop;
- end if;
- elsif (p(pc) = ']') then
- pc := pc - 1;
- while (l > 0 or p(pc) /= '[') loop
- if (p(pc) = '[') then l := l - 1; end if;
- if (p(pc) = ']') then l := l + 1; end if;
- pc := pc - 1;
- end loop;
- pc := pc - 1;
- end if;
- pc := pc + 1;
- end loop;
- else
- -- Affichage de l'aide
- Put_Line("Usage :");
- Put_Line(" bfi <fichier source>");
- end if;
- end bfi;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement