Skip to content

Commit

Permalink
the module now looks into the kind registry, whether the kind is alre…
Browse files Browse the repository at this point in the history
…ady initialized, before a new one is created #44
  • Loading branch information
JohannesMeierSE committed Dec 17, 2024
1 parent 36a6b8c commit 4022e05
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
24 changes: 19 additions & 5 deletions packages/typir/src/services/kind-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@
******************************************************************************/

import { Kind } from '../kinds/kind.js';
import { TypirServices } from '../typir.js';

export interface KindRegistry {
register(kind: Kind): void;
get(type: string): Kind | undefined;
get<T extends Kind>(type: T['$name']): T | undefined;
getOrCreateKind<T extends Kind>(type: T['$name'], factory: (services: TypirServices) => T): T;
}

export class DefaultKindRegistry implements KindRegistry {
// name of kind => kind (for an easier look-up)
protected readonly kinds: Map<string, Kind> = new Map();
protected readonly services: TypirServices;
protected readonly kinds: Map<string, Kind> = new Map(); // name of kind => kind (for an easier look-up)

constructor(services: TypirServices) {
this.services = services;
}

register(kind: Kind): void {
const key = kind.$name;
Expand All @@ -28,7 +34,15 @@ export class DefaultKindRegistry implements KindRegistry {
}
}

get(type: string): Kind | undefined {
return this.kinds.get(type)!;
get<T extends Kind>(type: T['$name']): T | undefined {
return this.kinds.get(type) as (T | undefined);
}

getOrCreateKind<T extends Kind>(type: T['$name'], factory: (services: TypirServices) => T): T {
const existing = this.get(type);
if (existing) {
return existing;
}
return factory(this.services);
}
}
22 changes: 11 additions & 11 deletions packages/typir/src/typir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

import { TypeGraph } from './graph/type-graph.js';
import { DefaultTypeResolver, TypeResolvingService } from './initialization/type-selector.js';
import { BottomFactoryService, BottomKind } from './kinds/bottom/bottom-kind.js';
import { ClassFactoryService, ClassKind } from './kinds/class/class-kind.js';
import { FunctionKind, FunctionFactoryService } from './kinds/function/function-kind.js';
import { PrimitiveFactoryService, PrimitiveKind } from './kinds/primitive/primitive-kind.js';
import { TopFactoryService, TopKind } from './kinds/top/top-kind.js';
import { BottomFactoryService, BottomKind, BottomKindName } from './kinds/bottom/bottom-kind.js';
import { ClassFactoryService, ClassKind, ClassKindName } from './kinds/class/class-kind.js';
import { FunctionFactoryService, FunctionKind, FunctionKindName } from './kinds/function/function-kind.js';
import { PrimitiveFactoryService, PrimitiveKind, PrimitiveKindName } from './kinds/primitive/primitive-kind.js';
import { TopFactoryService, TopKind, TopKindName } from './kinds/top/top-kind.js';
import { DefaultTypeAssignability, TypeAssignability } from './services/assignability.js';
import { DefaultDomainElementInferenceCaching, DefaultTypeRelationshipCaching, DomainElementInferenceCaching, TypeRelationshipCaching } from './services/caching.js';
import { DefaultTypeConversion, TypeConversion } from './services/conversion.js';
Expand Down Expand Up @@ -86,16 +86,16 @@ export const DefaultTypirServiceModule: Module<TypirServices> = {
Constraints: (services) => new DefaultValidationConstraints(services),
},
factory: {
Primitives: (services) => new PrimitiveKind(services),
Functions: (services) => new FunctionKind(services),
Classes: (services) => new ClassKind(services, { typing: 'Nominal' }),
Top: (services) => new TopKind(services),
Bottom: (services) => new BottomKind(services),
Primitives: (services) => services.infrastructure.Kinds.getOrCreateKind(PrimitiveKindName, services => new PrimitiveKind(services)),
Functions: (services) => services.infrastructure.Kinds.getOrCreateKind(FunctionKindName, services => new FunctionKind(services)),
Classes: (services) => services.infrastructure.Kinds.getOrCreateKind(ClassKindName, services => new ClassKind(services, { typing: 'Nominal' })),
Top: (services) => services.infrastructure.Kinds.getOrCreateKind(TopKindName, services => new TopKind(services)),
Bottom: (services) => services.infrastructure.Kinds.getOrCreateKind(BottomKindName, services => new BottomKind(services)),
Operators: (services) => new DefaultOperatorFactory(services),
},
infrastructure: {
Graph: () => new TypeGraph(),
Kinds: () => new DefaultKindRegistry(),
Kinds: (services) => new DefaultKindRegistry(services),
TypeResolver: (services) => new DefaultTypeResolver(services),
},
};
Expand Down

0 comments on commit 4022e05

Please sign in to comment.