squiggle/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res

310 lines
9.9 KiB
Plaintext
Raw Normal View History

module ErrorValue = Reducer_ErrorValue
2022-06-24 10:15:38 +00:00
module ExternalExpressionValue = ReducerInterface_ExternalExpressionValue
module Extra_Array = Reducer_Extra_Array
type internalCode = ExternalExpressionValue.internalCode
type environment = ExternalExpressionValue.environment
let defaultEnvironment = ExternalExpressionValue.defaultEnvironment
2022-06-24 10:15:38 +00:00
type rec t =
2022-06-27 00:40:31 +00:00
| IEvArray(array<t>) // FIXME: Convert to MapInt
| IEvArrayString(array<string>)
2022-06-23 18:38:07 +00:00
| IEvBool(bool)
| IEvCall(string) // External function call
| IEvDate(Js.Date.t)
| IEvDeclaration(lambdaDeclaration)
| IEvDistribution(DistributionTypes.genericDist)
| IEvLambda(lambdaValue)
| IEvBindings(nameSpace)
2022-06-23 18:38:07 +00:00
| IEvNumber(float)
| IEvRecord(map)
| IEvString(string)
| IEvSymbol(string)
| IEvTimeDuration(float)
2022-06-27 00:40:31 +00:00
| IEvType(map)
2022-06-23 18:38:07 +00:00
| IEvTypeIdentifier(string)
| IEvVoid
2022-06-24 10:15:38 +00:00
and map = Belt.Map.String.t<t>
and nameSpace = NameSpace(Belt.Map.String.t<t>)
and lambdaValue = {
parameters: array<string>,
context: nameSpace,
body: internalCode,
}
and lambdaDeclaration = Declaration.declaration<lambdaValue>
2022-06-24 10:15:38 +00:00
type internalExpressionValue = t
2022-06-24 10:15:38 +00:00
type functionCall = (string, array<t>)
2022-07-30 14:58:45 +00:00
module Internal = {
module NameSpace = {
external castNameSpaceToHidden: nameSpace => ExternalExpressionValue.hiddenNameSpace =
"%identity"
external castHiddenToNameSpace: ExternalExpressionValue.hiddenNameSpace => nameSpace =
"%identity"
}
module Lambda = {
let toInternal = (v: ExternalExpressionValue.lambdaValue): lambdaValue => {
let p = v.parameters
let c = v.context->NameSpace.castHiddenToNameSpace
let b = v.body
{parameters: p, context: c, body: b}
}
and toExternal = (v: lambdaValue): ExternalExpressionValue.lambdaValue => {
let p = v.parameters
let c = v.context->NameSpace.castNameSpaceToHidden
let b = v.body
{parameters: p, context: c, body: b}
}
}
}
let rec toString = aValue =>
switch aValue {
2022-06-23 18:38:07 +00:00
| IEvArray(anArray) => {
let args = anArray->Js.Array2.map(each => toString(each))->Js.Array2.toString
`[${args}]`
}
2022-06-23 18:38:07 +00:00
| IEvArrayString(anArray) => {
let args = anArray->Js.Array2.toString
`[${args}]`
}
2022-06-23 18:38:07 +00:00
| IEvBool(aBool) => Js.String.make(aBool)
| IEvCall(fName) => `:${fName}`
| IEvDate(date) => DateTime.Date.toString(date)
| IEvDeclaration(d) => Declaration.toString(d, r => toString(IEvLambda(r)))
| IEvDistribution(dist) => GenericDist.toString(dist)
| IEvLambda(lambdaValue) => `lambda(${Js.Array2.toString(lambdaValue.parameters)}=>internal code)`
| IEvBindings(m) => `@${m->toStringNameSpace}`
2022-06-23 18:38:07 +00:00
| IEvNumber(aNumber) => Js.String.make(aNumber)
| IEvRecord(aMap) => aMap->toStringMap
| IEvString(aString) => `'${aString}'`
| IEvSymbol(aString) => `:${aString}`
2022-06-27 00:40:31 +00:00
| IEvType(aMap) => aMap->toStringMap
2022-06-23 18:38:07 +00:00
| IEvTimeDuration(t) => DateTime.Duration.toString(t)
| IEvTypeIdentifier(id) => `#${id}`
| IEvVoid => `()`
}
and toStringMap = aMap => {
let pairs =
aMap
->Belt.Map.String.toArray
->Js.Array2.map(((eachKey, eachValue)) => `${eachKey}: ${toString(eachValue)}`)
->Js.Array2.toString
`{${pairs}}`
}
and toStringNameSpace = nameSpace => {
let NameSpace(container) = nameSpace
container->toStringMap
}
let toStringWithType = aValue =>
switch aValue {
2022-06-23 18:38:07 +00:00
| IEvArray(_) => `Array::${toString(aValue)}`
| IEvArrayString(_) => `ArrayString::${toString(aValue)}`
| IEvBool(_) => `Bool::${toString(aValue)}`
| IEvCall(_) => `Call::${toString(aValue)}`
2022-06-27 00:40:31 +00:00
| IEvDate(_) => `Date::${toString(aValue)}`
| IEvDeclaration(_) => `Declaration::${toString(aValue)}`
2022-06-23 18:38:07 +00:00
| IEvDistribution(_) => `Distribution::${toString(aValue)}`
| IEvLambda(_) => `Lambda::${toString(aValue)}`
2022-07-18 16:16:49 +00:00
| IEvBindings(_) => `Bindings::${toString(aValue)}`
2022-06-23 18:38:07 +00:00
| IEvNumber(_) => `Number::${toString(aValue)}`
| IEvRecord(_) => `Record::${toString(aValue)}`
| IEvString(_) => `String::${toString(aValue)}`
| IEvSymbol(_) => `Symbol::${toString(aValue)}`
| IEvTimeDuration(_) => `Date::${toString(aValue)}`
2022-06-27 00:40:31 +00:00
| IEvType(_) => `Type::${toString(aValue)}`
2022-06-23 18:38:07 +00:00
| IEvTypeIdentifier(_) => `TypeIdentifier::${toString(aValue)}`
| IEvVoid => `Void`
}
2022-06-24 10:15:38 +00:00
let argsToString = (args: array<t>): string => {
args->Js.Array2.map(arg => arg->toString)->Js.Array2.toString
}
let toStringFunctionCall = ((fn, args)): string => `${fn}(${argsToString(args)})`
let toStringResult = x =>
switch x {
| Ok(a) => `Ok(${toString(a)})`
| Error(m) => `Error(${ErrorValue.errorToString(m)})`
}
2022-06-24 10:15:38 +00:00
let toStringResultOkless = (codeResult: result<t, ErrorValue.errorValue>): string =>
switch codeResult {
| Ok(a) => toString(a)
| Error(m) => `Error(${ErrorValue.errorToString(m)})`
}
let toStringResultRecord = x =>
switch x {
| Ok(a) => `Ok(${ExternalExpressionValue.toStringRecord(a)})`
| Error(m) => `Error(${ErrorValue.errorToString(m)})`
}
2022-06-24 10:15:38 +00:00
type internalExpressionValueType =
| EvtArray
| EvtArrayString
| EvtBool
| EvtCall
2022-06-27 00:40:31 +00:00
| EvtDate
| EvtDeclaration
| EvtDistribution
| EvtLambda
2022-06-27 00:40:31 +00:00
| EvtModule
| EvtNumber
| EvtRecord
| EvtString
| EvtSymbol
| EvtTimeDuration
2022-06-27 00:40:31 +00:00
| EvtType
| EvtTypeIdentifier
| EvtVoid
2022-06-24 10:15:38 +00:00
type functionCallSignature = CallSignature(string, array<internalExpressionValueType>)
type functionDefinitionSignature =
2022-06-24 10:15:38 +00:00
FunctionDefinitionSignature(functionCallSignature, internalExpressionValueType)
let valueToValueType = value =>
switch value {
2022-06-23 18:38:07 +00:00
| IEvArray(_) => EvtArray
| IEvArrayString(_) => EvtArrayString
| IEvBool(_) => EvtBool
| IEvCall(_) => EvtCall
2022-06-27 00:40:31 +00:00
| IEvDate(_) => EvtDate
| IEvDeclaration(_) => EvtDeclaration
2022-06-23 18:38:07 +00:00
| IEvDistribution(_) => EvtDistribution
| IEvLambda(_) => EvtLambda
| IEvBindings(_) => EvtModule
2022-06-23 18:38:07 +00:00
| IEvNumber(_) => EvtNumber
| IEvRecord(_) => EvtRecord
| IEvString(_) => EvtString
| IEvSymbol(_) => EvtSymbol
| IEvTimeDuration(_) => EvtTimeDuration
2022-06-27 00:40:31 +00:00
| IEvType(_) => EvtType
2022-06-23 18:38:07 +00:00
| IEvTypeIdentifier(_) => EvtTypeIdentifier
| IEvVoid => EvtVoid
}
let externalValueToValueType = (value: ExternalExpressionValue.t) =>
switch value {
| EvArray(_) => EvtArray
| EvArrayString(_) => EvtArrayString
| EvBool(_) => EvtBool
| EvCall(_) => EvtCall
| EvDate(_) => EvtDate
| EvDeclaration(_) => EvtDeclaration
| EvDistribution(_) => EvtDistribution
| EvLambda(_) => EvtLambda
| EvModule(_) => EvtModule
| EvNumber(_) => EvtNumber
| EvRecord(_) => EvtRecord
| EvString(_) => EvtString
| EvSymbol(_) => EvtSymbol
| EvTimeDuration(_) => EvtTimeDuration
| EvType(_) => EvtType
| EvTypeIdentifier(_) => EvtTypeIdentifier
2022-07-22 15:49:53 +00:00
| EvVoid => EvtVoid
}
let functionCallToCallSignature = (functionCall: functionCall): functionCallSignature => {
let (fn, args) = functionCall
CallSignature(fn, args->Js.Array2.map(valueToValueType))
}
2022-06-24 10:15:38 +00:00
let valueTypeToString = (valueType: internalExpressionValueType): string =>
switch valueType {
| EvtArray => `Array`
| EvtArrayString => `ArrayString`
| EvtBool => `Bool`
| EvtCall => `Call`
2022-06-27 00:40:31 +00:00
| EvtDate => `Date`
| EvtDeclaration => `Declaration`
| EvtDistribution => `Distribution`
| EvtLambda => `Lambda`
2022-06-27 00:40:31 +00:00
| EvtModule => `Module`
| EvtNumber => `Number`
| EvtRecord => `Record`
| EvtString => `String`
| EvtSymbol => `Symbol`
| EvtTimeDuration => `Duration`
2022-06-27 00:40:31 +00:00
| EvtType => `Type`
| EvtTypeIdentifier => `TypeIdentifier`
| EvtVoid => `Void`
}
let functionCallSignatureToString = (functionCallSignature: functionCallSignature): string => {
let CallSignature(fn, args) = functionCallSignature
`${fn}(${args->Js.Array2.map(valueTypeToString)->Js.Array2.toString})`
}
2022-06-24 10:15:38 +00:00
let rec toExternal = (iev: t): ExternalExpressionValue.t => {
switch iev {
2022-06-23 18:38:07 +00:00
| IEvArray(v) => v->Belt.Array.map(e => toExternal(e))->EvArray
| IEvArrayString(v) => EvArrayString(v)
| IEvBool(v) => EvBool(v)
| IEvCall(v) => EvCall(v)
2022-06-27 00:40:31 +00:00
| IEvDeclaration(v) => {
let fn = lambdaValueToExternal(v.fn)
let args = v.args
EvDeclaration({fn: fn, args: args})
}
2022-06-23 18:38:07 +00:00
| IEvDistribution(v) => EvDistribution(v)
| IEvLambda(v) => EvLambda(lambdaValueToExternal(v))
| IEvNumber(v) => EvNumber(v)
| IEvRecord(v) => v->mapToExternal->EvRecord
| IEvString(v) => EvString(v)
| IEvSymbol(v) => EvSymbol(v)
| IEvDate(v) => EvDate(v)
| IEvTimeDuration(v) => EvTimeDuration(v)
2022-06-27 00:40:31 +00:00
| IEvType(v) => v->mapToExternal->EvType
2022-06-23 18:38:07 +00:00
| IEvTypeIdentifier(v) => EvTypeIdentifier(v)
| IEvBindings(v) => v->nameSpaceToTypeScriptBindings->EvModule
| IEvVoid => EvVoid
}
}
and mapToExternal = v =>
v->Belt.Map.String.map(e => toExternal(e))->Belt.Map.String.toArray->Js.Dict.fromArray
2022-07-30 14:58:45 +00:00
and lambdaValueToExternal = Internal.Lambda.toExternal
and nameSpaceToTypeScriptBindings = (
nameSpace: nameSpace,
2022-06-24 10:15:38 +00:00
): ReducerInterface_ExternalExpressionValue.externalBindings => {
let NameSpace(container) = nameSpace
Belt.Map.String.map(container, e => toExternal(e))->Belt.Map.String.toArray->Js.Dict.fromArray
}
2022-06-24 10:15:38 +00:00
let rec toInternal = (ev: ExternalExpressionValue.t): t => {
switch ev {
2022-06-23 18:38:07 +00:00
| EvArray(v) => v->Belt.Array.map(e => toInternal(e))->IEvArray
| EvArrayString(v) => IEvArrayString(v)
| EvBool(v) => IEvBool(v)
| EvCall(v) => IEvCall(v)
2022-06-27 00:40:31 +00:00
| EvDate(v) => IEvDate(v)
| EvDeclaration(v) => {
let fn = lambdaValueToInternal(v.fn)
let args = v.args
IEvDeclaration({fn: fn, args: args})
}
2022-06-23 18:38:07 +00:00
| EvDistribution(v) => IEvDistribution(v)
| EvLambda(v) => IEvLambda(lambdaValueToInternal(v))
| EvModule(v) => v->nameSpaceFromTypeScriptBindings->IEvBindings
2022-06-23 18:38:07 +00:00
| EvNumber(v) => IEvNumber(v)
| EvRecord(v) => v->recordToInternal->IEvRecord
| EvString(v) => IEvString(v)
| EvSymbol(v) => IEvSymbol(v)
| EvTimeDuration(v) => IEvTimeDuration(v)
2022-06-27 00:40:31 +00:00
| EvType(v) => v->recordToInternal->IEvType
2022-06-23 18:38:07 +00:00
| EvTypeIdentifier(v) => IEvTypeIdentifier(v)
| EvVoid => IEvVoid
}
}
and recordToInternal = v =>
v->Js.Dict.entries->Belt.Map.String.fromArray->Belt.Map.String.map(e => toInternal(e))
2022-07-30 14:58:45 +00:00
and lambdaValueToInternal = Internal.Lambda.toInternal
and nameSpaceFromTypeScriptBindings = (
2022-06-24 10:15:38 +00:00
r: ReducerInterface_ExternalExpressionValue.externalBindings,
): nameSpace =>
r->Js.Dict.entries->Belt.Map.String.fromArray->Belt.Map.String.map(e => toInternal(e))->NameSpace