Skip to content

Commit

Permalink
Set type for self param
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Aug 4, 2024
1 parent 855ad73 commit c059f7d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Package } from './package'
import { buildModule } from './package/build'
import { emitPackage } from './package/emit'
import { buildPackage } from './package/io'
import { Context, pathToVid } from './scope'
import { Context, pathToId } from './scope'
import { buildInstanceRelations } from './scope/trait'
import { checkModule, checkTopLevelDefinition, prepareModule } from './semantic'
import { Source } from './source'
Expand Down Expand Up @@ -34,7 +34,7 @@ const compile = async (files: { [path: string]: string }): Promise<Context> => {

const modules = Object.entries(files).map(([filepath, code]) => {
const source: Source = { code, filepath: join(config.srcPath, filepath) }
return buildModule(source, pathToVid(join('test', filepath)), ctx)!
return buildModule(source, pathToId(join('test', filepath)), ctx)!
})
const pkg: Package = {
path: pkgPath,
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { buildPackage } from './package/io'
import { resolveImports, setExports } from './phase/import-resolve'
import { resolveModuleScope } from './phase/module-resolve'
import { resolveName } from './phase/name-resolve'
import { desugar1 } from './phase/sugar'
import { collectTypeBounds, setPubType } from './phase/type-bound'
import { Context, eachModule, pathToId } from './scope'
import { Source } from './source'
Expand Down Expand Up @@ -124,7 +125,7 @@ ctx.packages = packages
ctx.prelude = std.modules.find(m => m.identifier.names.at(-1)!.value === 'prelude')!
assert(!!ctx.prelude, 'no prelude')

const phases = [resolveModuleScope, setExports, resolveImports, resolveName, setPubType, collectTypeBounds]
const phases = [resolveModuleScope, setExports, resolveImports, desugar1, resolveName, setPubType, collectTypeBounds]
phases.forEach(f => eachModule(f, ctx))

reportErrors(ctx)
Expand Down
33 changes: 33 additions & 0 deletions src/phase/sugar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { AstNode } from '../ast'
import { Context, idFromString } from '../scope'

/**
* Desugar phase that runs before name resolution
* Does:
* - populates type of the `self` param
*/
export const desugar1 = (node: AstNode, ctx: Context, parent?: AstNode) => {
switch (node.kind) {
case 'module': {
node.block.statements.forEach(s => desugar1(s, ctx))
break
}
case 'trait-def':
case 'impl-def': {
node.block.statements.forEach(s => desugar1(s, ctx, node))
break
}
case 'fn-def': {
node.params.forEach((p, i) => {
if (parent && (parent.kind === 'impl-def' || parent.kind === 'trait-def')) {
if (!p.paramType && i === 0) {
const selfType = idFromString('Self')
selfType.parseNode = p.parseNode
p.paramType = selfType
}
}
})
break
}
}
}
13 changes: 5 additions & 8 deletions src/phase/type-bound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { findById } from './name-resolve'
/**
* Set known types of public nodes
*/
export const setPubType = (node: AstNode, ctx: Context, parent?: AstNode) => {
export const setPubType = (node: AstNode, ctx: Context) => {
switch (node.kind) {
case 'module': {
node.block.statements.forEach(s => setPubType(s, ctx))
Expand All @@ -25,11 +25,9 @@ export const setPubType = (node: AstNode, ctx: Context, parent?: AstNode) => {
}
case 'fn-def': {
node.params.forEach(p => {
p.type = makeInferredType()
if (p.paramType) {
p.type = makeInferredType()
p.type.known = p.paramType
} else {
// TODO: self param
}
})
node.type = makeInferredType()
Expand Down Expand Up @@ -69,7 +67,7 @@ export const setPubType = (node: AstNode, ctx: Context, parent?: AstNode) => {
case 'trait-def':
case 'impl-def': {
if (node.kind === 'impl-def' && node.forTrait) break
node.block.statements.forEach(s => setPubType(s, ctx, node))
node.block.statements.forEach(s => setPubType(s, ctx))
break
}
}
Expand Down Expand Up @@ -201,9 +199,8 @@ export const collectTypeBounds = (node: AstNode, ctx: Context, parentBound?: Inf
const methodId = operatorImplMap.get(node.binaryOp.kind)
assert(!!methodId)
const methodDef = findById(methodId!, ctx)
if (methodDef) {
// TODO
}
assert(!!methodDef)
node.type!.bounds.push(methodDef!.type!)
// TODO
break
}
Expand Down
4 changes: 2 additions & 2 deletions src/semantic/semantic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { makeConfig } from '../config'
import { Package } from '../package'
import { buildModule } from '../package/build'
import { buildPackage } from '../package/io'
import { Context, pathToVid } from '../scope'
import { Context, pathToId } from '../scope'
import { Source } from '../source'
import { checkModule, checkTopLevelDefinition, prepareModule } from './index'

Expand All @@ -24,7 +24,7 @@ describe('semantic', () => {
relChainsMemo: new Map()
}

const moduleAst = buildModule(source, pathToVid(source.filepath), ctx)!
const moduleAst = buildModule(source, pathToId(source.filepath), ctx)!
const pkg: Package = {
path: source.filepath,
name: moduleAst?.identifier.names.at(-1)!,
Expand Down

0 comments on commit c059f7d

Please sign in to comment.