UNPKG

toylang

Version:

A toy programming language built with TypeScript for learning purposes

89 lines (88 loc) 3.54 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ToyLangFunction = exports.CallableFunction = void 0; var Environment_1 = require("./Environment"); var CallableFunction = /** @class */ (function () { function CallableFunction() { } CallableFunction.prototype.call = function (interpreter, args) { console.warn("unimplemented function called"); }; CallableFunction.prototype.arity = function () { return 0; }; CallableFunction.prototype.toString = function () { return "<unimplemented fn>"; }; CallableFunction.new = function (obj) { return new (/** @class */ (function () { function CallableFunction() { } CallableFunction.prototype.call = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } return obj.call.apply(obj, args); }; CallableFunction.prototype.arity = function () { return obj.arity; }; CallableFunction.prototype.toString = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } obj.toString.apply(obj, args); }; return CallableFunction; }()))(); }; return CallableFunction; }()); exports.CallableFunction = CallableFunction; var ToyLangFunction = /** @class */ (function (_super) { __extends(ToyLangFunction, _super); function ToyLangFunction(declaration, closure) { var _this = _super.call(this) || this; _this.declaration = declaration; _this.closure = closure; return _this; } ToyLangFunction.prototype.call = function (interpreter, args) { var environment = new Environment_1.Environment(this.closure); for (var i = 0; i < this.declaration.params.length; i++) { environment.add(this.declaration.params[i].name, args[i]); } try { interpreter.executeBlock(this.declaration.body.body, environment); } catch (returnValue /* instanceof `Return` */) { return returnValue.value; } return null; }; ToyLangFunction.prototype.arity = function () { return this.declaration.params.length; }; ToyLangFunction.prototype.toString = function () { return "<fn " + this.declaration.name.name + ">"; }; return ToyLangFunction; }(CallableFunction)); exports.ToyLangFunction = ToyLangFunction;