feat: Template operations #41

Merged
glen merged 10 commits from template_operations into main 2022-08-01 10:09:32 +00:00
Showing only changes of commit 0fbcbf661e - Show all commits

View File

@ -319,7 +319,8 @@ export default class PocomathInstance {
`Conflicting definitions of ${signature} for ${name}`) `Conflicting definitions of ${signature} for ${name}`)
} }
} else { } else {
opImps[signature] = behavior // Must avoid aliasing into another instance:
opImps[signature] = {uses: behavior.uses, does: behavior.does}
for (const dep of behavior.uses) { for (const dep of behavior.uses) {
const depname = dep.split('(', 1)[0] const depname = dep.split('(', 1)[0]
if (depname === 'self' || this._templateParam(depname)) { if (depname === 'self' || this._templateParam(depname)) {
@ -402,12 +403,26 @@ export default class PocomathInstance {
} }
Object.defineProperty(this, name, {configurable: true, value: 'limbo'}) Object.defineProperty(this, name, {configurable: true, value: 'limbo'})
const tf_imps = {} const tf_imps = {}
for (const [rawSignature, {uses, does}] of usableEntries) { for (const [rawSignature, behavior] of usableEntries) {
/* For now, replace theTemplateParam with 'any' */ /* For now, replace theTemplateParam with 'any' */
const signature = rawSignature.replaceAll(theTemplateParam, '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) { if (uses.length === 0) {
tf_imps[signature] = does() imps[signature] = does()
} else { return
}
const refs = {} const refs = {}
let full_self_referential = false let full_self_referential = false
let part_self_references = [] let part_self_references = []
@ -455,12 +470,14 @@ export default class PocomathInstance {
} }
} }
if (full_self_referential) { if (full_self_referential) {
tf_imps[signature] = this._typed.referToSelf(self => { imps[signature] = this._typed.referToSelf(self => {
refs.self = self refs.self = self
return does(refs) return does(refs)
}) })
} else if (part_self_references.length) { return
tf_imps[signature] = this._typed.referTo( }
if (part_self_references.length) {
imps[signature] = this._typed.referTo(
...part_self_references, (...impls) => { ...part_self_references, (...impls) => {
for (let i = 0; i < part_self_references.length; ++i) { for (let i = 0; i < part_self_references.length; ++i) {
refs[`self(${part_self_references[i]})`] = impls[i] refs[`self(${part_self_references[i]})`] = impls[i]
@ -468,14 +485,8 @@ export default class PocomathInstance {
return does(refs) return does(refs)
} }
) )
} else { return
tf_imps[signature] = does(refs) }
imps[signature] = does(refs)
} }
} }
}
const tf = this._typed(name, tf_imps)
Object.defineProperty(this, name, {configurable: true, value: tf})
return tf
}
}