Skip to content

Commit

Permalink
Merge pull request #203 from uqbar-project/fix-ignore-wollok-path
Browse files Browse the repository at this point in the history
Fix ignore wollok code in Type System
  • Loading branch information
PalumboN authored Jan 20, 2024
2 parents 8a121af + 9272690 commit 36180df
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
6 changes: 5 additions & 1 deletion src/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { INFIX_OPERATORS, PREFIX_OPERATORS } from './constants'
import { INFIX_OPERATORS, PREFIX_OPERATORS, WOLLOK_BASE_PACKAGE } from './constants'
import { cached, getPotentiallyUninitializedLazy, lazy } from './decorators'
import { ConstructorFor, InstanceOf, is, last, List, mapObject, Mixable, MixinDefinition, MIXINS, isEmpty, notEmpty, TypeDefinition } from './extensions'
import { TypeRegistry, WollokType } from './typeSystem/wollokTypes'
Expand Down Expand Up @@ -282,6 +282,10 @@ export function Entity<S extends Mixable<Node>>(supertype: S) {
? `${parent.fullyQualifiedName}.${label}`
: label
}

get isBaseWollokCode(): boolean {
return this.fullyQualifiedName.startsWith(WOLLOK_BASE_PACKAGE)
}
}

return EntityType
Expand Down
47 changes: 24 additions & 23 deletions src/typeSystem/typeVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const tVars = new Map<Node, TypeVariable>()

export function newTypeVariables(env: Environment): Map<Node, TypeVariable> {
tVars.clear()
createTypeVariables(env)
inferTypeVariables(env)
return tVars
}

