Skip to content

Commit

Permalink
Known type hint
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Aug 3, 2024
1 parent 379a534 commit 9b3f1a8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
15 changes: 8 additions & 7 deletions src/phase/type-bound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ export const collectTypeBounds = (node: AstNode, ctx: Context, parentBound?: Typ
case 'param': {
// TODO: handle self
if (node.paramType) return undefined
resolveTypeRef(node.type!).known = node.paramType
collectTypeBounds(node.pattern, ctx, node.paramType)
return node.paramType
return node.type
}
case 'generic': {
// TODO
Expand Down Expand Up @@ -111,7 +112,7 @@ export const collectTypeBounds = (node: AstNode, ctx: Context, parentBound?: Typ
}
case 'string-interpolated': {
node.tokens.filter(t => typeof t !== 'string').forEach(t => collectTypeBounds(t, ctx))
addBound(node.type!, stringVid)
resolveTypeRef(node.type!).known = stringVid
break
}
case 'operand-expr': {
Expand Down Expand Up @@ -163,23 +164,23 @@ export const collectTypeBounds = (node: AstNode, ctx: Context, parentBound?: Typ
break
}
case 'string-literal': {
addBound(node.type!, stringVid)
resolveTypeRef(node.type!).known = stringVid
break
}
case 'char-literal': {
addBound(node.type!, charVid)
resolveTypeRef(node.type!).known = charVid
break
}
case 'int-literal': {
addBound(node.type!, intVid)
resolveTypeRef(node.type!).known = intVid
break
}
case 'float-literal': {
addBound(node.type!, floatVid)
resolveTypeRef(node.type!).known = floatVid
break
}
case 'bool-literal': {
addBound(node.type!, boolVid)
resolveTypeRef(node.type!).known = boolVid
break
}
case 'method-call-op': {
Expand Down
5 changes: 3 additions & 2 deletions src/typecheck/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import { Identifier } from '../ast/operand'
import { Type } from '../ast/type'
import { ParseNode } from '../parser'

export type InferredType = InferredTypeDef | { kind: 'ref'; ref: InferredTypeDef }
export type InferredType = InferredTypeDef | { kind: 'ref'; ref: InferredType }

export type InferredTypeDef = {
kind: 'inferred'
known?: Type
bounds: Type[]
unified?: Type
}

export const makeInferredType = (bounds: Type[] = []): InferredType => ({ kind: 'inferred', bounds })

export const resolveTypeRef = (t: InferredType): InferredTypeDef => (t.kind === 'inferred' ? t : resolveTypeRef(t.ref))
export const resolveTypeRef = (t: InferredType): InferredTypeDef => (t.kind === 'ref' ? resolveTypeRef(t.ref) : t)

export const typeToString = (t: Type): string => {
switch (t.kind) {
Expand Down

0 comments on commit 9b3f1a8

Please sign in to comment.