refactor: add stub for instantiating the template

This commit is contained in:
Glen Whitney 2022-07-31 10:01:20 -07:00
parent 5cea71cb25
commit 171b6941f4
1 changed files with 23 additions and 7 deletions

View File

@ -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