Skip to content

Commit

Permalink
Refactors del maravilloso @fdodino! Gracias :)
Browse files Browse the repository at this point in the history
Co-authored-by: Fernando Dodino <[email protected]>
  • Loading branch information
PalumboN and fdodino authored Nov 26, 2023
1 parent faeb9d4 commit 5f8d212
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
12 changes: 7 additions & 5 deletions src/typeSystem/constraintBasedTypeSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,15 @@ export const bindReceivedMessages = (tVar: TypeVariable): boolean => {
return reportProblem(tVar, new TypeSystemProblem('methodNotFound', [send.signature, type.name]))

const methodInstance = typeVariableFor(method).instanceFor(tVar, typeVariableFor(send))
if (!methodInstance.atParam(RETURN).hasSupertype(typeVariableFor(send))) {
methodInstance.atParam(RETURN).addSupertype(typeVariableFor(send))
logger.log(`NEW SUPERTYPE |${typeVariableFor(send)}| for |${methodInstance.atParam(RETURN)}|`)
const returnParam = methodInstance.atParam(RETURN)
if (!returnParam.hasSupertype(typeVariableFor(send))) {
returnParam.addSupertype(typeVariableFor(send))
logger.log(`NEW SUPERTYPE |${typeVariableFor(send)}| for |${returnParam}|`)
method.parameters.forEach((_param, i) => {
const argTVAR = typeVariableFor(send.args[i])
methodInstance.atParam(`${PARAM}${i}`).addSubtype(argTVAR)
logger.log(`NEW SUBTYPE |${argTVAR}| for |${methodInstance.atParam(`${PARAM}${i}`)}|`)
const currentParam = methodInstance.atParam(`${PARAM}${i}`)
currentParam.addSubtype(argTVAR)
logger.log(`NEW SUBTYPE |${argTVAR}| for |${currentParam}|`)
})

logger.log(`BIND MESSAGE |${send}| WITH METHOD |${method}|`)
Expand Down
2 changes: 1 addition & 1 deletion src/typeSystem/typeVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function newTVarFor(node: Node) {
}
if (node.is(Singleton) && node.isClosure()) {
const methodApply = node.methods.find(_ => _.name === '<apply>')!
const parameters = methodApply.parameters.map(p => typeVariableFor(p))
const parameters = methodApply.parameters.map(typeVariableFor)
// annotatedVar = newSynteticTVar() // But for methods, annotations reference to return tVar
const returnType = typeVariableFor(methodApply).atParam(RETURN)
newTVar.setType(new WollokClosureType(returnType, parameters, node), false)
Expand Down
14 changes: 6 additions & 8 deletions src/typeSystem/wollokTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export class WollokAtomicType {
}

lookupMethod(_name: Name, _arity: number, _options?: { lookupStartFQN?: Name, allowAbstractMethods?: boolean }): Method {
throw new Error('Atomic types has no methods')
throw new Error('Atomic types have no methods')
}

atParam(_name: string): TypeVariable { throw new Error('Atomic types has no params') }
atParam(_name: string): TypeVariable { throw new Error('Atomic types have no params') }
instanceFor(_instance: TypeVariable, _send?: TypeVariable, _name?: string): TypeVariable | null { return null }

contains(type: WollokType): boolean {
Expand Down Expand Up @@ -195,16 +195,16 @@ export class WollokParameterType {
}

lookupMethod(_name: Name, _arity: number, _options?: { lookupStartFQN?: Name, allowAbstractMethods?: boolean }): Method {
throw new Error('Parameters types has no methods')
throw new Error('Parameters types have no methods')
}

atParam(_name: string): TypeVariable {
throw new Error('Parameters types has no params')
throw new Error('Parameters types have no params')
}

contains(type: WollokType): boolean {
if (this === type) return true
throw new Error('Parameters types does not contains other types')
throw new Error('Parameters types don't contain other types')
}

asList(): WollokType[] { return [this] }
Expand Down Expand Up @@ -264,8 +264,6 @@ export class TypeRegistry {
constructor(private tVars: Map<Node, TypeVariable>) { }

getType(node: Node): WollokType {
const tVar = this.tVars.get(node)
if (!tVar) throw new Error(`No type variable for node ${node}`)
return tVar.type()
this.tVars.get(node)?.type() ?? throw new Error(`No type variable for node ${node}`)
}
}

1 comment on commit 5f8d212

@fdodino
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

Please sign in to comment.