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

m
no edit summary
No edit summary
imported>Arakov
mNo edit summary
 
(9 intermediate revisions by 3 users not shown)
Line 1:
ELENA 6.x:
<lang elena>#import system'collections.
#<syntaxhighlight lang="elena">import system'routines.collections;
#import system'dynamic.routines;
import system'dynamic'expressions;
 
#import extensions.;
#import extensions'scripting.;
 
const bf_program = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.";
class BFTape
{
object theArray.
object thePointer.
object theBrackets.
 
class TapeAssembler
constructor new:aLength
{ [
Stack _brackets;
theArray := Array new:aLength set &every: (&int:n) [ Integer new:0 ].
List<Expression> _tape;
constructor()
{
_brackets := new Stack();
_tape := new List<Expression>();
_tape.append(Expression.DeclareAndAssigning(
thePointer := Integer new:0.
"ptr",
theBrackets := Stack new Expression.Constant(0)));
]}
 
append
[
(theArray@thePointer) += 1.
]
constructor load(assembly_program)
reduce
[{
assembly_program(self)
(theArray@thePointer) -= 1.
]}
nextopen()
[{
thePointer += 1_brackets.push(_tape);
_tape := new List<Expression>();
]
}
 
previous
[
thePointer -= 1.
]
push : bookmarkclose()
[{
theBracketsvar loop push:bookmark= Expression.Loop(
Expression.MessageCall(
]
new Message("notequal[2]"),
Expression.MessageCall(
new Message("at[2]"),
Expression.Variable("tape"),
Expression.Variable("ptr")
),
Expression.Constant($0)
),
CodeblockExpression.new(_tape.Value));
_tape := _brackets.pop();
_tape.append(loop)
}
popinput()
{
=> theBrackets.
_tape.append(
Expression.MessageCall(
new Message("setAt[3]"),
Expression.Variable("tape"),
Expression.Variable("ptr"),
Expression.MessageCall(
new Message("readChar[1]"),
Expression.Constant(console)
)
)
)
}
inputoutput()
[{
_tape.append(
theArray@thePointer := console readChar toInt.
Expression.MessageCall(
]
new Message("write[2]"),
Expression.Constant(console),
Expression.MessageCall(
new Message("at[2]"),
Expression.Variable("tape"),
Expression.Variable("ptr")
)
)
)
}
outputnext()
[{
_tape.append(
console write:((theArray@thePointer) toChar).
Expression.Assigning(
]
"ptr",
 
Expression.MessageCall(
check = theArray@thePointer != 0.
new Message("add[2]"),
Expression.Variable("ptr"),
Expression.Constant(1))))
}
previous()
{
_tape.append(
Expression.Assigning(
"ptr",
Expression.MessageCall(
new Message("subtract[2]"),
Expression.Variable("ptr"),
Expression.Constant(1))))
}
increase()
{
_tape.append(
Expression.MessageCall(
new Message("setAt[3]"),
Expression.Variable("tape"),
Expression.Variable("ptr"),
Expression.MessageCall(
new Message("load[2]"),
Expression.Constant(CharValue),
Expression.MessageCall(
new Message("add[2]"),
Expression.MessageCall(
new Message("toInt[2]"),
Expression.Constant(intConvertExt),
Expression.MessageCall(
new Message("at[2]"),
Expression.Variable("tape"),
Expression.Variable("ptr")
)
),
Expression.Constant(1)
)
)
));
}
decrease()
{
_tape.append(
Expression.MessageCall(
new Message("setAt[3]"),
Expression.Variable("tape"),
Expression.Variable("ptr"),
Expression.MessageCall(
new Message("load[2]"),
Expression.Constant(CharValue),
Expression.MessageCall(
new Message("subtract[2]"),
Expression.MessageCall(
new Message("toInt[2]"),
Expression.Constant(intConvertExt),
Expression.MessageCall(
new Message("at[2]"),
Expression.Variable("tape"),
Expression.Variable("ptr")
)
),
Expression.Constant(1)
)
)
));
}
compiled()
{
var program := DynamicSingleton.load(
Expression.Method(
"eval",
CodeblockExpression.new(_tape.Value),
ScopeIdentifier.Variable("tape"))
).compile();
^(tape){ program.eval(tape) }
}
}
 
public program()
program =
{
[
var bfAssemblyProgram := new ScriptEngine()
console
.loadPath("asmrules.es")
writeLine:"++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.".
.buildScript(bf_program);
 
var program := scriptEngine
var bfProgram := TapeAssembler.load(bfAssemblyProgram).compiled();
load &path:"rules.es"
 
eval:"++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.".
var bfTape := Array.allocate(1024).populate::(n => $0);
 
console.writeLine(bf_program);
 
bfProgram(bfTape)
program eval:(BFTape new:1024).
}</syntaxhighlight>
].</lang>
The grammar:
<langsyntaxhighlight lang="elena">[[
#grammar build
[[
#grammar transform
#grammar cf
 
#define start ::=
#define start ::= <= ( > += " 2" += " %""system'dynamic'tapeOp.var&args$[]""" => commands <= " *" "system'dynamic'Tape" "=" == ) =>;
<=
system'dynamic'DynamicTape (
system'dynamic'AllocFunction ( "1" )
system'dynamic'LocalFunction ( "2" ) => command commands
<= ) =>;
 
#define start ::= $eof;
 
#define commands ::= command commands;
Line 85 ⟶ 204:
#define commands ::= $eof;
 
#define command ::= <= +=system'dynamic'MessageFunction "( %""output[01]"" " ) => ".";
#define command ::= <= +=system'dynamic'MessageFunction "( %""input[01]"" " ) => ",";
#define command ::= <= +=system'dynamic'MessageFunction "( %""previous[01]"" ") => "<";
#define command ::= <= +=system'dynamic'MessageFunction "( %""next[01]"" " ) => ">";
#define command ::= <= +=system'dynamic'MessageFunction "( %""appendincrease[01]"" ") => "+";
#define command ::= <= +=system'dynamic'MessageFunction "( %""reducedecrease[01]"" ") => "-";
#define command ::= <= += " -2" += " %""system'dynamic'tapeOp.ptr&args$MessageFunction ( "open[1]"" " += " 1" +) => " %""system'dynamic'tapeOp.stack&args$[]"" " ;
#define command ::= <= system'dynamic'MessageFunction ( += "%""pushclose[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;
Line 105 ⟶ 222:
 
#mode symbolic;
]]</syntaxhighlight>
]]
{{out}}
 
]]</langpre>
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
Hello World!
</pre>
Anonymous user