ifc-expressions
Version:
Parsing and evaluation of IFC expressions
97 lines (96 loc) • 2.98 kB
JavaScript
import { ArrayType } from "./ArrayType.js";
import { TypeDisjunction } from "./TypeDisjunction.js";
export class TupleType {
constructor(...types) {
this.types = types;
Object.freeze(types);
}
getTypes() {
return this.types;
}
toArrayType() {
return new ArrayType(new TypeDisjunction(...this.getTypes()));
}
getName() {
return "<" + this.types.map((t) => t.getName()).join(", ") + ">";
}
isAssignableFrom(other) {
if (other instanceof TupleType) {
return this.compareToOtherTuple(other, (t, to) => t.isAssignableFrom(to));
}
return false;
}
isSameTypeAs(other) {
if (other instanceof TupleType) {
return this.compareToOtherTuple(other, (t, to) => t.isSameTypeAs(to));
}
return false;
}
isSubTypeOf(other) {
if (other instanceof TupleType) {
return this.compareToOtherTuple(other, (t, to) => t.isSubTypeOf(to));
}
else if (other instanceof ArrayType) {
for (const t of this.types) {
if (!(t.isSubTypeOf(other.getElementType()) ||
t.isSameTypeAs(other.getElementType()))) {
return false;
}
}
return true;
}
else if (other instanceof TypeDisjunction) {
return other.isSuperTypeOf(this);
}
return false;
}
compareToOtherTuple(other, cmp) {
const n = this.types.length;
if (other.types.length !== n) {
return false;
}
for (let i = 0; i < n; i++) {
const t = this.types[i];
const to = other.types[i];
if (!cmp(t, to)) {
return false;
}
}
return true;
}
isSuperTypeOf(other) {
if (other instanceof TupleType) {
return this.compareToOtherTuple(other, (t, to) => t.isSuperTypeOf(to));
}
else if (other instanceof ArrayType) {
for (const t of this.types) {
if (!t.isSuperTypeOf(other.getElementType())) {
return false;
}
}
return true;
}
else if (other instanceof TypeDisjunction) {
return other.isSubTypeOf(this);
}
return false;
}
overlapsWith(other) {
if (other instanceof TupleType) {
return this.compareToOtherTuple(other, (t, to) => t.overlapsWith(to));
}
else if (other instanceof ArrayType) {
for (const t of this.types) {
if (!t.overlapsWith(other.getElementType())) {
return false;
}
}
return true;
}
else if (other instanceof TypeDisjunction) {
return other.overlapsWith(this);
}
return false;
}
}
//# sourceMappingURL=TupleType.js.map