2022-08-28 16:19:44 +00:00
|
|
|
import * as RSValue from "../rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue.gen";
|
|
|
|
import { squiggleValueTag as Tag } from "../rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue_tag";
|
|
|
|
import { wrapDistribution } from "./SqDistribution";
|
|
|
|
import { SqLambda } from "./SqLambda";
|
|
|
|
import { SqLambdaDeclaration } from "./SqLambdaDeclaration";
|
|
|
|
import { SqModule } from "./SqModule";
|
|
|
|
import { SqRecord } from "./SqRecord";
|
|
|
|
import { SqArray } from "./SqArray";
|
|
|
|
import { SqType } from "./SqType";
|
2022-08-29 21:51:44 +00:00
|
|
|
import { SqProject } from "./SqProject";
|
|
|
|
import { SqValueLocation } from "./SqValueLocation";
|
2022-08-28 16:19:44 +00:00
|
|
|
|
|
|
|
export { Tag as SqValueTag };
|
|
|
|
|
|
|
|
type T = RSValue.squiggleValue;
|
|
|
|
|
2022-08-29 21:51:44 +00:00
|
|
|
export const wrapValue = (value: T, location: SqValueLocation): SqValue => {
|
2022-08-28 16:19:44 +00:00
|
|
|
const tag = RSValue.getTag(value);
|
|
|
|
|
2022-08-29 21:51:44 +00:00
|
|
|
return new tagToClass[tag](value, location);
|
2022-08-28 16:19:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export abstract class SqAbstractValue {
|
|
|
|
abstract tag: Tag;
|
|
|
|
|
2022-08-29 21:51:44 +00:00
|
|
|
constructor(private _value: T, public location: SqValueLocation) {}
|
2022-08-28 16:19:44 +00:00
|
|
|
|
2022-08-29 21:51:44 +00:00
|
|
|
protected valueMethod = <IR>(rsMethod: (v: T) => IR | null | undefined) => {
|
|
|
|
const value = rsMethod(this._value);
|
|
|
|
if (value === undefined || value === null) {
|
|
|
|
throw new Error("Internal casting error");
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
2022-09-01 10:36:27 +00:00
|
|
|
|
|
|
|
toString() {
|
|
|
|
return RSValue.toString(this._value);
|
|
|
|
}
|
2022-08-29 21:51:44 +00:00
|
|
|
}
|
2022-08-28 16:19:44 +00:00
|
|
|
|
|
|
|
export class SqArrayValue extends SqAbstractValue {
|
2022-08-28 17:33:16 +00:00
|
|
|
tag = Tag.Array as const;
|
2022-08-28 16:19:44 +00:00
|
|
|
|
|
|
|
get value() {
|
2022-08-29 21:51:44 +00:00
|
|
|
return new SqArray(this.valueMethod(RSValue.getArray), this.location);
|
2022-08-28 16:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SqArrayStringValue extends SqAbstractValue {
|
2022-08-28 17:33:16 +00:00
|
|
|
tag = Tag.ArrayString as const;
|
2022-08-28 16:19:44 +00:00
|
|
|
|
|
|
|
get value() {
|
2022-08-29 21:51:44 +00:00
|
|
|
return this.valueMethod(RSValue.getArrayString);
|
2022-08-28 16:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SqBoolValue extends SqAbstractValue {
|
2022-08-28 17:33:16 +00:00
|
|
|
tag = Tag.Bool as const;
|
2022-08-28 16:19:44 +00:00
|
|
|
|
|
|
|
get value() {
|
2022-08-29 21:51:44 +00:00
|
|
|
return this.valueMethod(RSValue.getBool);
|
2022-08-28 16:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SqCallValue extends SqAbstractValue {
|
2022-08-28 17:33:16 +00:00
|
|
|
tag = Tag.Call as const;
|
2022-08-28 16:19:44 +00:00
|
|
|
|
|
|
|
get value() {
|
2022-08-29 21:51:44 +00:00
|
|
|
return this.valueMethod(RSValue.getCall);
|
2022-08-28 16:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SqDateValue extends SqAbstractValue {
|
2022-08-28 17:33:16 +00:00
|
|
|
tag = Tag.Date as const;
|
2022-08-28 16:19:44 +00:00
|
|
|
|
|
|
|
get value() {
|
2022-08-29 21:51:44 +00:00
|
|
|
return this.valueMethod(RSValue.getDate);
|
2022-08-28 16:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SqDeclarationValue extends SqAbstractValue {
|
2022-08-28 17:33:16 +00:00
|
|
|
tag = Tag.Declaration as const;
|
2022-08-28 16:19:44 +00:00
|
|
|
|
|
|
|
get value() {
|
2022-08-29 21:51:44 +00:00
|
|
|
return new SqLambdaDeclaration(this.valueMethod(RSValue.getDeclaration));
|
2022-08-28 16:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SqDistributionValue extends SqAbstractValue {
|
2022-08-28 17:33:16 +00:00
|
|
|
tag = Tag.Distribution as const;
|
2022-08-28 16:19:44 +00:00
|
|
|
|
|
|
|
get value() {
|
2022-08-29 21:51:44 +00:00
|
|
|
return wrapDistribution(this.valueMethod(RSValue.getDistribution));
|
2022-08-28 16:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SqLambdaValue extends SqAbstractValue {
|
2022-08-28 17:33:16 +00:00
|
|
|
tag = Tag.Lambda as const;
|
2022-08-28 16:19:44 +00:00
|
|
|
|
|
|
|
get value() {
|
2022-08-29 21:51:44 +00:00
|
|
|
return new SqLambda(this.valueMethod(RSValue.getLambda), this.location);
|
2022-08-28 16:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SqModuleValue extends SqAbstractValue {
|
2022-08-28 17:33:16 +00:00
|
|
|
tag = Tag.Module as const;
|
2022-08-28 16:19:44 +00:00
|
|
|
|
|
|
|
get value() {
|
2022-08-29 21:51:44 +00:00
|
|
|
return new SqModule(this.valueMethod(RSValue.getModule), this.location);
|
2022-08-28 16:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SqNumberValue extends SqAbstractValue {
|
2022-08-28 17:33:16 +00:00
|
|
|
tag = Tag.Number as const;
|
2022-08-28 16:19:44 +00:00
|
|
|
|
|
|
|
get value() {
|
2022-08-29 21:51:44 +00:00
|
|
|
return this.valueMethod(RSValue.getNumber);
|
2022-08-28 16:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SqRecordValue extends SqAbstractValue {
|
2022-08-28 17:33:16 +00:00
|
|
|
tag = Tag.Record as const;
|
2022-08-28 16:19:44 +00:00
|
|
|
|
|
|
|
get value() {
|
2022-08-29 21:51:44 +00:00
|
|
|
return new SqRecord(this.valueMethod(RSValue.getRecord), this.location);
|
2022-08-28 16:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SqStringValue extends SqAbstractValue {
|
2022-08-28 17:33:16 +00:00
|
|
|
tag = Tag.String as const;
|
2022-08-28 16:19:44 +00:00
|
|
|
|
|
|
|
get value(): string {
|
2022-08-29 21:51:44 +00:00
|
|
|
return this.valueMethod(RSValue.getString);
|
2022-08-28 16:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SqSymbolValue extends SqAbstractValue {
|
2022-08-28 17:33:16 +00:00
|
|
|
tag = Tag.Symbol as const;
|
2022-08-28 16:19:44 +00:00
|
|
|
|
|
|
|
get value(): string {
|
2022-08-29 21:51:44 +00:00
|
|
|
return this.valueMethod(RSValue.getSymbol);
|
2022-08-28 16:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SqTimeDurationValue extends SqAbstractValue {
|
2022-08-28 17:33:16 +00:00
|
|
|
tag = Tag.TimeDuration as const;
|
2022-08-28 16:19:44 +00:00
|
|
|
|
|
|
|
get value() {
|
2022-08-29 21:51:44 +00:00
|
|
|
return this.valueMethod(RSValue.getTimeDuration);
|
2022-08-28 16:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SqTypeValue extends SqAbstractValue {
|
2022-08-28 17:33:16 +00:00
|
|
|
tag = Tag.Type as const;
|
2022-08-28 16:19:44 +00:00
|
|
|
|
|
|
|
get value() {
|
2022-08-29 21:51:44 +00:00
|
|
|
return new SqType(this.valueMethod(RSValue.getType));
|
2022-08-28 16:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SqTypeIdentifierValue extends SqAbstractValue {
|
2022-08-28 17:33:16 +00:00
|
|
|
tag = Tag.TypeIdentifier as const;
|
2022-08-28 16:19:44 +00:00
|
|
|
|
|
|
|
get value() {
|
2022-08-29 21:51:44 +00:00
|
|
|
return this.valueMethod(RSValue.getTypeIdentifier);
|
2022-08-28 16:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SqVoidValue extends SqAbstractValue {
|
2022-08-28 17:33:16 +00:00
|
|
|
tag = Tag.Void as const;
|
2022-08-29 02:50:51 +00:00
|
|
|
|
|
|
|
get value() {
|
|
|
|
return null;
|
|
|
|
}
|
2022-08-28 16:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const tagToClass = {
|
2022-08-28 17:33:16 +00:00
|
|
|
[Tag.Array]: SqArrayValue,
|
|
|
|
[Tag.ArrayString]: SqArrayStringValue,
|
|
|
|
[Tag.Bool]: SqBoolValue,
|
|
|
|
[Tag.Call]: SqCallValue,
|
|
|
|
[Tag.Date]: SqDateValue,
|
|
|
|
[Tag.Declaration]: SqDeclarationValue,
|
|
|
|
[Tag.Distribution]: SqDistributionValue,
|
|
|
|
[Tag.Lambda]: SqLambdaValue,
|
|
|
|
[Tag.Module]: SqModuleValue,
|
|
|
|
[Tag.Number]: SqNumberValue,
|
|
|
|
[Tag.Record]: SqRecordValue,
|
|
|
|
[Tag.String]: SqStringValue,
|
|
|
|
[Tag.Symbol]: SqSymbolValue,
|
|
|
|
[Tag.TimeDuration]: SqTimeDurationValue,
|
|
|
|
[Tag.Type]: SqTypeValue,
|
|
|
|
[Tag.TypeIdentifier]: SqTypeIdentifierValue,
|
|
|
|
[Tag.Void]: SqVoidValue,
|
2022-08-28 16:19:44 +00:00
|
|
|
} as const;
|
|
|
|
|
|
|
|
// FIXME
|
|
|
|
// type SqValue = typeof tagToClass[keyof typeof tagToClass];
|
|
|
|
export type SqValue =
|
|
|
|
| SqArrayValue
|
|
|
|
| SqArrayStringValue
|
|
|
|
| SqBoolValue
|
|
|
|
| SqCallValue
|
|
|
|
| SqDateValue
|
|
|
|
| SqDeclarationValue
|
|
|
|
| SqDistributionValue
|
|
|
|
| SqLambdaValue
|
|
|
|
| SqModuleValue
|
|
|
|
| SqNumberValue
|
|
|
|
| SqRecordValue
|
|
|
|
| SqStringValue
|
|
|
|
| SqSymbolValue
|
|
|
|
| SqTimeDurationValue
|
|
|
|
| SqTypeValue
|
|
|
|
| SqTypeIdentifierValue
|
|
|
|
| SqVoidValue;
|