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

m
Fixed syntax highlighting.
m (RCBF (Ada) moved to RCBF/Ada: now using subpages)
m (Fixed syntax highlighting.)
 
(6 intermediate revisions by 5 users not shown)
Line 1:
{{implementation|Brainf***}}{{collection|RCBF}}[[Category:Ada]]
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.
 
The program reads input data from the standard input and writes output data to the standard output. Error messages go to the standard error output. Of course, these channels can be redirected to files, as usual, when invoking the BF interpreter.
 
The program is stored internally in a fixed length array of characters. Programs larger than the array size cannot be processed.
 
The internal data memory is implemented as a fixed length array of integers. Programs that attempattempt to go beyond the memory limits will stop with an error message at run time.
 
To allow using the interpreter in an interactive terminal session, control characters are ignored on input. But they can be generated on output, like any other ASCII character.
Line 16:
More detailed information about the code can be found in the comments throughout it.<br clear=all>
 
<syntaxhighlight lang="ada">-- BF Interpreter
 
-- Usage: bf programfile[.bf]
Line 159:
end if;
 
when ']' => -- (]) end of loop sructurestructure
if M(Mp) /= 0 then
-- repeat loop, goto matching '['
Line 185:
New_Line;
 
end Bf;</adasyntaxhighlight>
===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.
<syntaxhighlight 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;
</syntaxhighlight>
===Test programs===
====Hello world====
<syntaxhighlight 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;
</syntaxhighlight>
Sample output:
<pre>
Hello World!
</pre>
====Bracket test====
<syntaxhighlight 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;
</syntaxhighlight>
Sample output:
<pre>
@
</pre>
9,476

edits