Execute Brain****/Elena

From Rosetta Code

<lang elena>#import system.

  1. import system'collections.
  2. import system'routines.
  3. import system'dynamic.
  1. import extensions.
  2. import extensions'scripting.

// --- Tape ---

  1. class BFTape

{

   #field theArray.
   #field thePointer.
   #field theBrackets.
   #constructor new:aLength
   [
       theArray := Array new:aLength set &every: (&int:n) [ Integer new:0 ].
       
       thePointer := Integer new:0.
       
       theBrackets := Stack new.
   ]
   #method append
   [
        (theArray@thePointer) += 1.
   ]
   
   #method reduce
   [
        (theArray@thePointer) -= 1.
   ]
   
   #method next
   [
       thePointer += 1.
   ]
   #method previous
   [
       thePointer -= 1.
   ]
   
   #method push : bookmark
   [
       theBrackets push:bookmark.
   ]
   
   #method pop
       => theBrackets.
   
   #method input
   [
       theArray@thePointer := console readChar toInt.
   ]
   
   #method output
   [
       console write:((theArray@thePointer) toChar).
   ]
   #method check = theArray@thePointer != 0.

}

  1. symbol program =

[

   console 
      writeLine:"++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.".
                             
   #var program := scriptEngine 
       load &path:"rules.es"
       eval:"++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.".
   program eval:(BFTape new:1024).

].</lang> The grammar: <lang elena>[[

  #grammar cf
  #define start      ::= <= [ 2 %"system'dynamic'tapeOp.var&args$[]" => commands <= * system'dynamic'Tape ] =>;
  #define commands   ::= command commands;
  #define commands   ::= comment commands;
  #define commands   ::= $eof;
  #define command    ::= <= %"output[0]" => ".";
  #define command    ::= <= %"input[0]" => ",";
  #define command    ::= <= %"previous[0]" => "<";
  #define command    ::= <= %"next[0]" => ">";
  #define command    ::= <= %"append[0]" => "+";
  #define command    ::= <= %"reduce[0]" => "-";
  #define command    ::= <= -2 %"system'dynamic'tapeOp.ptr&args$[]" 1 %"system'dynamic'tapeOp.stack&args$[]" %"push[1]" => "[";
  #define command    ::= <= 0 %"system'dynamic'tapeOp.stack&args$[]" %"check[0]" 1 %"system'dynamic'tapeOp.stack&args$[]" %"pop[0]" %"system'dynamic'tapeOp.jumpif&args$[13]" => "]";
  #define comment    ::= " " comments;
  #define comment    ::= "'" comments;
  #define comment    ::= "!" comments;
  #define comment    ::= $eol;
  #define comments   ::= $chr comments;
  #define comments   ::= $eps;
  #mode symbolic;

]]</lang>