Skip to content

Commit

Permalink
Unify type bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Aug 17, 2024
1 parent cddf807 commit 79d5197
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/phase/top-scope-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const setTopScopeType = (node: AstNode, ctx: Context) => {
typeArgs: [],
def: node
}
node.type = makeInferredFromType(nodeId)
node.type = makeTemplateType(nodeId)
node.variants.forEach(v => {
v.type = makeTemplateType({
kind: 'inferred-fn',
Expand All @@ -61,6 +61,7 @@ export const setTopScopeType = (node: AstNode, ctx: Context) => {
}
case 'generic': {
node.type = makeInferredType()
break
}
}
}
27 changes: 18 additions & 9 deletions src/phase/type-bound.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AstNode } from '../ast'
import { FnDef } from '../ast/statement'
import { Context } from '../scope'
import { Context, addError } from '../scope'
import { genericError } from '../semantic/error'
import { operatorImplMap } from '../semantic/op'
import {
InferredType,
Expand All @@ -10,8 +11,8 @@ import {
makeInferredType,
makeReturnType
} from '../typecheck'
import { boolType, charType, floatType, intType, stringType } from '../typecheck/type'
import { assert, unreachable } from '../util/todo'
import { boolType, charType, floatType, intType, stringType, unitType } from '../typecheck/type'
import { assert } from '../util/todo'
import { findById, findParent } from './name-resolve'

/**
Expand Down Expand Up @@ -45,6 +46,7 @@ export const collectTypeBounds = (node: AstNode, ctx: Context, parentBound?: Inf
case 'int-literal':
case 'float-literal':
case 'bool-literal':
case 'var-def':
node.type ??= makeInferredType()
}
if (node.type && node.type.kind === 'inferred') {
Expand Down Expand Up @@ -124,7 +126,6 @@ export const collectTypeBounds = (node: AstNode, ctx: Context, parentBound?: Inf
case 'identifier':
case 'name': {
if (node.def) {
assert(!!node.def.type)
node.type = instantiateTemplateType(node.def.type!)
break
}
Expand Down Expand Up @@ -157,6 +158,10 @@ export const collectTypeBounds = (node: AstNode, ctx: Context, parentBound?: Inf
break
}
case 'binary-expr': {
if (node.binaryOp.kind === 'assign-op') {
// TODO
break
}
collectTypeBounds(node.lOperand, ctx)
collectTypeBounds(node.rOperand, ctx)
const methodId = operatorImplMap.get(node.binaryOp.kind)
Expand Down Expand Up @@ -193,22 +198,26 @@ export const collectTypeBounds = (node: AstNode, ctx: Context, parentBound?: Inf
collectTypeBounds(node.expr, ctx, node.varType ? makeInferredFromType(node.varType) : undefined)
}
collectTypeBounds(node.pattern, ctx, node.expr?.type)
node.type = instantiateTemplateType(unitType)
break
}
case 'fn-def': {
node.generics.forEach(g => collectTypeBounds(g, ctx))
node.params.forEach(p => collectTypeBounds(p, ctx))
if (node.block) {
if (node.type?.kind !== 'template' || node.type.type.kind !== 'inferred-fn') return unreachable()
if (node.type?.kind !== 'template' || node.type.type.kind !== 'inferred-fn') {
addError(ctx, genericError(ctx, node, 'no type'))
break
// return unreachable()
}
collectTypeBounds(node.block, ctx, node.type.type.returnType)
}
break
}
case 'trait-def': {
// TODO
break
}
case 'trait-def':
case 'impl-def': {
if (node.kind === 'impl-def' && node.forTrait) break
node.block.statements.forEach(s => collectTypeBounds(s, ctx))
// TODO
break
}
Expand Down
2 changes: 1 addition & 1 deletion src/typecheck/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const addBounds = (type: InferredType, bounds: InferredType[]): void => {

export const instantiateTemplateType = (t: InferredType): InferredType => {
if (t.kind === 'template') {
return makeInferredType([structuredClone(t.type)])
return makeInferredType([{ ...t.type }])
}
return t
}
Expand Down

0 comments on commit 79d5197

Please sign in to comment.