Skip to content

Commit

Permalink
Join inherent impls with their type def; resolve methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Aug 3, 2024
1 parent 40bdf77 commit 855ad73
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/ast/type-def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BaseAstNode } from '.'
import { ParseNode, filterNonAstNodes } from '../parser'
import { Context } from '../scope'
import { Name, buildName } from './operand'
import { ImplDef } from './statement'
import { Generic, Type, buildGeneric, buildType } from './type'

export type TypeDef = BaseAstNode & {
Expand All @@ -10,6 +11,7 @@ export type TypeDef = BaseAstNode & {
generics: Generic[]
variants: Variant[]
pub: boolean
impl?: ImplDef
}

export const buildTypeDef = (node: ParseNode, ctx: Context): TypeDef => {
Expand Down
6 changes: 5 additions & 1 deletion src/phase/module-resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AstNode } from '../ast'
import { Context, Definition, addError, defKey } from '../scope'
import { duplicateDefError } from '../semantic/error'
import { todo } from '../util/todo'
import { findById } from './name-resolve'

/**
* Resolve module definitions available from the outside
Expand All @@ -21,7 +22,10 @@ export const resolveModuleScope = (node: AstNode, ctx: Context): void => {
break
}
case 'impl-def': {
// TODO
if (node.forTrait) break
const typeDef = findById(node.identifier, ctx)
if (typeDef?.kind !== 'type-def') break
typeDef.impl = node
break
}
case 'var-def': {
Expand Down
15 changes: 10 additions & 5 deletions src/phase/name-resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ export const findById = (id: Identifier, ctx: Context): Definition | undefined =
return findWithinDef(def, id.names[1], ctx)
}

export const findNameInStack = (name: string, stack: DefinitionMap): Definition | undefined => {
return stack.get(name)
}

const addDef = (name: string, def: Definition, ctx: Context): void => {
const m = ctx.moduleStack.at(-1)!
const scope = m.scopeStack.at(-1) ?? m.topScope
Expand All @@ -278,7 +282,12 @@ const findWithinDef = (def: Definition, name: Name, ctx: Context): Definition |
return <FnDef | undefined>def.block.statements.find(s => s.kind === 'fn-def' && defKey(s) === key)
}
case 'type-def': {
return def.variants.find(v => defKey(v) === key)
const variant = def.variants.find(v => defKey(v) === key)
if (variant) return variant
if (def.impl) {
return findWithinDef(def.impl, name, ctx)
}
return undefined
}
default: {
// TODO: report error
Expand All @@ -287,10 +296,6 @@ const findWithinDef = (def: Definition, name: Name, ctx: Context): Definition |
}
}

const findNameInStack = (name: string, stack: DefinitionMap): Definition | undefined => {
return stack.get(name)
}

const withScope = <T>(ctx: Context, f: () => T): T => {
const m = ctx.moduleStack.at(-1)!
m.scopeStack.push(new Map())
Expand Down
4 changes: 2 additions & 2 deletions src/phase/type-bound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Context } from '../scope'
import { operatorImplMap } from '../semantic/op'
import { InferredType, makeInferredType } from '../typecheck'
import { boolId, charId, floatId, intId, stringId, unitId } from '../typecheck/type'
import { assert, todo } from '../util/todo'
import { assert } from '../util/todo'
import { findById } from './name-resolve'

/**
Expand Down Expand Up @@ -202,7 +202,7 @@ export const collectTypeBounds = (node: AstNode, ctx: Context, parentBound?: Inf
assert(!!methodId)
const methodDef = findById(methodId!, ctx)
if (methodDef) {
todo()
// TODO
}
// TODO
break
Expand Down

0 comments on commit 855ad73

Please sign in to comment.