From 171b6941f42f680062e0531a3503f8e155817ed8 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Sun, 31 Jul 2022 10:01:20 -0700 Subject: [PATCH] refactor: add stub for instantiating the template --- src/core/PocomathInstance.mjs | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/core/PocomathInstance.mjs b/src/core/PocomathInstance.mjs index 62c6757..4fc7b5b 100644 --- a/src/core/PocomathInstance.mjs +++ b/src/core/PocomathInstance.mjs @@ -7,6 +7,14 @@ const anySpec = {} // fixed dummy specification of 'any' type const theTemplateParam = 'T' // First pass: only allow this one exact parameter +/* Returns a new signature just like sig but with the parameter replaced by + * the type + */ +function substituteInSig(sig, parameter, type) { + const pattern = new RegExp("\\b" + parameter + "\\b", 'g') + return sig.replaceAll(pattern, type) +} + export default class PocomathInstance { /* Disallowed names for ops; beware, this is slightly non-DRY * in that if a new top-level PocomathInstance method is added, its name @@ -413,15 +421,22 @@ export default class PocomathInstance { } } if (explicit) { - this._addTFimplementation( - tf_imps, rawSignature, behavior.uses, behavior.does) + this._addTFimplementation(tf_imps, rawSignature, behavior) continue } /* It's a template, have to instantiate */ - /* But just for initial testing, punt and use T as synonym for 'any' */ - const signature = rawSignature.replaceAll(theTemplateParam, 'any') - this._addTFimplementation( - tf_imps, signature, behavior.uses, behavior.does) + /* First, add the known instantiations */ + if (!('instantiations' in behavior)) { + behavior.instantiations = new Set() + } + for (const instType of behavior.instantiations) { + const signature = + substituteInSig(rawSignature, theTemplateParam, instType) + this._addTFimplementation(tf_imps, signature, behavior, instType) + } + /* Now add the catchall signature; for now, punting on the behavior */ + const signature = substituteInSig(rawSignature, theTemplateParam, 'any') + this._addTFimplementation(tf_imps, signature, behavior) } const tf = this._typed(name, tf_imps) Object.defineProperty(this, name, {configurable: true, value: tf}) @@ -432,7 +447,8 @@ export default class PocomathInstance { * to typed-function implementations and inserts the result into plain object * imps */ - _addTFimplementation(imps, signature, uses, does) { + _addTFimplementation(imps, signature, behavior, instType) { + const {uses, does} = behavior if (uses.length === 0) { imps[signature] = does() return