Execute Brain****/Elena: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 7: | Line 7: | ||
#import extensions'scripting. |
#import extensions'scripting. |
||
⚫ | |||
// --- Tape --- |
|||
⚫ | |||
{ |
{ |
||
object theArray. |
|||
object thePointer. |
|||
object theBrackets. |
|||
constructor new:aLength |
|||
[ |
[ |
||
theArray := Array new:aLength set &every: (&int:n) [ Integer new:0 ]. |
theArray := Array new:aLength set &every: (&int:n) [ Integer new:0 ]. |
||
Line 24: | Line 22: | ||
] |
] |
||
append |
|||
[ |
[ |
||
(theArray@thePointer) += 1. |
(theArray@thePointer) += 1. |
||
] |
] |
||
reduce |
|||
[ |
[ |
||
(theArray@thePointer) -= 1. |
(theArray@thePointer) -= 1. |
||
] |
] |
||
next |
|||
[ |
[ |
||
thePointer += 1. |
thePointer += 1. |
||
] |
] |
||
previous |
|||
[ |
[ |
||
thePointer -= 1. |
thePointer -= 1. |
||
] |
] |
||
push : bookmark |
|||
[ |
[ |
||
theBrackets push:bookmark. |
theBrackets push:bookmark. |
||
] |
] |
||
pop |
|||
=> theBrackets. |
=> theBrackets. |
||
input |
|||
[ |
[ |
||
theArray@thePointer := console readChar toInt. |
theArray@thePointer := console readChar toInt. |
||
] |
] |
||
output |
|||
[ |
[ |
||
console write:((theArray@thePointer) toChar). |
console write:((theArray@thePointer) toChar). |
||
] |
] |
||
check = theArray@thePointer != 0. |
|||
} |
} |
||
program = |
|||
[ |
[ |
||
console |
console |
||
writeLine:"++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.". |
writeLine:"++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.". |
||
var program := scriptEngine |
|||
load &path:"rules.es" |
load &path:"rules.es" |
||
eval:"++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.". |
eval:"++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.". |
||
Line 78: | Line 76: | ||
The grammar: |
The grammar: |
||
<lang elena>[[ |
<lang elena>[[ |
||
[[ |
|||
#grammar transform |
|||
#grammar cf |
#grammar cf |
||
#define start ::= <= |
#define start ::= <= ( > += " 2" += " %""system'dynamic'tapeOp.var&args$[]""" => commands <= " *" "system'dynamic'Tape" "=" == ) =>; |
||
#define commands ::= command commands; |
#define commands ::= command commands; |
||
Line 86: | Line 86: | ||
#define commands ::= $eof; |
#define commands ::= $eof; |
||
#define command ::= <= %"output[0]" => "."; |
#define command ::= <= += " %""output[0]"" " => "."; |
||
#define command ::= <= %"input[0]" => ","; |
#define command ::= <= += " %""input[0]"" " => ","; |
||
#define command ::= <= %"previous[0]" => "<"; |
#define command ::= <= += " %""previous[0]"" " => "<"; |
||
#define command ::= <= %"next[0]" => ">"; |
#define command ::= <= += " %""next[0]"" " => ">"; |
||
#define command ::= <= %"append[0]" => "+"; |
#define command ::= <= += " %""append[0]"" " => "+"; |
||
#define command ::= <= %"reduce[0]" => "-"; |
#define command ::= <= += " %""reduce[0]"" " => "-"; |
||
#define command ::= <= -2 %"system'dynamic'tapeOp.ptr&args$[]" 1 %"system'dynamic'tapeOp.stack&args$[]" |
#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" += " |
|||
⚫ | |||
#define comment ::= " " comments; |
#define comment ::= " " comments; |
||
Line 104: | Line 106: | ||
#mode symbolic; |
#mode symbolic; |
||
]] |
|||
]]</lang> |
]]</lang> |
Revision as of 20:01, 24 March 2017
<lang elena>#import system.
- 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 set &every: (&int:n) [ Integer new:0 ]. thePointer := Integer new:0. theBrackets := Stack new. ]
append [ (theArray@thePointer) += 1. ] reduce [ (theArray@thePointer) -= 1. ] next [ thePointer += 1. ]
previous [ thePointer -= 1. ] push : bookmark [ theBrackets push:bookmark. ] pop => theBrackets. input [ theArray@thePointer := console readChar toInt. ] output [ console write:((theArray@thePointer) toChar). ]
check = theArray@thePointer != 0.
}
program = [
console writeLine:"++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.". var program := scriptEngine load &path:"rules.es" eval:"++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.".
program eval:(BFTape new:1024).
].</lang> The grammar: <lang elena>[[ [[
#grammar transform #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>