2022-04-29 20:10:39 +00:00
|
|
|
import * as _ from "lodash";
|
|
|
|
import {
|
2022-05-10 15:52:13 +00:00
|
|
|
expressionValue,
|
2022-04-29 20:10:39 +00:00
|
|
|
mixedShape,
|
|
|
|
sampleSetDist,
|
|
|
|
genericDist,
|
2022-04-29 22:51:00 +00:00
|
|
|
environment,
|
2022-04-29 20:10:39 +00:00
|
|
|
symbolicDist,
|
|
|
|
discreteShape,
|
|
|
|
continuousShape,
|
2022-05-02 14:53:16 +00:00
|
|
|
lambdaValue,
|
2022-05-24 21:23:37 +00:00
|
|
|
lambdaDeclaration
|
2022-04-29 20:10:39 +00:00
|
|
|
} from "../rescript/TypescriptInterface.gen";
|
|
|
|
import { Distribution } from "./distribution";
|
|
|
|
import { tagged, tag } from "./types";
|
|
|
|
// This file is here to compensate for genType not fully recursively converting types
|
|
|
|
|
|
|
|
// Raw rescript types.
|
|
|
|
export type rescriptExport =
|
|
|
|
| {
|
|
|
|
TAG: 0; // EvArray
|
|
|
|
_0: rescriptExport[];
|
|
|
|
}
|
|
|
|
| {
|
2022-04-29 22:51:00 +00:00
|
|
|
TAG: 1; // EvString
|
|
|
|
_0: string[];
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
TAG: 2; // EvBool
|
2022-04-29 20:10:39 +00:00
|
|
|
_0: boolean;
|
|
|
|
}
|
|
|
|
| {
|
2022-04-29 22:51:00 +00:00
|
|
|
TAG: 3; // EvCall
|
2022-04-29 20:10:39 +00:00
|
|
|
_0: string;
|
|
|
|
}
|
|
|
|
| {
|
2022-04-29 22:51:00 +00:00
|
|
|
TAG: 4; // EvDistribution
|
2022-04-29 20:10:39 +00:00
|
|
|
_0: rescriptDist;
|
|
|
|
}
|
|
|
|
| {
|
2022-04-29 22:51:00 +00:00
|
|
|
TAG: 5; // EvLambda
|
2022-05-02 14:53:16 +00:00
|
|
|
_0: lambdaValue;
|
2022-04-29 22:51:00 +00:00
|
|
|
}
|
|
|
|
| {
|
|
|
|
TAG: 6; // EvNumber
|
2022-04-29 20:10:39 +00:00
|
|
|
_0: number;
|
|
|
|
}
|
|
|
|
| {
|
2022-04-29 22:51:00 +00:00
|
|
|
TAG: 7; // EvRecord
|
2022-04-29 20:10:39 +00:00
|
|
|
_0: { [key: string]: rescriptExport };
|
|
|
|
}
|
|
|
|
| {
|
2022-04-29 22:51:00 +00:00
|
|
|
TAG: 8; // EvString
|
2022-04-29 20:10:39 +00:00
|
|
|
_0: string;
|
|
|
|
}
|
|
|
|
| {
|
2022-04-29 22:51:00 +00:00
|
|
|
TAG: 9; // EvSymbol
|
2022-04-29 20:10:39 +00:00
|
|
|
_0: string;
|
2022-05-22 22:37:07 +00:00
|
|
|
}
|
|
|
|
| {
|
|
|
|
TAG: 10; // EvDate
|
|
|
|
_0: Date;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
TAG: 11; // EvTimeDuration
|
|
|
|
_0: number;
|
2022-05-24 21:23:37 +00:00
|
|
|
}
|
|
|
|
| {
|
|
|
|
TAG: 12; // EvDeclaration
|
|
|
|
_0: lambdaDeclaration;
|
2022-04-29 20:10:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type rescriptDist =
|
|
|
|
| { TAG: 0; _0: rescriptPointSetDist }
|
|
|
|
| { TAG: 1; _0: sampleSetDist }
|
|
|
|
| { TAG: 2; _0: symbolicDist };
|
|
|
|
|
|
|
|
type rescriptPointSetDist =
|
|
|
|
| {
|
|
|
|
TAG: 0; // Mixed
|
|
|
|
_0: mixedShape;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
TAG: 1; // Discrete
|
|
|
|
_0: discreteShape;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
TAG: 2; // ContinuousShape
|
|
|
|
_0: continuousShape;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type squiggleExpression =
|
|
|
|
| tagged<"symbol", string>
|
|
|
|
| tagged<"string", string>
|
|
|
|
| tagged<"call", string>
|
2022-05-02 14:53:16 +00:00
|
|
|
| tagged<"lambda", lambdaValue>
|
2022-04-29 20:10:39 +00:00
|
|
|
| tagged<"array", squiggleExpression[]>
|
2022-04-29 22:51:00 +00:00
|
|
|
| tagged<"arraystring", string[]>
|
2022-04-29 20:10:39 +00:00
|
|
|
| tagged<"boolean", boolean>
|
|
|
|
| tagged<"distribution", Distribution>
|
|
|
|
| tagged<"number", number>
|
2022-05-22 22:37:07 +00:00
|
|
|
| tagged<"date", Date>
|
|
|
|
| tagged<"timeDuration", number>
|
2022-05-24 21:23:37 +00:00
|
|
|
| tagged<"lambdaDeclaration", lambdaDeclaration>
|
2022-05-24 18:12:49 +00:00
|
|
|
| tagged<"record", { [key: string]: squiggleExpression }>;
|
2022-04-29 20:10:39 +00:00
|
|
|
|
2022-05-10 15:52:13 +00:00
|
|
|
export { lambdaValue };
|
|
|
|
|
2022-04-29 20:10:39 +00:00
|
|
|
export function convertRawToTypescript(
|
|
|
|
result: rescriptExport,
|
2022-04-29 22:51:00 +00:00
|
|
|
environment: environment
|
2022-04-29 20:10:39 +00:00
|
|
|
): squiggleExpression {
|
|
|
|
switch (result.TAG) {
|
|
|
|
case 0: // EvArray
|
|
|
|
return tag(
|
|
|
|
"array",
|
2022-04-29 22:51:00 +00:00
|
|
|
result._0.map((x) => convertRawToTypescript(x, environment))
|
2022-04-29 20:10:39 +00:00
|
|
|
);
|
2022-04-29 22:51:00 +00:00
|
|
|
case 1: // EvArrayString
|
|
|
|
return tag("arraystring", result._0);
|
|
|
|
case 2: // EvBool
|
2022-04-29 20:10:39 +00:00
|
|
|
return tag("boolean", result._0);
|
2022-04-29 22:51:00 +00:00
|
|
|
case 3: // EvCall
|
2022-04-29 20:10:39 +00:00
|
|
|
return tag("call", result._0);
|
2022-04-29 22:51:00 +00:00
|
|
|
case 4: // EvDistribution
|
2022-04-29 20:10:39 +00:00
|
|
|
return tag(
|
|
|
|
"distribution",
|
|
|
|
new Distribution(
|
|
|
|
convertRawDistributionToGenericDist(result._0),
|
2022-04-29 22:51:00 +00:00
|
|
|
environment
|
2022-04-29 20:10:39 +00:00
|
|
|
)
|
|
|
|
);
|
2022-04-29 22:51:00 +00:00
|
|
|
case 5: // EvDistribution
|
|
|
|
return tag("lambda", result._0);
|
|
|
|
case 6: // EvNumber
|
2022-04-29 20:10:39 +00:00
|
|
|
return tag("number", result._0);
|
2022-04-29 22:51:00 +00:00
|
|
|
case 7: // EvRecord
|
2022-04-29 20:10:39 +00:00
|
|
|
return tag(
|
|
|
|
"record",
|
2022-04-29 22:51:00 +00:00
|
|
|
_.mapValues(result._0, (x) => convertRawToTypescript(x, environment))
|
2022-04-29 20:10:39 +00:00
|
|
|
);
|
2022-04-29 22:51:00 +00:00
|
|
|
case 8: // EvString
|
2022-04-29 20:10:39 +00:00
|
|
|
return tag("string", result._0);
|
2022-04-29 22:51:00 +00:00
|
|
|
case 9: // EvSymbol
|
2022-04-29 20:10:39 +00:00
|
|
|
return tag("symbol", result._0);
|
2022-05-22 22:37:07 +00:00
|
|
|
case 10: // EvDate
|
|
|
|
return tag("date", result._0);
|
|
|
|
case 11: // EvTimeDuration
|
|
|
|
return tag("number", result._0);
|
2022-05-24 21:23:37 +00:00
|
|
|
case 12: // EvDeclaration
|
|
|
|
return tag("lambdaDeclaration", result._0);
|
2022-04-29 20:10:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertRawDistributionToGenericDist(
|
|
|
|
result: rescriptDist
|
|
|
|
): genericDist {
|
|
|
|
switch (result.TAG) {
|
|
|
|
case 0: // Point Set Dist
|
|
|
|
switch (result._0.TAG) {
|
|
|
|
case 0: // Mixed
|
|
|
|
return tag("PointSet", tag("Mixed", result._0._0));
|
|
|
|
case 1: // Discrete
|
|
|
|
return tag("PointSet", tag("Discrete", result._0._0));
|
|
|
|
case 2: // Continuous
|
|
|
|
return tag("PointSet", tag("Continuous", result._0._0));
|
|
|
|
}
|
|
|
|
case 1: // Sample Set Dist
|
|
|
|
return tag("SampleSet", result._0);
|
|
|
|
case 2: // Symbolic Dist
|
|
|
|
return tag("Symbolic", result._0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type jsValue =
|
|
|
|
| string
|
|
|
|
| number
|
|
|
|
| jsValue[]
|
|
|
|
| { [key: string]: jsValue }
|
|
|
|
| boolean;
|
|
|
|
|
|
|
|
export function jsValueToBinding(value: jsValue): rescriptExport {
|
|
|
|
if (typeof value === "boolean") {
|
2022-04-29 22:51:00 +00:00
|
|
|
return { TAG: 2, _0: value as boolean };
|
2022-04-29 20:10:39 +00:00
|
|
|
} else if (typeof value === "string") {
|
2022-04-29 22:51:00 +00:00
|
|
|
return { TAG: 8, _0: value as string };
|
2022-04-29 20:10:39 +00:00
|
|
|
} else if (typeof value === "number") {
|
2022-04-29 22:51:00 +00:00
|
|
|
return { TAG: 6, _0: value as number };
|
2022-04-29 20:10:39 +00:00
|
|
|
} else if (Array.isArray(value)) {
|
|
|
|
return { TAG: 0, _0: value.map(jsValueToBinding) };
|
|
|
|
} else {
|
|
|
|
// Record
|
2022-04-29 22:51:00 +00:00
|
|
|
return { TAG: 7, _0: _.mapValues(value, jsValueToBinding) };
|
2022-04-29 20:10:39 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-10 15:52:13 +00:00
|
|
|
|
|
|
|
export function jsValueToExpressionValue(value: jsValue): expressionValue {
|
|
|
|
if (typeof value === "boolean") {
|
|
|
|
return { tag: "EvBool", value: value as boolean };
|
|
|
|
} else if (typeof value === "string") {
|
|
|
|
return { tag: "EvString", value: value as string };
|
|
|
|
} else if (typeof value === "number") {
|
|
|
|
return { tag: "EvNumber", value: value as number };
|
|
|
|
} else if (Array.isArray(value)) {
|
|
|
|
return { tag: "EvArray", value: value.map(jsValueToExpressionValue) };
|
|
|
|
} else {
|
|
|
|
// Record
|
|
|
|
return {
|
|
|
|
tag: "EvRecord",
|
|
|
|
value: _.mapValues(value, jsValueToExpressionValue),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|