Skip to content

Commit

Permalink
Linter
Browse files Browse the repository at this point in the history
  • Loading branch information
PalumboN committed Nov 19, 2023
1 parent 345af4e commit 18932d0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 52 deletions.
8 changes: 4 additions & 4 deletions src/typeSystem/constraintBasedTypeSystem.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { anyPredicate, is } from '../extensions'
import { Environment, Module, Node, Reference } from '../model'
import { newTypeVariables, TypeVariable, typeVariableFor } from './typeVariables'
import { ANY, PARAM, RETURN, TypeRegistry, TypeSystemProblem, WollokModuleType, WollokType } from './wollokTypes'
import { PARAM, RETURN, TypeRegistry, TypeSystemProblem, WollokModuleType, WollokType } from './wollokTypes'

const { assign } = Object

Expand Down Expand Up @@ -96,7 +96,7 @@ export const bindReceivedMessages = (tVar: TypeVariable): boolean => {
methodInstance.atParam(RETURN).addSupertype(typeVariableFor(send))
logger.log(`NEW SUPERTYPE |${typeVariableFor(send)}| for |${methodInstance.atParam(RETURN)}|`)
method.parameters.forEach((_param, i) => {
const argTVAR= typeVariableFor(send.args[i])
const argTVAR = typeVariableFor(send.args[i])
methodInstance.atParam(`${PARAM}${i}`).addSubtype(argTVAR)
logger.log(`NEW SUBTYPE |${argTVAR}| for |${methodInstance.atParam(`${PARAM}${i}`)}|`)
})
Expand Down Expand Up @@ -161,12 +161,12 @@ export const mergeSuperAndSubTypes = (tVar: TypeVariable): boolean => {
export const closeTypes = (tVar: TypeVariable): boolean => {
// if(tVar.syntetic) return false
let changed = false
if(tVar.allMaxTypes().length === 0 && tVar.allMinTypes().length > 0 && tVar.supertypes.length === 0) {
if (tVar.allMaxTypes().length === 0 && tVar.allMinTypes().length > 0 && tVar.supertypes.length === 0) {
tVar.typeInfo.maxTypes = tVar.allMinTypes()
logger.log(`MAX TYPES FROM MIN FOR |${tVar}|`)
changed = true
}
if(tVar.allMinTypes().length === 0 && tVar.allMaxTypes().length > 0 && tVar.subtypes.length === 0) {
if (tVar.allMinTypes().length === 0 && tVar.allMaxTypes().length > 0 && tVar.subtypes.length === 0) {
tVar.typeInfo.minTypes = tVar.allMaxTypes()
logger.log(`MIN TYPES FROM MAX FOR |${tVar}|`)
changed = true
Expand Down
47 changes: 23 additions & 24 deletions src/typeSystem/typeVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,95 +265,94 @@ export class TypeVariable {
}
instanceFor(instance: TypeVariable, send?: TypeVariable, name?: string): TypeVariable { return this.type().instanceFor(instance, send, name) || this }

hasType(type: WollokType) { return this.allPossibleTypes().some(_type => _type.contains(type)) }
hasType(type: WollokType): boolean { return this.allPossibleTypes().some(_type => _type.contains(type)) }

setType(type: WollokType, closed?: boolean) {
setType(type: WollokType, closed?: boolean): this {
this.typeInfo.setType(type, closed)
return this
}

addMinType(type: WollokType) {
addMinType(type: WollokType): void {
this.typeInfo.addMinType(type)
}

addMaxType(type: WollokType) {
addMaxType(type: WollokType): void {
this.typeInfo.addMaxType(type)
}

beSubtypeOf(tVar: TypeVariable) {
beSubtypeOf(tVar: TypeVariable): this {
this.addSupertype(tVar)
tVar.addSubtype(this)
return this
}

beSupertypeOf(tVar: TypeVariable) {
beSupertypeOf(tVar: TypeVariable): this {
this.addSubtype(tVar)
tVar.addSupertype(this)
return this
}

unify(tVar: TypeVariable) {
unify(tVar: TypeVariable): void {
// Unification means same type, so min and max types should be propagated in both directions
this.beSupertypeOf(tVar)
this.beSubtypeOf(tVar)
}

hasSubtype(tVar: TypeVariable) {
hasSubtype(tVar: TypeVariable): boolean {
return this.subtypes.includes(tVar)
}

hasSupertype(tVar: TypeVariable) {
hasSupertype(tVar: TypeVariable): boolean {
return this.supertypes.includes(tVar)
}

addSend(send: Send) {
addSend(send: Send): void {
this.messages.push(send)
}

allMinTypes() {
allMinTypes(): WollokType[] {
return this.typeInfo.minTypes
}
allMaxTypes() {
allMaxTypes(): WollokType[] {
return this.typeInfo.maxTypes
}
allPossibleTypes() {
allPossibleTypes(): WollokType[] {
return [...this.allMinTypes(), ...this.allMaxTypes()]
}

hasTypeInfered() {
hasTypeInfered(): boolean {
return this.allPossibleTypes().some(t => t.isComplete)
}



validSubtypes() {
validSubtypes(): TypeVariable[] {
return this.subtypes.filter(tVar => !tVar.hasProblems)
}
validSupertypes() {
validSupertypes(): TypeVariable[] {
return this.supertypes.filter(tVar => !tVar.hasProblems)
}

addSubtype(tVar: TypeVariable) {
addSubtype(tVar: TypeVariable): void {
this.subtypes.push(tVar)
}

addSupertype(tVar: TypeVariable) {
addSupertype(tVar: TypeVariable): void {
this.supertypes.push(tVar)
}

addProblem(problem: TypeSystemProblem) {
addProblem(problem: TypeSystemProblem): void {
assign(this.node, { problems: [...this.node.problems ?? [], problem] })
this.hasProblems = true
}

beSyntetic() {
beSyntetic(): this {
this.syntetic = true
return this
}

get closed() { return this.typeInfo.closed }
get closed(): boolean { return this.typeInfo.closed }

toString() { return `TVar(${this.syntetic ? 'SYNTEC' + this.node?.sourceInfo : this.node})` }
toString(): string { return `TVar(${this.syntetic ? 'SYNTEC' + this.node?.sourceInfo : this.node})` }
}

class TypeInfo {
Expand All @@ -371,7 +370,7 @@ class TypeInfo {
throw new Error('Halt')
}

setType(type: WollokType, closed: boolean = true) {
setType(type: WollokType, closed = true) {
this.minTypes = [type]
this.maxTypes = [type]
this.closed = closed
Expand Down
40 changes: 20 additions & 20 deletions src/typeSystem/wollokTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { List } from '../extensions'
import { BaseProblem, Level, Module, Name, Node, Singleton } from '../model'
import { BaseProblem, Level, Method, Module, Name, Node, Singleton } from '../model'
import { TypeVariable } from './typeVariables'

const { entries, fromEntries } = Object
Expand Down Expand Up @@ -28,7 +28,7 @@ export class WollokAtomicType {
this.id = id
}

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

Expand All @@ -39,9 +39,9 @@ export class WollokAtomicType {
return type instanceof WollokAtomicType && this.id === type.id
}

asList() { return [this] }
asList(): WollokType[] { return [this] }

isSubtypeOf(_type: WollokType) { return false }
isSubtypeOf(_type: WollokType): boolean { return false }

get name(): string { return this.id }
get kind(): string { return this.name }
Expand All @@ -56,27 +56,27 @@ export class WollokModuleType {
this.module = module
}

lookupMethod(name: Name, arity: number, options?: { lookupStartFQN?: Name, allowAbstractMethods?: boolean }) {
lookupMethod(name: Name, arity: number, options?: { lookupStartFQN?: Name, allowAbstractMethods?: boolean }): Method | undefined {
return this.module.lookupMethod(name, arity, options)
}

contains(type: WollokType): boolean {
return type instanceof WollokModuleType && (this.module === type.module ||
(this.module instanceof Singleton && type.module instanceof Singleton
&& this.module.isClosure() && type.module.isClosure()))
this.module instanceof Singleton && type.module instanceof Singleton
&& this.module.isClosure() && type.module.isClosure())
}

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

asList() { return [this] }
asList(): WollokType[] { return [this] }

isSubtypeOf(type: WollokType) {
isSubtypeOf(type: WollokType): boolean {
return type instanceof WollokModuleType && this.module !== type.module &&
(type.module.name === 'Object' || this.module.inherits(type.module))
}

get name(): string { return this.module?.name! }
get name(): string { return this.module.name! }
get kind(): string { return this.module?.name ?? 'null' }
get isComplete(): boolean { return true }

Expand Down Expand Up @@ -117,14 +117,14 @@ export class WollokParametricType extends WollokModuleType {
return instance.newInstance(name).setType(new WollokParametricType(this.module, resolvedParamTypes), false)
}

addMinType(minType: WollokParametricType) {
addMinType(minType: WollokParametricType): void {
this.params.forEach((paramTVar, name) =>
minType.atParam(name).allPossibleTypes().forEach(paramMinType =>
paramTVar.addMinType(paramMinType)
)
)
}
addMaxType(minType: WollokParametricType) {
addMaxType(minType: WollokParametricType): void {
this.params.forEach((paramTVar, name) =>
minType.atParam(name).allPossibleTypes().forEach(paramMaxType =>
paramTVar.addMaxType(paramMaxType)
Expand All @@ -146,7 +146,7 @@ export class WollokParametricType extends WollokModuleType {
return [...this.params.values()].every((tVar) => tVar.hasTypeInfered())
}

sameParams(type: WollokParametricType) {
sameParams(type: WollokParametricType): boolean {
return [...this.params.entries()].every(([name, tVar]) =>
type.atParam(name).type().name == ANY || tVar.type().contains(type.atParam(name).type()))
}
Expand All @@ -158,7 +158,7 @@ export class WollokMethodType extends WollokParametricType {
super(base!, {
...fromEntries(params.map((p, i) => [`${PARAM}${i}`, p])),
[RETURN]: returnVar,
...extra
...extra,
})
}

Expand Down Expand Up @@ -194,7 +194,7 @@ export class WollokParameterType {
return instance.atParam(this.name) || send?.newInstance(this.name)
}

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

Expand All @@ -207,9 +207,9 @@ export class WollokParameterType {
throw new Error('Parameters types does not contains other types')
}

asList() { return [this] }
asList(): WollokType[] { return [this] }

isSubtypeOf(_type: WollokType) {
isSubtypeOf(_type: WollokType): boolean {
throw new Error('Parameters types cannot be subtype of other types (invariant)')
}

Expand All @@ -226,18 +226,18 @@ export class WollokUnionType {
this.types = types
}

lookupMethod(_name: Name, _arity: number, _options?: { lookupStartFQN?: Name, allowAbstractMethods?: boolean }) {
lookupMethod(_name: Name, _arity: number, _options?: { lookupStartFQN?: Name, allowAbstractMethods?: boolean }): Method {
throw new Error('Halt')
}

atParam(_name: string): TypeVariable { throw new Error('Union types has no params') }
instanceFor(_instance: TypeVariable) { return null }
instanceFor(_instance: TypeVariable): TypeVariable | null { return null }

contains(type: WollokType): boolean {
return type.asList().every(t => this.types.some(_ => _.contains(t)))
}

asList() { return this.types }
asList(): WollokType[] { return this.types }

isSubtypeOf(type: WollokType): boolean { return this.types.every(t => t.isSubtypeOf(type)) }

Expand Down
6 changes: 2 additions & 4 deletions test/typeSystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ describe('Wollok Type System', () => {
parametricType =new WollokParametricType(module, {
'param1': newSynteticTVar(),
'param2': newSynteticTVar().setType(otherStubType),
'param3': newSynteticTVar()
'param3': newSynteticTVar(),
})
const param1 = newSynteticTVar().setType(stubType)
const param2 = newSynteticTVar()
Expand Down Expand Up @@ -328,8 +328,6 @@ describe('Wollok Type System', () => {
})




const env = new Environment({ members: [] })


Expand Down Expand Up @@ -364,4 +362,4 @@ const testMethod = newMethod('TEST_METHOD')
const otherTestMethod = newMethod('OTHER_TEST_METHOD')

const stubType = new TestWollokType('TEST_TYPE', testMethod)
const otherStubType = new TestWollokType('OTHER_TEST_TYPE', otherTestMethod)
const otherStubType = new TestWollokType('OTHER_TEST_TYPE', otherTestMethod)

0 comments on commit 18932d0

Please sign in to comment.