Advertisement
Guest User

Untitled

a guest
May 9th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 4.43 KB | None | 0 0
  1. --
  2. -- BFI - BrainFuck Interpreter
  3. -- Copyright (C) 2009 Jonathan Tremesaygues
  4. --
  5. -- ToDo List :
  6. --  - stocker les instructions du programme dans une liste doublement chainée
  7. --  - résoudre le bug de la dernière ligne vide
  8. --  - bouclage de la mémoire (?)
  9. --    + Liste doublement chainée bouclé
  10. --    + ou correspondance (si case 30000 demandée, aller en 0; si case -1 demandée, aller en 29999
  11. --
  12. -- Licence :
  13. -- This program is free software; you can redistribute it and/or modify
  14. -- it under the terms of the GNU General Public License as published by
  15. -- the Free Software Foundation; either version 2 of the License, or
  16. -- (at your option) any later version.
  17. --
  18. -- This program is distributed in the hope that it will be useful,
  19. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. -- GNU General Public License for more details.
  22. --
  23. -- You should have received a copy of the GNU General Public License
  24. -- along with this program; if not, write to the Free Software
  25. -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. --
  27. --
  28. -- Program Name:  BFI
  29. -- Version:       0.0.1
  30. -- Date:          2009-08-13
  31. -- Description:   Interpreter for the Brainfuck Programming Language
  32. -- License:       GPL
  33. -- Web page:      None
  34. -- Download:      None
  35. -- Source Info:   None
  36. -- Latest Ver:    None
  37. -- Documentation: None
  38. -- Help:          killruana@gmail.com
  39. -- Developement:  killruana@gmail.com
  40. -- Bugs:          killruana@gmail.com
  41. -- Maintainer:    Jonathan Tremesaygues <killruana@gmail.com>
  42. -- Developer:     Jonathan Tremesaygues <killruana@gmail.com>
  43. -- Interfaces:    Command Line
  44. -- Source Lang:   Ada
  45. -- Build Prereq:  None
  46. -- Related Progs: None
  47. -- Category:      Software Development > Programming languages
  48. --
  49.  
  50. with Ada.Text_Io; use Ada.Text_Io;
  51. with Ada.Command_Line; use Ada.Command_Line;
  52.  
  53. procedure bfi is
  54.    source : File_Type;
  55.    val : Character;
  56.  
  57.    l : Integer := 0;
  58.  
  59.    p : array (0..999999) of Character; -- Tableau des instructions
  60.    pc : Integer := 0;  -- "Pointeur" sur l'instruction actuelle
  61.    pl : Integer := 0;  -- Nombres d'instructions
  62.  
  63.    x : array (0..29999) of Integer := (others=>0); -- Ruban de la mémoire
  64.    xc : Integer := 0; -- "Pointeur" sur la case mémoire actuelle
  65.  
  66. begin
  67.    if (Argument_Count = 1) then -- Bon nombre d'arguments ?
  68.       Open(source, In_File, Argument(1)); -- Ouverture du fichier
  69.  
  70.       -- Copie de la source en mémoire
  71.       while (not End_Of_File(source)) loop
  72.          Get(source, val); -- Lecture d'un caractère
  73.          if (val = '+' or val = '-' or val = '.' or val = ',' or val = '>'
  74.                or val = '<' or val = '[' or val = ']') then -- Filtre pour récupérer instructions
  75.             p(pl) := val;
  76.             pl :=  pl + 1;
  77.          end if;
  78.       end loop;
  79.  
  80.       Close(source); -- Fermeture du fichier
  81.  
  82.       -- Tant qu'il y a des instructions à traiter...
  83.       while (pc < pl) loop
  84.          if (p(pc) = '+') then -- Incrémente la case mémoire
  85.             x(xc) := x(xc) + 1;
  86.          elsif (p(pc) = '-') then -- Décrémente la case mémoire
  87.             x(xc) := x(xc) - 1;
  88.          elsif (p(pc) = '.') then -- Affichage du contenu la case mémoire
  89.             Put(Character'Val(x(xc)));
  90.          elsif (p(pc) = ',') then -- Met une valeur dans la case mémoire
  91.             Put(">");
  92.             Get(val);
  93.             x(xc) := Character'Pos(val);
  94.          elsif (p(pc) = '>') then -- Avance d'une case mémoire
  95.             xc := xc + 1;
  96.          elsif (p(pc) = '<') then -- Recule d'une case mémoire
  97.             xc := xc - 1;
  98.          elsif (p(pc) = '[') then
  99.             if (x(xc) = 0) then
  100.                pc := pc + 1;
  101.                while (l > 0 or p(pc) /= ']') loop
  102.                   if (p(pc) = '[') then l := l + 1; end if;
  103.                   if (p(pc) = ']') then l := l - 1; end if;
  104.                   pc := pc + 1;
  105.                end loop;
  106.             end if;
  107.          elsif (p(pc) = ']') then
  108.             pc := pc - 1;
  109.             while (l > 0 or p(pc) /= '[') loop
  110.                if (p(pc) = '[') then l := l - 1; end if;
  111.                if (p(pc) = ']') then l := l + 1; end if;
  112.                pc := pc - 1;
  113.             end loop;
  114.             pc := pc - 1;
  115.          end if;
  116.          pc := pc + 1;
  117.       end loop;
  118.    else
  119.       -- Affichage de l'aide
  120.       Put_Line("Usage :");
  121.       Put_Line("   bfi <fichier source>");
  122.    end if;
  123. end bfi;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement