Skip to content

Commit

Permalink
Refactor semantic check; revive e2e spec
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Sep 9, 2024
1 parent 41549a0 commit eba81a1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 36 deletions.
16 changes: 4 additions & 12 deletions src/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { buildModule } from './package/build'
import { emitPackage } from './package/emit'
import { buildPackage } from './package/io'
import { Context, pathToId } from './scope'
import { buildInstanceRelations } from './scope/trait'
import { checkModule, checkTopLevelDefinition, prepareModule } from './semantic'
import { Source } from './source'

const compile = async (files: { [path: string]: string }): Promise<Context> => {
Expand All @@ -23,13 +21,10 @@ const compile = async (files: { [path: string]: string }): Promise<Context> => {
config,
moduleStack: [],
packages: [],
impls: [],
stdTypeIds: {},
errors: [],
warnings: [],
check: false,
silent: false,
variableCounter: 0,
relChainsMemo: new Map()
}

const modules = Object.entries(files).map(([filepath, code]) => {
Expand All @@ -46,7 +41,7 @@ const compile = async (files: { [path: string]: string }): Promise<Context> => {
const std = buildPackage('tmp/dist/std', 'std', ctx, true)!

ctx.packages = [std, pkg]
ctx.prelude = std.modules.find(m => m.identifier.names.at(-1)! === 'prelude')!
ctx.prelude = std.modules.find(m => m.identifier.names.at(-1)!.value === 'prelude')!

ctx.packages.forEach(p => {
p.modules.forEach(m => {
Expand Down Expand Up @@ -76,13 +71,10 @@ const compileStd = async (): Promise<void> => {
config,
moduleStack: [],
packages: [],
impls: [],
stdTypeIds: {},
errors: [],
warnings: [],
check: false,
silent: false,
variableCounter: 0,
relChainsMemo: new Map()
variableCounter: 0
}
const pkg = buildPackage(config.pkgPath, config.pkgName!, ctx)!

Expand Down
27 changes: 3 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,8 @@ import { Package } from './package'
import { buildModule } from './package/build'
import { emitPackage } from './package/emit'
import { buildPackage } from './package/io'
import { registerImpl } from './phase/impl-register'
import { resolveImports, setExports } from './phase/import-resolve'
import { resolveModuleScope } from './phase/module-resolve'
import { resolveName } from './phase/name-resolve'
import { setStdTypeIds } from './phase/std-type'
import { desugar1 } from './phase/sugar'
import { setTopScopeDefType, setTopScopeType } from './phase/top-scope-type'
import { collectTypeBounds } from './phase/type-bound'
import { unifyTypeBounds } from './phase/type-unify'
import { Context, eachModule, pathToId } from './scope'
import { Context, pathToId } from './scope'
import { semanticCheck } from './semantic'
import { Source } from './source'
import { assert } from './util/todo'

Expand Down Expand Up @@ -128,20 +120,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,
setStdTypeIds,
registerImpl,
desugar1,
resolveName,
setTopScopeDefType,
setTopScopeType,
collectTypeBounds,
unifyTypeBounds
]
phases.forEach(f => eachModule(f, ctx))
semanticCheck(ctx)

// biome-ignore lint:
// console.log(inspect(debugAst(pkg.modules[0].block), { compact: true, depth: null, breakLength: 120 }))
Expand Down
27 changes: 27 additions & 0 deletions src/semantic/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { registerImpl } from '../phase/impl-register'
import { resolveImports, setExports } from '../phase/import-resolve'
import { resolveModuleScope } from '../phase/module-resolve'
import { resolveName } from '../phase/name-resolve'
import { setStdTypeIds } from '../phase/std-type'
import { desugar1 } from '../phase/sugar'
import { setTopScopeDefType, setTopScopeType } from '../phase/top-scope-type'
import { collectTypeBounds } from '../phase/type-bound'
import { unifyTypeBounds } from '../phase/type-unify'
import { Context, eachModule } from '../scope'

export const semanticCheck = (ctx: Context): void => {
const phases = [
resolveModuleScope,
setExports,
resolveImports,
setStdTypeIds,
registerImpl,
desugar1,
resolveName,
setTopScopeDefType,
setTopScopeType,
collectTypeBounds,
unifyTypeBounds
]
phases.forEach(f => eachModule(f, ctx))
}

0 comments on commit eba81a1

Please sign in to comment.