UNPKG

molstar

Version:

A comprehensive macromolecular library.

171 lines 6.4 kB
/** * Copyright (c) 2018 Mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal <david.sehnal@gmail.com> */ import { Expression } from '../../language/expression'; import { QueryContext, Structure } from '../../../mol-model/structure'; var QueryRuntimeTable = /** @class */ (function () { function QueryRuntimeTable() { this.map = new Map(); } QueryRuntimeTable.prototype.removeSymbol = function (runtime) { this.map.delete(runtime.symbol.id); }; QueryRuntimeTable.prototype.addSymbol = function (runtime) { if (this.map.has(runtime.symbol.id)) { console.warn("Symbol '" + runtime.symbol.id + "' already added. Call removeSymbol/removeCustomProps re-adding the symbol."); } this.map.set(runtime.symbol.id, runtime); }; QueryRuntimeTable.prototype.addCustomProp = function (desc) { if (!desc.symbols) return; for (var _a = 0, _b = Object.keys(desc.symbols); _a < _b.length; _a++) { var k = _b[_a]; this.addSymbol(desc.symbols[k]); } }; QueryRuntimeTable.prototype.removeCustomProp = function (desc) { if (!desc.symbols) return; for (var _a = 0, _b = Object.keys(desc.symbols); _a < _b.length; _a++) { var k = _b[_a]; this.removeSymbol(desc.symbols[k]); } }; QueryRuntimeTable.prototype.getRuntime = function (id) { return this.map.get(id); }; return QueryRuntimeTable; }()); export { QueryRuntimeTable }; export var DefaultQueryRuntimeTable = new QueryRuntimeTable(); var QueryCompilerCtx = /** @class */ (function () { function QueryCompilerCtx(table) { this.table = table; this.constQueryContext = new QueryContext(Structure.Empty); } return QueryCompilerCtx; }()); export { QueryCompilerCtx }; export var QueryCompiledSymbol; (function (QueryCompiledSymbol) { function Const(value) { return { kind: 'const', value: value }; } QueryCompiledSymbol.Const = Const; function Dynamic(runtime) { return { kind: 'dynamic', runtime: runtime }; } QueryCompiledSymbol.Dynamic = Dynamic; })(QueryCompiledSymbol || (QueryCompiledSymbol = {})); export var CompiledQueryFn; (function (CompiledQueryFn) { function Const(value) { return { isConst: true, fn: function CompiledQueryFn_Const(ctx) { return value; } }; } CompiledQueryFn.Const = Const; function Dynamic(fn) { return { isConst: false, fn: fn }; } CompiledQueryFn.Dynamic = Dynamic; })(CompiledQueryFn || (CompiledQueryFn = {})); export var QueryRuntimeArguments; (function (QueryRuntimeArguments) { function forEachEval(xs, queryCtx, f, ctx) { if (typeof xs.length === 'number') { for (var i = 0, _i = xs.length; i < _i; i++) f(xs[i](queryCtx), i, ctx); } else { var i = 0; for (var _a = 0, _b = Object.keys(xs); _a < _b.length; _a++) { var k = _b[_a]; f(xs[k](queryCtx), i++, ctx); } } return ctx; } QueryRuntimeArguments.forEachEval = forEachEval; })(QueryRuntimeArguments || (QueryRuntimeArguments = {})); export var QuerySymbolRuntime; (function (QuerySymbolRuntime) { function Const(symbol, fn) { return new SymbolRuntimeImpl(symbol, fn, true); } QuerySymbolRuntime.Const = Const; function Dynamic(symbol, fn) { return new SymbolRuntimeImpl(symbol, fn, false); } QuerySymbolRuntime.Dynamic = Dynamic; })(QuerySymbolRuntime || (QuerySymbolRuntime = {})); var SymbolRuntimeImpl = /** @class */ (function () { function SymbolRuntimeImpl(symbol, fn, isConst) { this.symbol = symbol; this.fn = fn; this.isConst = isConst; } SymbolRuntimeImpl.prototype.compile = function (ctx, inputArgs) { var args, constArgs = false; if (!inputArgs) { args = void 0; constArgs = true; } else if (Expression.isArgumentsArray(inputArgs)) { args = []; constArgs = true; for (var _a = 0, inputArgs_1 = inputArgs; _a < inputArgs_1.length; _a++) { var arg = inputArgs_1[_a]; var compiled = _compile(ctx, arg); constArgs = constArgs && compiled.isConst; args.push(compiled.fn); } } else { args = Object.create(null); constArgs = true; for (var _b = 0, _c = Object.keys(inputArgs); _b < _c.length; _b++) { var key = _c[_b]; var compiled = _compile(ctx, inputArgs[key]); constArgs = constArgs && compiled.isConst; args[key] = compiled.fn; } } if (this.isConst) { if (this.isConst && constArgs) { return CompiledQueryFn.Const(this.fn(ctx.constQueryContext, args)); } return CompiledQueryFn.Dynamic(createDynamicFn(this.fn, args)); } return CompiledQueryFn.Dynamic(createDynamicFn(this.fn, args)); }; return SymbolRuntimeImpl; }()); function createDynamicFn(fn, args) { return function DynamicFn(ctx) { return fn(ctx, args); }; } function _compile(ctx, expression) { if (Expression.isLiteral(expression)) { return CompiledQueryFn.Const(expression); } if (Expression.isSymbol(expression)) { var runtime = ctx.table.getRuntime(expression.name); if (!runtime) return CompiledQueryFn.Const(expression.name); return runtime.compile(ctx); } if (!Expression.isSymbol(expression.head)) { throw new Error('Can only apply symbols.'); } var compiler = ctx.table.getRuntime(expression.head.name); if (!compiler) { throw new Error("Symbol '" + expression.head.name + "' is not implemented."); } return compiler.compile(ctx, expression.args); } export function compile(expression) { var ctx = new QueryCompilerCtx(DefaultQueryRuntimeTable); return _compile(ctx, expression).fn; } //# sourceMappingURL=base.js.map