Skip to content

Commit

Permalink
tir types
Browse files Browse the repository at this point in the history
  • Loading branch information
michele-nuzzi committed Feb 7, 2025
1 parent 6b7db32 commit b45874c
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions src/tir/TirAliasType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { TirInterfaceImpl } from "./TirInterfaceImpl";
import { TirType } from "./TirType";
import { TirTypeParam } from "./TirTypeParam";

export class TirAliasType
{
constructor(
readonly name: string,
readonly tyArgs: TirTypeParam[],
readonly aliased: TirType,
readonly impls: TirInterfaceImpl[]
) {}
}
11 changes: 11 additions & 0 deletions src/tir/TirInterfaceImpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { TirInterfaceType } from "./TirInterfaceType";
import { TirCustomType } from "./TirType";


export class TirInterfaceImpl
{
constructor(
readonly targetType: TirCustomType,
readonly interf: TirInterfaceType
) {}
}
27 changes: 27 additions & 0 deletions src/tir/TirInterfaceType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { TirType } from "./TirType";
import { TirTypeParam } from "./TirTypeParam";

export class TirInterfaceType
{
constructor(
// anonymos interface for custom type methods
readonly name: string | undefined,
readonly typeParams: TirTypeParam[],
readonly methods: TirInterfaceMethod[]
) {
for( const method of methods )
{
method.parentInterface = this;
}
}
}

export class TirInterfaceMethod
{
constructor(
public parentInterface: TirInterfaceType,
readonly name: string,
readonly params: TirType[],
readonly returnType: TirType
) {}
}
62 changes: 62 additions & 0 deletions src/tir/TirNativeType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { TirType } from "./TirType";

export type TirNativeType
= TirVoidT
| TirBoolT
| TirNumT
| TirBytesT
| TirOptT<TirType>
| TirListT<TirType>
| TirLinearMapT<TirType,TirType>
| TirFuncT
;

export function isTirNativeType( t: any ): t is TirNativeType
{
return (
t instanceof TirVoidT
|| t instanceof TirBoolT
|| t instanceof TirNumT
|| t instanceof TirBytesT
|| t instanceof TirOptT
|| t instanceof TirListT
|| t instanceof TirLinearMapT
|| t instanceof TirFuncT
);
}

export class TirVoidT {}
export class TirBoolT {}
export class TirNumT {}
export class TirBytesT {}

export class TirOptT<T extends TirType>
{
constructor(
readonly typeArg: T
) {}
}

export class TirListT<T extends TirType>
{
constructor(
readonly typeArg: T
) {}
}

export class TirLinearMapT<K extends TirType,V extends TirType>
{
constructor(
readonly keyTypeArg: K,
readonly valTypeArg: V
) {}
}

export class TirFuncT
{
constructor(
// readonly genericTyArgsName: TirTypeParam[],
readonly argTypes: TirType[],
readonly returnType: TirType
) {}
}
27 changes: 27 additions & 0 deletions src/tir/TirStructType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { TirInterfaceImpl } from "./TirInterfaceImpl";
import { TirType } from "./TirType";

export class TirStructType
{
constructor(
readonly name: string,
readonly constructors: TirStructConstr[],
readonly impls: TirInterfaceImpl[]
) {}
}

export class TirStructConstr
{
constructor(
readonly name: string,
readonly fields: TirStructField[]
) {}
}

export class TirStructField
{
constructor(
readonly name: string,
readonly type: TirType
) {}
}
37 changes: 37 additions & 0 deletions src/tir/TirType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { isObject } from "@harmoniclabs/obj-utils";
import { isTirNativeType, TirNativeType } from "./TirNativeType";
import { TirTypeParam } from "./TirTypeParam";
import { TirAliasType } from "./TirAliasType";
import { TirStructType } from "./TirStructType";
// import { TirInterfaceType } from "./TirInterfaceType";


export type TirCustomType
= TirAliasType
| TirStructType
;

export function isTirCustomType( thing: any ): thing is TirCustomType
{
return isObject( thing ) && (
thing instanceof TirAliasType
|| thing instanceof TirStructType
);
}

export type TirType
= TirNativeType
| TirTypeParam
| TirCustomType
// | TirInterfaceType
;

export function isTirType( thing: any ): thing is TirType
{
return isObject( thing ) && (
isTirNativeType( thing )
|| thing instanceof TirTypeParam
|| isTirCustomType( thing )
// || thing instanceof TirInterfaceType
);
}
7 changes: 7 additions & 0 deletions src/tir/TirTypeParam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

export class TirTypeParam
{
constructor(
readonly name: string
) {}
}

0 comments on commit b45874c

Please sign in to comment.