Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Jul 27, 2023
1 parent e2965d7 commit 51326e3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {
isNumericLiteral,
isSameType,
isStringLiteral,
isStructInstance,
literal,
} from './types-util';
export { intersect, isDisjointWith } from './intersection';
Expand Down
5 changes: 2 additions & 3 deletions src/struct-constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { canonicalize } from './canonical';
import { StructDescriptor, UnionType } from './types';
import { EMPTY_ARRAY } from './util';

const trueDescriptor = new StructDescriptor('true', EMPTY_ARRAY);
const falseDescriptor = new StructDescriptor('false', EMPTY_ARRAY);
const trueDescriptor = new StructDescriptor('true');
const falseDescriptor = new StructDescriptor('false');

export const BOOL_TRUE = trueDescriptor.default;
export const BOOL_FALSE = falseDescriptor.default;
Expand Down
15 changes: 14 additions & 1 deletion src/struct-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NamedExpression } from './expression';
import { isSubsetOf } from './relation';
import { Scope } from './scope';
import { NonNeverType, StructDescriptor, StructInstanceType } from './types';
import { isStructInstance } from './types-util';
import { isReadonlyArray } from './util';

const createInstanceFromFields = (
Expand Down Expand Up @@ -119,16 +120,28 @@ export const getStructDescriptor = (scope: Scope, name: string): StructDescripto

// the descriptor has already been added
if (definition.descriptor) {
if (definition.descriptor.name !== name) {
throw new Error(
`Expected struct descriptor to have name ${name}, found ${definition.descriptor.name}. This is a bug in the implementation.`
);
}
return definition.descriptor;
}

// the descriptor has not been added yet,
// so we need to evaluate the default instance of the struct
const instance = evaluate(new NamedExpression(name), scope);
if (instance.underlying !== 'struct' || instance.type !== 'instance') {
if (!isStructInstance(instance)) {
throw new Error(
'Internal implementation error: Expected struct instance. This is a bug in the implementation.'
);
}

if (instance.descriptor.name !== name) {
throw new Error(
`Expected struct descriptor to have name ${name}, found ${instance.descriptor.name}. This is a bug in the implementation.`
);
}

return instance.descriptor;
};
8 changes: 6 additions & 2 deletions src/types-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
NonIntIntervalType,
NumericLiteralType,
StringLiteralType,
StructInstanceType,
Type,
WithUnderlying,
} from './types';
Expand Down Expand Up @@ -73,10 +74,13 @@ export const nonIntInterval = (min: number, max: number) => {
};

export const isNumericLiteral = (type: Type): type is NumericLiteralType => {
return type.type === 'literal' && type.underlying === 'number';
return type.underlying === 'number' && type.type === 'literal';
};
export const isStringLiteral = (type: Type): type is StringLiteralType => {
return type.type === 'literal' && type.underlying === 'string';
return type.underlying === 'string' && type.type === 'literal';
};
export const isStructInstance = (type: Type): type is StructInstanceType => {
return type.underlying === 'struct' && type.type === 'instance';
};

export const newBounds = (minExclusive: boolean, maxExclusive: boolean): Bounds => {
Expand Down

0 comments on commit 51326e3

Please sign in to comment.