UNPKG

bds.js

Version:

A simple interpreter written to simulate and run BDScript Language in JavaScript

60 lines (59 loc) 1.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Context = void 0; const Evaluator_1 = require("./Evaluator"); class Context { constructor(fileName, bag, env, runtime) { this.fileName = fileName; this.bag = bag; this.env = env; this.runtime = runtime; this._target = null; this.options = runtime.options; } async callIdentifier(node) { const fn = this.env.get(node.value); let lastTarget = this._target; if (typeof fn === "function") { this._target = node; return fn(this); } this._target = lastTarget; return fn; } /** * Returns a true if provided arguments exists * @param amount - Amount of argument required * @param throwError - Throw error automatically * @returns */ argsCheck(amount = 1, throwError = true) { if (this._target.child.length < amount) if (throwError) throw new Error(`Expected ${amount} arguments but got ${this._target.child.length}`); else return false; return true; } /** * Return arguments from `start` up to `end` * @param start - The start of index * @param end - Amount of argument returned * @returns */ getArgs(start = -1, end = 1) { if (start < 0) { return this._target.child.copyWithin(start, start); } return this._target.child.slice(start, end + 1); } /** * Evaluate and run provided arguments * @param args - Arguments to run * @returns */ evaluateArgs(args) { return Promise.all(args.map((v) => Evaluator_1.Evaluator.singleton.visitArgument(v, this))); } } exports.Context = Context;