calcium-lang
Version:
Calcium language interpreter
64 lines • 1.59 kB
JavaScript
/**
* the kind of a `Block`
*/
export var Kind;
(function (Kind) {
Kind["Call"] = "Call";
Kind["ClassDef"] = "ClassDef";
Kind["Except"] = "Except";
Kind["For"] = "For";
Kind["IfElifElse"] = "IfElifElse";
Kind["Ifs"] = "Ifs";
Kind["ForEach"] = "ForEach";
Kind["ForRange"] = "ForRange";
Kind["Try"] = "Try";
Kind["While"] = "While";
})(Kind || (Kind = {}));
export var Result;
(function (Result) {
/**
* an exception has occurred
*/
Result["Invalid"] = "Invalid";
/**
* moved over two or more points
*/
Result["Jumpped"] = "Jumpped";
/**
* shifted one point only
*/
Result["Exited"] = "Exited";
})(Result || (Result = {}));
/**
* a syntactic scope
*/
export class Block {
/**
*
* @param kind
* @param address
* @param shouldEnter determine whether this block should be executed
* @param willExit executed before this block ends
*/
constructor(kind, address, shouldEnter, willExit) {
this.kind = kind;
this.shouldEnter = shouldEnter;
this.willExit = willExit;
this.address = address.clone();
}
willEnter(env) {
env.address = this.address.clone();
if (this.shouldEnter(env)) {
env.address.shift(1);
env.blocks.push(this);
}
}
exit(env) {
env.blocks.pop();
if (env.exceptionThrown) {
return Result.Invalid;
}
return this.willExit(env); // each command can have their own result
}
}
//# sourceMappingURL=block.js.map