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

m
no edit summary
mNo edit summary
imported>Arakov
mNo edit summary
 
(5 intermediate revisions by 3 users not shown)
Line 1:
ELENA 6.x:
<langsyntaxhighlight lang="elena">import system'collections;
import system'routines;
import system'dynamic'expressions;
Line 10 ⟶ 11:
class TapeAssembler
{
Stack theBrackets_brackets;
List<Expression> theTape_tape;
constructor()
{
theBrackets_brackets := new Stack();
theTape_tape := new List<Expression>();
theTape_tape.append(Expression.DeclareAndAssigning(
new ScopeVariable("ptr"),
Expression.Constant(0)));
}
Line 30 ⟶ 31:
open()
{
theBrackets_brackets.push(theTape_tape);
theTape_tape := new List<Expression>();
}
Line 46 ⟶ 47:
Expression.Constant($0)
),
ExpressionCodeblockExpression.CodeBlocknew(theTape_tape.Value));
theTape_tape := theBrackets_brackets.pop();
theTape_tape.append(loop)
}
input()
{
theTape_tape.append(
Expression.MessageCall(
new Message("setAt[3]"),
Expression.Variable(new ScopeVariable("tape")),
Expression.Variable(new ScopeVariable("ptr")),
Expression.MessageCall(
new Message("readChar[1]"),
Expression.Constant(console)
)
)
)
Line 69 ⟶ 70:
output()
{
theTape_tape.append(
Expression.MessageCall(
new Message("write[2]"),
Line 75 ⟶ 76:
Expression.MessageCall(
new Message("at[2]"),
Expression.Variable(new ScopeVariable("tape")),
Expression.Variable(new ScopeVariable("ptr"))
)
)
Line 84 ⟶ 85:
next()
{
theTape_tape.append(
Expression.Assigning(
new ScopeVariable("ptr"),
Expression.MessageCall(
new Message("add[2]"),
Expression.Variable(new ScopeVariable("ptr")),
Expression.Constant(1))))
}
Line 95 ⟶ 96:
previous()
{
theTape_tape.append(
Expression.Assigning(
new ScopeVariable("ptr"),
Expression.MessageCall(
new Message("subtract[2]"),
Expression.Variable(new ScopeVariable("ptr")),
Expression.Constant(1))))
}
Line 106 ⟶ 107:
increase()
{
theTape_tape.append(
Expression.MessageCall(
new Message("setAt[3]"),
Line 118 ⟶ 119:
Expression.MessageCall(
new Message("toInt[2]"),
Expression.Constant(convertorintConvertExt),
Expression.MessageCall(
new Message("at[2]"),
Line 133 ⟶ 134:
decrease()
{
theTape_tape.append(
Expression.MessageCall(
new Message("setAt[3]"),
Line 145 ⟶ 146:
Expression.MessageCall(
new Message("toInt[2]"),
Expression.Constant(convertorintConvertExt),
Expression.MessageCall(
new Message("at[2]"),
Line 160 ⟶ 161:
compiled()
{
var program := DynamicSingleton.newload(
Expression.Method(
"eval",
CodeblockExpression.new ScopeVariable("tape"_tape.Value),
ExpressionScopeIdentifier.CodeBlockVariable(theTape.Value"tape"))).compiled();
).compile();
^(tape){ program.eval(tape) }
Line 178 ⟶ 180:
var bfProgram := TapeAssembler.load(bfAssemblyProgram).compiled();
 
var bfTape := Array.allocate(1024).populate::(n => $0);
 
console.writeLine(bf_program);
 
bfProgram(bfTape)
}</langsyntaxhighlight>
The grammar:
<langsyntaxhighlight lang="elena">[[
#grammar build
#grammar cf
 
#define start ::= <= system'dynamic'ClosureTape ( => command commands <= ) =>;
<=
system'dynamic'DynamicTape (
system'dynamic'AllocFunction ( "1" )
system'dynamic'LocalFunction ( "2" ) => command commands
<= ) =>;
 
#define start ::= $eof;
 
Line 194 ⟶ 204:
#define commands ::= $eof;
 
#define command ::= <= system'dynamic'MessageClosureMessageFunction ( "output[1]" ) => ".";
#define command ::= <= system'dynamic'MessageClosureMessageFunction ( "input[1]" ) => ",";
#define command ::= <= system'dynamic'MessageClosureMessageFunction ( "previous[1]" ) => "<";
#define command ::= <= system'dynamic'MessageClosureMessageFunction ( "next[1]" ) => ">";
#define command ::= <= system'dynamic'MessageClosureMessageFunction ( "increase[1]" ) => "+";
#define command ::= <= system'dynamic'MessageClosureMessageFunction ( "decrease[1]" ) => "-";
#define command ::= <= system'dynamic'MessageClosureMessageFunction ( "open[1]" ) => "[";
#define command ::= <= system'dynamic'MessageClosureMessageFunction ( "close[1]" ) => "]";
 
#define comment ::= " " comments;
Line 212 ⟶ 222:
 
#mode symbolic;
]]</langsyntaxhighlight>
{{out}}
<pre>
Anonymous user