Anonymous user
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 TapeAssembler
{
stack theBrackets.
list<TapeExpression> theTape.
constructor new
[
theBrackets := Stack new.
theTape := list<TapeExpression>().
theTape append(TapeExpression Declaring("ptr")).
theTape append(TapeExpression Assigning("ptr", TapeExpression Constant(0))).
]
constructor new : assembly_program
<= new;
[
]
open
[
theBrackets push(theTape).
theTape := list<TapeExpression>().
]
[
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(
TapeExpression Constant(console),
"write",
TapeExpression MessageCall(
TapeExpression Variable("tape"),
"getAt",
TapeExpression Variable("ptr")
))).
]
next
[
"ptr",
TapeExpression MessageCall(
TapeExpression Variable("ptr"),
"add",
TapeExpression Constant(1)))).
]
previous
[
theTape append(TapeExpression Assigning(
"ptr",
TapeExpression MessageCall(
TapeExpression Variable("ptr"),
"subtract",
TapeExpression Constant(1)))).
]
[
theTape append(TapeExpression MessageCall(
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))))).
]
[
theTape append(TapeExpression MessageCall(
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))))).
]
[
var program := TapeExpression Singleton(
TapeExpression Method(
"eval",
TapeExpression Code(theTape array),
TapeExpression Parameter("tape"))).
var o := (program compiled)().
^(:tape) [ o eval(tape) ]
]
}
Line 68 ⟶ 159:
console
writeLine:bf_program.
var
load path:"
eval
var bfProgram := TapeAssembler new(bfAssemblyProgram); get.
var bfTape := Array new:1024; populate(:n)<int>($0).
bfProgram(bfTape).
].</lang>
The grammar:
Line 80 ⟶ 175:
#grammar cf
#define start ::= <= ( >
#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 ::= <= += " %""
#define command ::= <= += " %""
#define command ::= <=
#define command ::= <=
#define comment ::= " " comments;
|