refactor: add stub for instantiating the template
This commit is contained in:
parent
5cea71cb25
commit
171b6941f4
@ -7,6 +7,14 @@ const anySpec = {} // fixed dummy specification of 'any' type
|
|||||||
|
|
||||||
const theTemplateParam = 'T' // First pass: only allow this one exact parameter
|
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 {
|
export default class PocomathInstance {
|
||||||
/* Disallowed names for ops; beware, this is slightly non-DRY
|
/* Disallowed names for ops; beware, this is slightly non-DRY
|
||||||
* in that if a new top-level PocomathInstance method is added, its name
|
* in that if a new top-level PocomathInstance method is added, its name
|
||||||
@ -413,15 +421,22 @@ export default class PocomathInstance {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (explicit) {
|
if (explicit) {
|
||||||
this._addTFimplementation(
|
this._addTFimplementation(tf_imps, rawSignature, behavior)
|
||||||
tf_imps, rawSignature, behavior.uses, behavior.does)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
/* It's a template, have to instantiate */
|
/* It's a template, have to instantiate */
|
||||||
/* But just for initial testing, punt and use T as synonym for 'any' */
|
/* First, add the known instantiations */
|
||||||
const signature = rawSignature.replaceAll(theTemplateParam, 'any')
|
if (!('instantiations' in behavior)) {
|
||||||
this._addTFimplementation(
|
behavior.instantiations = new Set()
|
||||||
tf_imps, signature, behavior.uses, behavior.does)
|
}
|
||||||
|
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)
|
const tf = this._typed(name, tf_imps)
|
||||||
Object.defineProperty(this, name, {configurable: true, value: tf})
|
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
|
* to typed-function implementations and inserts the result into plain object
|
||||||
* imps
|
* imps
|
||||||
*/
|
*/
|
||||||
_addTFimplementation(imps, signature, uses, does) {
|
_addTFimplementation(imps, signature, behavior, instType) {
|
||||||
|
const {uses, does} = behavior
|
||||||
if (uses.length === 0) {
|
if (uses.length === 0) {
|
||||||
imps[signature] = does()
|
imps[signature] = does()
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user