Execute Brain****/Elena: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
<lang elena> |
<lang elena>import system'collections. |
||
import system'routines. |
|||
import system'dynamic. |
|||
import extensions. |
|||
import extensions'scripting. |
|||
class BFTape |
class BFTape |
||
{ |
{ |
||
Line 11: | Line 11: | ||
object thePointer. |
object thePointer. |
||
object theBrackets. |
object theBrackets. |
||
constructor new:aLength |
constructor new:aLength |
||
[ |
[ |
||
theArray := Array new:aLength |
theArray := Array new:aLength; populate(:n) [ Integer new:0 ]. |
||
thePointer := Integer new:0. |
thePointer := Integer new:0. |
||
theBrackets := Stack new. |
theBrackets := Stack new. |
||
] |
] |
||
append |
append |
||
[ |
[ |
||
theArray[thePointer] append:1. |
|||
] |
] |
||
⚫ | |||
reduce |
reduce |
||
[ |
[ |
||
theArray[thePointer] reduce:1. |
|||
] |
] |
||
next |
next |
||
[ |
[ |
||
thePointer |
thePointer append:1. |
||
] |
] |
||
previous |
previous |
||
[ |
[ |
||
thePointer |
thePointer reduce:1. |
||
] |
] |
||
push : bookmark |
push : bookmark |
||
[ |
[ |
||
theBrackets push:bookmark. |
theBrackets push:bookmark. |
||
] |
] |
||
pop |
pop |
||
=> theBrackets. |
=> theBrackets. |
||
input |
input |
||
[ |
[ |
||
theArray |
theArray[thePointer] := console readChar; toInt. |
||
] |
] |
||
output |
output |
||
[ |
[ |
||
console write |
console write(theArray[thePointer] toChar). |
||
] |
] |
||
check = theArray |
check = theArray[thePointer] != 0. |
||
} |
} |
||
⚫ | |||
⚫ | |||
program = |
program = |
||
[ |
[ |
||
console |
console |
||
writeLine:bf_program. |
|||
⚫ | |||
var program := scriptEngine |
var program := scriptEngine |
||
load |
load path:"rules.es"; |
||
eval:bf_program. |
|||
⚫ | |||
program eval:(BFTape new:1024). |
program eval:(BFTape new:1024). |
||
].</lang> |
].</lang> |
||
The grammar: |
The grammar: |
||
<lang elena>[[ |
<lang elena>[[ |
||
[[ |
|||
#grammar transform |
#grammar transform |
||
#grammar cf |
#grammar cf |
||
#define start ::= <= ( > += " 2" += " %""system'dynamic'tapeOp. |
#define start ::= <= ( > += " 2" += " %""system'dynamic'tapeOp.tape_var[]""" => commands <= " *" "system'dynamic'Tape" "=" # ) =>; |
||
#define commands ::= command commands; |
#define commands ::= command commands; |
||
Line 91: | Line 92: | ||
#define command ::= <= += " %""append[0]"" " => "+"; |
#define command ::= <= += " %""append[0]"" " => "+"; |
||
#define command ::= <= += " %""reduce[0]"" " => "-"; |
#define command ::= <= += " %""reduce[0]"" " => "-"; |
||
#define command ::= <= += " -2" += " %""system'dynamic'tapeOp. |
#define command ::= <= += " -2" += " %""system'dynamic'tapeOp.tape_ptr[]"" " += " 1" += " %""system'dynamic'tapeOp.tape_stack[]"" " += "%""push[1]"" " => "["; |
||
⚫ | |||
+= "%""push[1]"" " => "["; |
|||
#define command ::= <= += " 0" += " %""system'dynamic'tapeOp.stack&args$[]"" " += " %""check[0]"" " += " 1" += " |
|||
⚫ | |||
#define comment ::= " " comments; |
#define comment ::= " " comments; |
||
Line 105: | Line 104: | ||
#mode symbolic; |
#mode symbolic; |
||
]] |
|||
]]</lang> |
]]</lang> |
||
{{out}} |
|||
<pre> |
|||
ELENA VM 3.2.15 (C)2005-2017 by Alex Rakov |
|||
Initializing... |
|||
Debug mode... |
|||
Done... |
|||
⚫ | |||
Hello World! |
|||
</pre> |
Revision as of 13:06, 7 September 2017
<lang elena>import system'collections. import system'routines. import system'dynamic.
import extensions. import extensions'scripting.
class BFTape {
object theArray. object thePointer. object theBrackets. constructor new:aLength [ theArray := Array new:aLength; populate(:n) [ Integer new:0 ]. thePointer := Integer new:0. theBrackets := Stack new. ] append [ theArray[thePointer] append:1. ] reduce [ theArray[thePointer] reduce:1. ] next [ thePointer append:1. ] previous [ thePointer reduce:1. ] push : bookmark [ theBrackets push:bookmark. ] pop => theBrackets. input [ theArray[thePointer] := console readChar; toInt. ] output [ console write(theArray[thePointer] toChar). ] check = theArray[thePointer] != 0.
}
const bf_program = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.".
program = [
console writeLine:bf_program. var program := scriptEngine load path:"rules.es"; eval:bf_program. program eval:(BFTape new:1024).
].</lang> The grammar: <lang elena>[[
#grammar transform #grammar cf
#define start ::= <= ( > += " 2" += " %""system'dynamic'tapeOp.tape_var[]""" => 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.tape_ptr[]"" " += " 1" += " %""system'dynamic'tapeOp.tape_stack[]"" " += "%""push[1]"" " => "["; #define command ::= <= += " 0" += " %""system'dynamic'tapeOp.tape_stack[]"" " += " %""check[0]"" " += " 1" += " %""system'dynamic'tapeOp.tape_stack[]"" " += " %""pop[0]"" " += " %""system'dynamic'tapeOp.tape_jumpif[]"" " => "]";
#define comment ::= " " comments; #define comment ::= "'" comments; #define comment ::= "!" comments; #define comment ::= $eol;
#define comments ::= $chr comments; #define comments ::= $eps;
#mode symbolic;
]]</lang>
- Output:
ELENA VM 3.2.15 (C)2005-2017 by Alex Rakov Initializing... Debug mode... Done... ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>. Hello World!