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

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
Line 1: Line 1:
<lang elena>#define system.
<lang elena>#define system.
#define system'routines.
#define extensions'text.
#define extensions'text.


// --- Tape ---
// --- Tape ---


#class Tape
#class BFTape
{
{
#field theArray.
#field theArray.
#field thePointer.
#field thePointer.


#constructor new &type'length:aLength
#constructor new &length:aLength
[
[
thePointer := Integer new:0.
thePointer := Integer new:0.


theArray := Array new &type'length:aLength &function: &&:n [ Integer new:0 ].
theArray := arrayControl new &length:aLength &each: n [ Integer new:0 ].
]
]


#method type'tape = $self.
#method bf_tape = $self.


#method append
#method append
Line 40: Line 41:
#method input
#method input
[
[
(theArray@thePointer) write &type'int:(console readChar).
(theArray@thePointer) write &int:(console readChar).
]
]
Line 69: Line 70:
]
]
#method type'tape = theTape type'tape.
#method bf_tape = theTape bf_tape.


#method append
#method append
Line 103: Line 104:
#method repeatUntil
#method repeatUntil
[
[
theTape run: &&:aTape [ interpreter'Interpreter new:aTape eval:theLoopBody ].
theTape run: aTape [ interpreter'Interpreter new:aTape eval:theLoopBody ].
^ theTape.
^ theTape.
Line 125: Line 126:
]
]
#method eval &type'widestr:aLiteral
#method eval &literal:aLiteral
[
[
control foreach:aLiteral &do:$self.
control foreach:aLiteral &do:$self.
]
]


#method eval &widechar:aChar
#method eval &char:aChar
[
[
aChar =>
aChar =>
Line 148: Line 149:
#symbol program =
#symbol program =
[
[
('program'arguments Count == 1)?
('program'arguments length == 1)?
[ console write:"Please provide the path to the file to interpret". #throw BreakException new. ].
[ console write:"Please provide the path to the file to interpret". #throw BreakException new. ].
textFileControl forEachLine:('program'arguments@1) &do:(Interpreter new:(Tape new &type'length:1024)).
textFileControl forEachLine:('program'arguments@1) &do:(Interpreter new:(BFTape new &length:1024)).
].
].</lang>
</lang>

Revision as of 07:53, 15 January 2014

<lang elena>#define system.

  1. define system'routines.
  2. define extensions'text.

// --- Tape ---

  1. class BFTape

{

   #field theArray.
   #field thePointer.
   #constructor new &length:aLength
   [
       thePointer := Integer new:0.
       theArray := arrayControl new &length:aLength &each: n [ Integer new:0 ].
   ]
   #method bf_tape = $self.
   #method append
   [
        (theArray@thePointer) += 1.
   ]
   
   #method reduce
   [
        (theArray@thePointer) -= 1.
   ]
   
   #method next
   [
       thePointer += 1.
   ]
   #method previous
   [
       thePointer -= 1.
   ]
   
   #method input
   [
       (theArray@thePointer) write &int:(console readChar).
   ]
   
   #method output
   [
       console write:(CharValue new:(theArray@thePointer)).
   ]
   #method run : aLoop
   [
       control while:[ 0 < (theArray@thePointer) ] &do: [ aLoop eval:self ].
   ]
           
   #method get = theArray@thePointer.

}

// --- LoopInterpreter ---

  1. class LoopInterpreter

{

   #field theLoopBody.
   #field theTape.
   
   #constructor new &tape:aTape
   [
       theTape := aTape.
       theLoopBody := String new.
   ]
   
   #method bf_tape = theTape bf_tape.
   #method append
   [
        theLoopBody += "+".
   ]
   
   #method reduce
   [
        theLoopBody += "-".
   ]
   
   #method next
   [
        theLoopBody += ">".
   ]
   #method previous
   [
        theLoopBody += "<".
   ]
   
   #method input
   [
        theLoopBody += ",".
   ]
   
   #method output
   [
        theLoopBody += ".".
   ]
   
   #method repeatUntil
   [
       theTape run: aTape [ interpreter'Interpreter new:aTape eval:theLoopBody ].
           
       ^ theTape.
   ]

}

// --- Interpreter ---

  1. class Interpreter

{

   #field theTape.
   #constructor new : aTape
   [
       theTape := aTape.
   ]
   #method eval : anObject
   [
       $self eval::anObject.
   ]
       
   #method eval &literal:aLiteral
   [
       control foreach:aLiteral &do:$self.
   ]
   #method eval &char:aChar
   [
       aChar =>
           ">" ? [ theTape next ]
           "<" ? [ theTape previous ]
           "+" ? [ theTape append ]
           "-" ? [ theTape reduce ]
           "." ? [ theTape output. ]
           "," ? [ theTape input. ]
           "[" ? [ theTape := LoopInterpreter new &tape:theTape. ]
           "]" ? [ theTape := theTape repeatUntil. ].
   ]

}

// --- Program ---

  1. symbol program =

[

   ('program'arguments length == 1)?
       [  console write:"Please provide the path to the file to interpret". #throw BreakException new. ].
   
   textFileControl forEachLine:('program'arguments@1) &do:(Interpreter new:(BFTape new &length:1024)).

].</lang>