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

no edit summary
No edit summary
No edit summary
Line 5:
import extensions.
import extensions'scripting.
import extensions'dynamic'expressions.
 
class BFTape
class TapeAssembler
{
{
object theArray.
stack theBrackets.
object thePointer.
list<TapeExpression> theTape.
object theBrackets.
constructor new:aLength
[
theArray := Array new:aLength; populate(:n) [ Integer new:0 ].
thePointer := Integer new:0.
theBrackets := Stack new.
theTape := list<TapeExpression>().
theTape append(TapeExpression Declaring("ptr")).
theTape append(TapeExpression Assigning("ptr", TapeExpression Constant(0))).
]
constructor new : assembly_program
append
<= new;
[
theArray[thePointer] append:1assembly_program($self).
]
open
[
theBrackets push(theTape).
theTape := list<TapeExpression>().
]
reduceclose
[
var loop := TapeExpression Loop(
TapeExpression MessageCall(
TapeExpression MessageCall(
TapeExpression Variable("tape"),
"getAt",
TapeExpression Variable("ptr")
),
"notequal",
TapeExpression Constant($0)),
TapeExpression Code(theTape array)).
theTape := theBrackets pop.
theTape append(loop).
]
input
[
theTape append(TapeExpression MessageCall(
TapeExpression Variable("tape"),
"setAt",
TapeExpression Variable("ptr"),
TapeExpression MessageCall(
TapeExpression Constant(console),
"readChar"
))).
]
output
[
theTape append(TapeExpression MessageCall(
theArray[thePointer] reduce:1.
TapeExpression Constant(console),
"write",
TapeExpression MessageCall(
TapeExpression Variable("tape"),
"getAt",
TapeExpression Variable("ptr")
))).
]
next
[
thePointertheTape append:1.(TapeExpression Assigning(
"ptr",
TapeExpression MessageCall(
TapeExpression Variable("ptr"),
"add",
TapeExpression Constant(1)))).
]
previous
[
theTape append(TapeExpression Assigning(
thePointer reduce:1.
"ptr",
TapeExpression MessageCall(
TapeExpression Variable("ptr"),
"subtract",
TapeExpression Constant(1)))).
]
push : bookmarkincrease
[
theTape append(TapeExpression MessageCall(
theBrackets push:bookmark.
TapeExpression Variable("tape"),
"setAt",
TapeExpression Variable("ptr"),
TapeExpression MessageCall(
TapeExpression Constant(CharValue),
"new",
TapeExpression MessageCall(
TapeExpression MessageCall(
TapeExpression Constant(convertor),
"toInt",
TapeExpression MessageCall(
TapeExpression Variable("tape"),
"getAt",
TapeExpression Variable("ptr"))
),
"add",
TapeExpression Constant(1))))).
]
popdecrease
=> theBrackets.
input
[
theTape append(TapeExpression MessageCall(
theArray[thePointer] := console readChar; toInt.
TapeExpression Variable("tape"),
"setAt",
TapeExpression Variable("ptr"),
TapeExpression MessageCall(
TapeExpression Constant(CharValue),
"new",
TapeExpression MessageCall(
TapeExpression MessageCall(
TapeExpression Constant(convertor),
"toInt",
TapeExpression MessageCall(
TapeExpression Variable("tape"),
"getAt",
TapeExpression Variable("ptr"))
),
"subtract",
TapeExpression Constant(1))))).
 
]
outputget
[
var program := TapeExpression Singleton(
console write(theArray[thePointer] toChar).
TapeExpression Method(
"eval",
TapeExpression Code(theTape array),
TapeExpression Parameter("tape"))).
var o := (program compiled)().
^(:tape) [ o eval(tape) ]
]
check = theArray[thePointer] != 0.
}
 
Line 68 ⟶ 159:
console
writeLine:bf_program.
 
var programbfAssemblyProgram := scriptEngine
load path:"rulesasmrules.es";
eval:(bf_program).
 
var bfProgram := TapeAssembler new(bfAssemblyProgram); get.
program eval:(BFTape new:1024).
 
var bfTape := Array new:1024; populate(:n)<int>($0).
 
bfProgram(bfTape).
].</lang>
The grammar:
Line 80 ⟶ 175:
#grammar cf
 
#define start ::= <= ( > += " 2" += " %""system'dynamic'tapeOp.tape_var[]""" => commands <= " *" "system'dynamic'Tape"ClosureTape= "=" # ) =>;
 
#define commands ::= command commands;
Line 86 ⟶ 181:
#define commands ::= $eof;
 
#define command ::= <= += " %""output[0]"" system'dynamic'MessageClosure ^""new[1]"" " => ".";
#define command ::= <= += " %""input[0]"" system'dynamic'MessageClosure ^""new[1]"" " => ",";
#define command ::= <= += " %""previous[0]"" system'dynamic'MessageClosure ^""new[1]"" " => "<";
#define command ::= <= += " %""next[0]"" system'dynamic'MessageClosure ^""new[1]"" " => ">";
#define command ::= <= += " %""appendincrease[0]"" system'dynamic'MessageClosure ^""new[1]"" " => "+";
#define command ::= <= += " %""reducedecrease[0]"" system'dynamic'MessageClosure ^""new[1]"" " => "-";
#define command ::= <= += " -2" += " %""system'dynamic'tapeOp.tape_ptropen[0]"" " += " 1" += " %""system'dynamic'tapeOp.tape_stack[]""MessageClosure " += "%^""pushnew[1]"" " => "[";
#define command ::= <= += " 0" += " %""system'dynamic'tapeOp.tape_stack[]"" " += " %""checkclose[0]"" " += " 1" += " %""system'dynamic'tapeOp.tape_stack[]""MessageClosure ^" += " %""pop[0]"" " += " %""system'dynamic'tapeOp.tape_jumpifnew[1]"" " => "]";
 
#define comment ::= " " comments;
Anonymous user