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

m
Fixed syntax highlighting.
(Alternative implementation as a callable procedure)
m (Fixed syntax highlighting.)
 
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 16:
More detailed information about the code can be found in the comments throughout it.<br clear=all>
 
<langsyntaxhighlight lang="ada">-- BF Interpreter
 
-- Usage: bf programfile[.bf]
Line 185:
New_Line;
 
end Bf;</langsyntaxhighlight>
===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">
<lang Ada>
with Ada.Streams; use Ada.Streams;
with Ada.Text_IO.Text_Streams; use Ada.Text_IO.Text_Streams;
Line 255:
end loop;
end BF;
</syntaxhighlight>
</lang>
===Test programs===
====Hello world====
<syntaxhighlight lang="ada">
<lang Ada>
with System.Storage_Elements; use System.Storage_Elements;
with BF;
Line 267:
BF ("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.", Memory);
end Test_BF_Hello;
</syntaxhighlight>
</lang>
Sample output:
<pre>
Hello World!
</pre>
====Bracket test====
<syntaxhighlight lang="ada">
<lang Ada>
with System.Storage_Elements; use System.Storage_Elements;
with BF;
Line 282:
BF (">>++++[<++++[<++++>-]>-]<<.[-]++++++++++.", Memory);
end Test_BF;
</syntaxhighlight>
</lang>
Sample output:
<pre>
9,476

edits