Skip to content

Commit

Permalink
add missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Apr 11, 2023
1 parent 97bd75a commit 302e1eb
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions lib/flow/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@

const is = Object.is || ((a, b) => {
if (isNaN(a)) return isNaN(b);
return a === b;
});

export class Type {
constructor(types) {
if (new.target === Type) {
return new GenericType(types);
} else {
this.types = types;
}
}

OR(other) {
if (other === UnknownType) return other;

const all_types = new Set([...this.types, ...other.types]);
return new GenericType([...all_types]);
}

equals(other) {
throw new Error("not implemented (Type.equals)");
}

is_truthy() {
return undefined;
}
}

export class GenericType extends Type {
constructor(types) {
super(types);
}

OR(other) {
if (other === UnknownType || this.equals(other)) return other;

return super.OR(other);
}

equals(other) {
return other === this
|| other instanceof GenericType
&& this.types.length === other.types.length
&& this.types.every((type, i) => type === other.types[i]);
}

is_truthy() {
return undefined;
}
}

export class LiteralType extends Type {
constructor(value) {
super([typeof value]);
this.value = value;
}

OR(other) {
if (other === UnknownType || this.equals(other)) return other;

return super.OR(other);
}

equals(other) {
return other === this
|| other instanceof LiteralType
&& is(other.value, this.value);
}


is_truthy() {
return !!this.value;
}
}

export class FunctionType extends Type {
constructor(node, parent_world) {
super(["function"]);
this.node = node;
this.parent_world = parent_world;
}

OR(other) {
if (other === UnknownType || this.equals(other)) return other;

return super.OR(other);
}

equals(other) {
return other === this
|| other instanceof FunctionType
&& other.node === this.node
&& other.parent_world === this.parent_world;
}

is_truthy() {
return true;
}
}

export const UnknownType = new (class extends Type {
constructor() { super([]); }

OR(_other) { return this; }
is_truthy() { return undefined; }
})();

0 comments on commit 302e1eb

Please sign in to comment.