UNPKG

@mojir/lits

Version:

Lits is a pure functional programming language implemented in TypeScript

3 lines (2 loc) 1.82 kB
"use strict";var e=require("node:fs"),t=require("node:path");const n=new Set(["assert","grid","random","vector","linear-algebra","matrix","number-theory","math","functional","string","collection","sequence","bitwise"]),r=/import\(\s*"([^"]+)"\s*\)|import\(\s*'([^']+)'\s*\)/g;exports.bundle=function(o){const s=t.resolve(o),i=t.dirname(s),c=new Map,a=new Map,u=new Map;!function n(o,s){if(s.includes(o)){const e=[...s.slice(s.indexOf(o)),o];throw new Error(`Circular dependency detected: ${e.join(" → ")}`)}if(c.has(o))return;if(!e.existsSync(o))throw new Error(`File not found: ${o}`);const i=e.readFileSync(o,"utf-8");c.set(o,i);const a=new Set;u.set(o,a);const f=t.dirname(o);for(const e of i.matchAll(r)){const r=e[1]??e[2],i=t.resolve(f,r);a.add(i),n(i,[...s,o])}}(s,[]),function(e){const t=new Set;for(const r of c.keys()){if(r===s)continue;let o=l(r,e);for(;n.has(o)||t.has(o);)o=`_${o}`;t.add(o),a.set(r,o)}}(i);const f=function(e){const t=[],n=new Set,r=new Set;return function o(s){if(n.has(s))return;if(r.has(s))throw new Error(`Circular dependency detected during topological sort: ${s}`);r.add(s);const i=u.get(s);if(i)for(const e of i)o(e);r.delete(s),n.add(s),s!==e&&t.push(s)}(e),t}(s).map(e=>{const t=p(c.get(e),e);return[a.get(e),t]});return{program:p(c.get(s),s),fileModules:f};function l(e,n){const r=t.relative(n,e);if(!r.startsWith(".."))return d(r);return d(e.split(t.sep).slice(-2).join("/"))}function d(e){return e.endsWith(".lits")?e.slice(0,-5):e}function p(e,n){const o=t.dirname(n);return e.replace(r,(e,n,r)=>{const s=n??r,i=t.resolve(o,s),c=a.get(i);if(!c)throw new Error(`No canonical name for: ${i}`);return`import(${c})`})}},exports.isLitsBundle=function(e){return"object"==typeof e&&null!==e&&"string"==typeof e.program&&Array.isArray(e.fileModules)}; //# sourceMappingURL=bundler.js.map