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

Content added Content deleted
(Alternative implementation as a callable procedure)
m (Fixed syntax highlighting.)
 
Line 1: Line 1:
{{implementation|Brainf***}}{{collection|RCBF}}
{{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]].
This is a simple implementation of [[Brainf***]] (it will be called just BF in what follows). It is written in [[Ada]].
=Standalone interpreter=
===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.
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 16: Line 16:
More detailed information about the code can be found in the comments throughout it.<br clear=all>
More detailed information about the code can be found in the comments throughout it.<br clear=all>


<lang ada>-- BF Interpreter
<syntaxhighlight lang="ada">-- BF Interpreter


-- Usage: bf programfile[.bf]
-- Usage: bf programfile[.bf]
Line 185: Line 185:
New_Line;
New_Line;


end Bf;</lang>
end Bf;</syntaxhighlight>
=Callable interpreter=
===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.
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">
<lang Ada>
with Ada.Streams; use Ada.Streams;
with Ada.Streams; use Ada.Streams;
with Ada.Text_IO.Text_Streams; use Ada.Text_IO.Text_Streams;
with Ada.Text_IO.Text_Streams; use Ada.Text_IO.Text_Streams;
Line 255: Line 255:
end loop;
end loop;
end BF;
end BF;
</syntaxhighlight>
</lang>
==Test programs==
===Test programs===
===Hello world===
====Hello world====
<syntaxhighlight lang="ada">
<lang Ada>
with System.Storage_Elements; use System.Storage_Elements;
with System.Storage_Elements; use System.Storage_Elements;
with BF;
with BF;
Line 267: Line 267:
BF ("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.", Memory);
BF ("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.", Memory);
end Test_BF_Hello;
end Test_BF_Hello;
</syntaxhighlight>
</lang>
Sample output:
Sample output:
<pre>
<pre>
Hello World!
Hello World!
</pre>
</pre>
===Bracket test===
====Bracket test====
<syntaxhighlight lang="ada">
<lang Ada>
with System.Storage_Elements; use System.Storage_Elements;
with System.Storage_Elements; use System.Storage_Elements;
with BF;
with BF;
Line 282: Line 282:
BF (">>++++[<++++[<++++>-]>-]<<.[-]++++++++++.", Memory);
BF (">>++++[<++++[<++++>-]>-]<<.[-]++++++++++.", Memory);
end Test_BF;
end Test_BF;
</syntaxhighlight>
</lang>
Sample output:
Sample output:
<pre>
<pre>