calcium-js
Version:
Calcium runtime on JavaScript
92 lines • 2.76 kB
JavaScript
import { Behavior } from './behavior';
import { Environment } from './environment';
import { Index } from './statement';
import { Status } from './status';
export class Runtime {
constructor(params) {
this.breakpoints = new Set();
this.events = params.events;
this.parser = params.parser;
let stmts;
if (typeof params.code === 'string') {
stmts = JSON.parse(params.code);
}
else {
stmts = params.code;
}
this.env = new Environment({ code: stmts });
}
backward() {
while (this.currentIndex > 0) {
this.env.address.step(-1);
if (this.currentIndent === this.currentLine[Index.Indent]) {
return;
}
}
}
forward() {
while (this.currentIndex < this.env.code.length) {
this.env.address.step(1);
if (this.currentIndex >= this.env.code.length) {
return;
}
if (this.currentIndent === this.currentLine[Index.Indent]) {
return;
}
}
}
run() {
while (true) {
const result = this.step();
if (result === Status.Running) {
continue;
}
else {
return result;
}
}
}
step() {
var _a;
if (this.currentIndex > this.lastIndex || this.currentIndent === 0) {
return Status.Terminated;
}
const cmd = this.parser.readStmt(this.currentLine);
const result = cmd.execute(this.env);
this.env.previousBehavior = result;
if (result === Behavior.Loop) {
this.backward();
}
else {
this.forward();
}
if (this.currentIndex > this.lastIndex || this.currentIndent === 0) {
return Status.Terminated;
}
// currentLine should be the next statement.
let nextStmt = this.currentLine;
while ((_a = this.events) === null || _a === void 0 ? void 0 : _a.skip(nextStmt, this)) {
nextStmt = this.currentLine;
}
if (this.currentIndex > this.lastIndex || this.currentIndent === 0) {
return Status.Terminated;
}
if (this.breakpoints.has(this.currentIndex)) {
return Status.AtBreakpoint;
}
return Status.Running;
}
get currentIndent() {
return this.env.address.indent;
}
get currentIndex() {
return this.env.address.index;
}
get currentLine() {
return this.env.code[this.env.address.index];
}
get lastIndex() {
return this.env.code.length - 1;
}
}
//# sourceMappingURL=runtime.js.map