cookeylang-ts
Version:
A dynamic, interpreted language.
78 lines (77 loc) • 3.11 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserCallable = exports.FuncCallable = void 0;
const environment_1 = require("./environment");
const token_1 = require("./token");
const errors_1 = require("./errors");
const Stmt = __importStar(require("./expr/stmt"));
const Expr = __importStar(require("./expr/expr"));
class FuncCallable {
constructor(arity = 0, call = () => { }, toString = () => "") {
this.arity = arity;
this.call = call;
this.toString = toString;
}
call(_, _1, _2) { }
;
toString() { return ""; }
;
}
exports.FuncCallable = FuncCallable;
class UserCallable extends FuncCallable {
constructor(decl, closure) {
super(0, () => { }, () => {
if (this.decl instanceof Stmt.FuncDecl)
return `<function ${this.decl.name.value}>`;
else if (this.decl instanceof Expr.Lambda)
return `<anonymous function>`;
return "";
});
this.decl = decl;
if (this.decl instanceof Stmt.FuncDecl || this.decl instanceof Expr.Lambda)
this.arity = this.decl.params.length;
this.closure = closure;
this.call = (interpreter, args, callFrom) => {
let env = new environment_1.Environment(this.closure);
if (this.decl instanceof Stmt.FuncDecl || this.decl instanceof Expr.Lambda) {
for (let i = 0; i < this.decl.params.length; i++) {
env.define(token_1.defualtToken, this.decl.params[i], args[i]);
}
try {
interpreter.initBlock(this.decl.body, env);
}
catch (ret) {
if (ret instanceof errors_1.CookeyRet) {
return ret.value;
}
else {
if (ret instanceof errors_1.CookeyError)
ret.pushStack(callFrom || this.decl.lineData);
throw ret;
}
}
}
return null;
};
}
}
exports.UserCallable = UserCallable;