Expand All @@ -26,7 +26,7 @@ function newTVarFor(node: Node) {
const newTVar = doNewTVarFor(node)
let annotatedVar = newTVar // By default, annotations reference the same tVar
if (node.is(Method)) {
const parameters = node.parameters.map(p => createTypeVariables(p)!)
const parameters = node.parameters.map(p => inferTypeVariables(p)!)
annotatedVar = newSyntheticTVar(node) // But for methods, annotations reference to return tVar
newTVar.setType(new WollokMethodType(annotatedVar, parameters, annotatedVariableMap(node)), false)
}
Expand All @@ -50,7 +50,7 @@ function doNewTVarFor(node: Node) {
return newTVar
}

function createTypeVariables(node: Node): TypeVariable | void {
function inferTypeVariables(node: Node): TypeVariable | void {
return match(node)(
when(Environment)(inferEnvironment),

Expand Down Expand Up @@ -88,28 +88,29 @@ function createTypeVariables(node: Node): TypeVariable | void {
}

const inferEnvironment = (env: Environment) => {
env.children.forEach(createTypeVariables)
env.children.forEach(inferTypeVariables)
}

const inferPackage = (p: Package) => {
if (p.name.startsWith('wollok')) return //TODO: Fix wrong inferences
p.children.forEach(createTypeVariables)
// Wollok code should be typed by annotations, avoid inference.
if (p.isBaseWollokCode) return;

Check warning on line 96 in src/typeSystem/typeVariables.ts

View workflow job for this annotation

GitHub Actions / build

Extra semicolon
p.children.forEach(inferTypeVariables)
}

const inferProgram = (p: Program) => {
createTypeVariables(p.body)
inferTypeVariables(p.body)
}

const inferTest = (t: Test) => {
createTypeVariables(t.body)
inferTypeVariables(t.body)
}

const inferBody = (body: Body) => {
body.sentences.forEach(createTypeVariables)
body.sentences.forEach(inferTypeVariables)
}

const inferModule = (m: Module | Describe) => {
m.members.forEach(createTypeVariables)
m.members.forEach(inferTypeVariables)
const tVar = typeVariableFor(m)
if (!(m.is(Singleton) && m.isClosure())) // Avoid closures
tVar.setType(typeForModule(m)) // Set module type
Expand All @@ -119,12 +120,12 @@ const inferModule = (m: Module | Describe) => {
const inferNew = (n: New) => {
const clazz = n.instantiated.target!
const tVar = typeVariableFor(n).setType(typeForModule(clazz))
/*const args =*/ n.args.map(createTypeVariables)
/*const args =*/ n.args.map(inferTypeVariables)
return tVar
}

const inferNamedArgument = (n: NamedArgument) => {
const valueTVar = createTypeVariables(n.value)!
const valueTVar = inferTypeVariables(n.value)!
if (n.parent instanceof New) {
// Named arguments value should be subtype of field definition
const clazz = n.parent.instantiated.target!
Expand All @@ -140,7 +141,7 @@ const inferNamedArgument = (n: NamedArgument) => {

const inferMethod = (m: Method) => {
const method = typeVariableFor(m)
m.sentences.forEach(createTypeVariables)
m.sentences.forEach(inferTypeVariables)
if (m.sentences.length) {
const lastSentence = last(m.sentences)!
if (!lastSentence.is(Return) && !lastSentence.is(If)) { // Return inference already propagate type to method
Expand All @@ -153,22 +154,22 @@ const inferMethod = (m: Method) => {
}

const inferSend = (send: Send) => {
const receiver = createTypeVariables(send.receiver)!
/*const args =*/ send.args.map(createTypeVariables)
const receiver = inferTypeVariables(send.receiver)!
/*const args =*/ send.args.map(inferTypeVariables)
receiver.addSend(send)
// TODO: Save args info for max type inference
return typeVariableFor(send)
}

const inferAssignment = (a: Assignment) => {
const variable = createTypeVariables(a.variable)!
const value = createTypeVariables(a.value)!
const variable = inferTypeVariables(a.variable)!
const value = inferTypeVariables(a.value)!
variable.beSupertypeOf(value)
return typeVariableFor(a).setType(new WollokAtomicType(VOID))
}

const inferVariable = (v: Variable | Field) => {
const valueTVar = createTypeVariables(v.value)
const valueTVar = inferTypeVariables(v.value)
const varTVar = typeVariableFor(v)
if (valueTVar && !varTVar.closed) varTVar.beSupertypeOf(valueTVar)
return varTVar
Expand All @@ -182,16 +183,16 @@ const inferReturn = (r: Return) => {
const method = r.ancestors.find(is(Method))
if (!method) throw new Error('Method for Return not found')
if (r.value)
typeVariableFor(method).atParam(RETURN).beSupertypeOf(createTypeVariables(r.value)!)
typeVariableFor(method).atParam(RETURN).beSupertypeOf(inferTypeVariables(r.value)!)
else
typeVariableFor(method).atParam(RETURN).setType(new WollokAtomicType(VOID))
return typeVariableFor(r).setType(new WollokAtomicType(VOID))
}

const inferIf = (_if: If) => {
createTypeVariables(_if.condition)!.setType(new WollokModuleType(_if.environment.booleanClass))
createTypeVariables(_if.thenBody)
createTypeVariables(_if.elseBody)
inferTypeVariables(_if.condition)!.setType(new WollokModuleType(_if.environment.booleanClass))
inferTypeVariables(_if.thenBody)
inferTypeVariables(_if.elseBody)
if (_if.elseBody.sentences.length) {
typeVariableFor(_if)
.beSupertypeOf(typeVariableFor(last(_if.elseBody.sentences)!))
Expand Down Expand Up @@ -233,7 +234,7 @@ const inferLiteral = (l: Literal) => {
const arrayLiteralType = (value: readonly [Reference<Class>, List<Expression>]) => {
const arrayTVar = typeForModule(value[0].target!)
const elementTVar = arrayTVar.atParam(ELEMENT)
value[1].map(createTypeVariables).forEach(inner =>
value[1].map(inferTypeVariables).forEach(inner =>
elementTVar.beSupertypeOf(inner!)
)
return arrayTVar
Expand Down

0 comments on commit 36180df

Please sign in to comment.