refactor: separate the generation of a typed-function implementation
This commit is contained in:
parent
5c3716ff99
commit
0fbcbf661e
@ -319,7 +319,8 @@ export default class PocomathInstance {
|
||||
`Conflicting definitions of ${signature} for ${name}`)
|
||||
}
|
||||
} else {
|
||||
opImps[signature] = behavior
|
||||
// Must avoid aliasing into another instance:
|
||||
opImps[signature] = {uses: behavior.uses, does: behavior.does}
|
||||
for (const dep of behavior.uses) {
|
||||
const depname = dep.split('(', 1)[0]
|
||||
if (depname === 'self' || this._templateParam(depname)) {
|
||||
@ -402,12 +403,26 @@ export default class PocomathInstance {
|
||||
}
|
||||
Object.defineProperty(this, name, {configurable: true, value: 'limbo'})
|
||||
const tf_imps = {}
|
||||
for (const [rawSignature, {uses, does}] of usableEntries) {
|
||||
for (const [rawSignature, behavior] of usableEntries) {
|
||||
/* For now, replace theTemplateParam with 'any' */
|
||||
const signature = rawSignature.replaceAll(theTemplateParam, 'any')
|
||||
this._addTFimplementation(
|
||||
tf_imps, signature, behavior.uses, behavior.does)
|
||||
}
|
||||
const tf = this._typed(name, tf_imps)
|
||||
Object.defineProperty(this, name, {configurable: true, value: tf})
|
||||
return tf
|
||||
}
|
||||
|
||||
/* Adapts Pocomath-style behavior specification (uses, does) for signature
|
||||
* to typed-function implementations and inserts the result into plain object
|
||||
* imps
|
||||
*/
|
||||
_addTFimplementation(imps, signature, uses, does) {
|
||||
if (uses.length === 0) {
|
||||
tf_imps[signature] = does()
|
||||
} else {
|
||||
imps[signature] = does()
|
||||
return
|
||||
}
|
||||
const refs = {}
|
||||
let full_self_referential = false
|
||||
let part_self_references = []
|
||||
@ -455,12 +470,14 @@ export default class PocomathInstance {
|
||||
}
|
||||
}
|
||||
if (full_self_referential) {
|
||||
tf_imps[signature] = this._typed.referToSelf(self => {
|
||||
imps[signature] = this._typed.referToSelf(self => {
|
||||
refs.self = self
|
||||
return does(refs)
|
||||
})
|
||||
} else if (part_self_references.length) {
|
||||
tf_imps[signature] = this._typed.referTo(
|
||||
return
|
||||
}
|
||||
if (part_self_references.length) {
|
||||
imps[signature] = this._typed.referTo(
|
||||
...part_self_references, (...impls) => {
|
||||
for (let i = 0; i < part_self_references.length; ++i) {
|
||||
refs[`self(${part_self_references[i]})`] = impls[i]
|
||||
@ -468,14 +485,8 @@ export default class PocomathInstance {
|
||||
return does(refs)
|
||||
}
|
||||
)
|
||||
} else {
|
||||
tf_imps[signature] = does(refs)
|
||||
return
|
||||
}
|
||||
imps[signature] = does(refs)
|
||||
}
|
||||
}
|
||||
}
|
||||
const tf = this._typed(name, tf_imps)
|
||||
Object.defineProperty(this, name, {configurable: true, value: tf})
|
||||
return tf
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user