dope2/main.js

94 lines
2.7 KiB
JavaScript

import {Function, ModuleMetaCircular, getIn, getOut} from "./metacircular.js";
import {ModuleTyped} from "./typed.js";
import {ModuleBool} from "./primitives/bool.js";
import { Double_to_Double_to_Double, ModuleDouble, mulDouble } from "./primitives/double.js";
import {Int_to_Int_to_Int, ModuleInt, mulInt} from "./primitives/int.js";
import {Int} from "./primitives/symbols.js";
import { makeSquare } from "./lib/square.js";
import {makeIdFn} from "./lib/id.js";
import {ModuleValues} from "./lib/values.js";
import {DefaultMap} from "./util.js";
import { ModuleModule } from "./structures/module.js";
import { ModuleList } from "./structures/list.js";
class Context {
constructor(mod) {
// input type -> Function-signature
this.functionsFrom = new DefaultMap(() => []);
this.functionsTo = new DefaultMap(() => []);
this.types = new DefaultMap(() => []);
this.instances = new DefaultMap(() => []);
for (const {i, t} of mod) {
this.addInstance({i, t});
}
const callFunctionOnEveryValue = (fn, fnType, indent=1) => {
const inType = getIn(fnType);
const outType = getOut(fnType);
console.log();
if (fn.name !== "") {
console.log("--".repeat(indent), fn, ':', fnType);
}
for (const i of this.instances.getdefault(inType)) {
try {
const result = fn(i);
console.log("--".repeat(indent+1), i, '->', result);
if (this.types.getdefault(outType).includes(Function)) {
if (indent < 5) {
callFunctionOnEveryValue(result, outType, indent+2);
}
}
} catch (e) {
console.log("--".repeat(indent+1), i, `-> (exception: ${e})`);
}
}
}
for (const fntypes of this.functionsFrom.m.values()) {
for (const fntype of fntypes) {
for (const fn of this.instances.getdefault(fntype)) {
callFunctionOnEveryValue(fn, fntype);
}
}
}
}
addInstance({i, t}) {
const arr = this.types.getdefault(i, true);
arr.push(t);
const arrI = this.instances.getdefault(t, true);
arrI.push(i);
if (t === Function) {
const arr = this.functionsFrom.getdefault(getIn(i), true);
arr.push(i);
const arrTo = this.functionsTo.getdefault(getOut(i), true);
arrTo.push(i);
}
else {
}
}
}
const ctx = new Context([
...ModuleMetaCircular,
...ModuleTyped,
// ...ModuleConformable,
// ...ModuleConformanceCheckConforms,
// ...ModuleNum,
// ...ModuleEq,
...ModuleBool,
...ModuleInt,
...ModuleDouble,
// ...ModuleSquare,
// ...ModuleList,
...makeIdFn(Int),
...makeSquare({i: mulInt, t: Int_to_Int_to_Int}),
...makeSquare({i: mulDouble, t: Double_to_Double_to_Double}),
...ModuleList,
...ModuleValues,
...ModuleModule,
]);