Execute Brain****/Ada: Difference between revisions

Alternative implementation as a callable procedure
(Alternative implementation as a callable procedure)
Line 1:
{{implementation|Brainf***}}{{collection|RCBF}}
This is a simple implementation of [[Brainf***]] (it will be called just BF in what follows). It is written in [[Ada]].
=Standalone interpreter=
 
This implementation imposes hard limits in the size of both the memory and the program size. The whole program is read first, and then interpreted. The program must be stored in a file. The filename is given as the only argument to the interpreter.
 
Line 186:
 
end Bf;</lang>
=Callable interpreter=
This implementation provides a procedure that can be called to interpret a [[Brainf***]] stored in a string. The memory is passed as a parameter. Input and output of the memory cells is stream. By default the input and output streams are used. The interpreter uses the native machine memory. Upon errors such as addressing errors and program errors (unclosed brackets) Constraint_Error is propagated.
<lang Ada>
with Ada.Streams; use Ada.Streams;
with Ada.Text_IO.Text_Streams; use Ada.Text_IO.Text_Streams;
with Ada.Text_IO; use Ada.Text_IO;
with System.Storage_Elements; use System.Storage_Elements;
 
procedure BF
( Source : String;
Memory : in out Storage_Array;
Input : access Root_Stream_Type'Class := Stream (Standard_Input);
Output : access Root_Stream_Type'Class := Stream (Standard_Output)
) is
subtype Address is Storage_Offset range Memory'Range;
PC : Address := Address'First;
Index : Integer := Source'First;
Nesting : Natural := 0;
begin
while Index <= Source'Last loop
case Source (Index) is
when '>' => -- Increment PC
PC := PC + 1;
when '<' => -- Decrement PC
PC := PC - 1;
when '+' => -- Increment at PC
Memory (PC) := Memory (PC) + 1;
when '-' => -- Decrement at PC
Memory (PC) := Memory (PC) - 1;
when '.' => -- Output at PC
Storage_Element'Write (Output, Memory (PC));
when ',' => -- Input at PC
Storage_Element'Read (Input, Memory (PC));
when '[' => -- Forward if zero at PC
if Memory (PC) = 0 then
loop
Index := Index + 1;
case Source (Index) is
when '[' =>
Nesting := Nesting + 1;
when ']' =>
exit when Nesting = 0;
Nesting := Nesting - 1;
when others =>
null;
end case;
end loop;
end if;
when ']' => -- Backward if non-zero at PC
if Memory (PC) /= 0 then
loop
Index := Index - 1;
case Source (Index) is
when '[' =>
exit when Nesting = 0;
Nesting := Nesting - 1;
when ']' =>
Nesting := Nesting + 1;
when others =>
null;
end case;
end loop;
end if;
when others => -- Comment
null;
end case;
Index := Index + 1;
end loop;
end BF;
</lang>
==Test programs==
===Hello world===
<lang Ada>
with System.Storage_Elements; use System.Storage_Elements;
with BF;
 
procedure Test_BF_Hello is
Memory : Storage_Array := (0..100_000 => 0);
begin
BF ("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.", Memory);
end Test_BF_Hello;
</lang>
Sample output:
<pre>
Hello World!
</pre>
===Bracket test===
<lang Ada>
with System.Storage_Elements; use System.Storage_Elements;
with BF;
 
procedure Test_BF is
Memory : Storage_Array := (0..100_000 => 0);
begin
BF (">>++++[<++++[<++++>-]>-]<<.[-]++++++++++.", Memory);
end Test_BF;
</lang>
Sample output:
<pre>
@
</pre>