私はBrain(Brainfuckのような言語)のための通訳を開発しており、breakステートメントのデザインに関するいくつかの懸念を持っています。インタープリタでbreakステートメントを実装する最善の方法は何ですか?
var Stmt = (function() {
var Stmt = function() {};
Stmt.prototype = {
update_expression: function(update) { return false; },
value: function() { return this; },
exec: function(delegate) { },
zerofy: function (tapeObj) {
if (tapeObj.data[tapeObj.d_ptr] === undefined) {
tapeObj.data[tapeObj.d_ptr] = 0;
}
}
};
return Stmt;
})();
var BreakStmt = (function() {
var BreakStmt = function(){ };
BreakStmt.prototype = Object.create(Stmt.prototype);
return BreakStmt;
})();
var LoopStmt = (function() {
var LoopStmt = function(stmts, type) {
this.type = 'undefined';
this.stmts = null;
if (tokens[type] === 'TT_BEGIN_WHILE'
|| tokens[type] === 'TT_BEGIN_FOR') {
this.type = type;
this.stmts = stmts;
}
};
LoopStmt.prototype = Object.create(Stmt.prototype);
return LoopStmt;
})();
var IfStmt = (function() {
var IfStmt = function(stmts_then) {
this.stmts_then = stmts_then;
this.stmts_else = null;
};
IfStmt.prototype = Object.create(Stmt.prototype);
IfStmt.prototype.set_else = function(stmts_else) {
this.stmts_else = stmts_else;
};
return IfStmt;
})();
パーサは再帰的に文を経由して、ASTを返します。
はJSで以下のコードを考えてみましょう。ここで、次の文でASTを想像:
LoopStmt
IfStmt
IfStmt
BreakStmt
...
要素exec
関数が再帰的にもなります。この実装には多くのアイデアがあるにも関わらず、多くの研究の後、最も近いLoopStmtが見つかるまで、この再帰を再帰的に止める最良の方法がわかりません。
これにはどのようなデザインが最適ですか? お手数をお掛けします。ありがとうございます!