Merge pull request #917 from quantified-uncertainty/issue-916

Reducer lambda context, hiddenNameSpace
This commit is contained in:
Vyacheslav Matyukhin 2022-07-31 00:35:13 +04:00 committed by GitHub
commit 37aa63438f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 14 deletions

View File

@ -7,6 +7,9 @@ module ErrorValue = Reducer_ErrorValue
@genType.opaque @genType.opaque
type internalCode = Object type internalCode = Object
@genType.opaque
type hiddenNameSpace = Object
@genType @genType
type rec externalExpressionValue = type rec externalExpressionValue =
| EvArray(array<externalExpressionValue>) | EvArray(array<externalExpressionValue>)
@ -27,14 +30,16 @@ type rec externalExpressionValue =
| EvType(record) | EvType(record)
| EvVoid | EvVoid
and record = Js.Dict.t<externalExpressionValue> and record = Js.Dict.t<externalExpressionValue>
and externalBindings = record
and lambdaValue = { and lambdaValue = {
parameters: array<string>, parameters: array<string>,
context: externalBindings, context: hiddenNameSpace,
body: internalCode, body: internalCode,
} }
and lambdaDeclaration = Declaration.declaration<lambdaValue> and lambdaDeclaration = Declaration.declaration<lambdaValue>
@genType
type externalBindings = record
@genType @genType
type t = externalExpressionValue type t = externalExpressionValue

View File

@ -37,6 +37,29 @@ type internalExpressionValue = t
type functionCall = (string, array<t>) type functionCall = (string, array<t>)
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 => let rec toString = aValue =>
switch aValue { switch aValue {
| IEvArray(anArray) => { | IEvArray(anArray) => {
@ -244,12 +267,7 @@ let rec toExternal = (iev: t): ExternalExpressionValue.t => {
} }
and mapToExternal = v => and mapToExternal = v =>
v->Belt.Map.String.map(e => toExternal(e))->Belt.Map.String.toArray->Js.Dict.fromArray v->Belt.Map.String.map(e => toExternal(e))->Belt.Map.String.toArray->Js.Dict.fromArray
and lambdaValueToExternal = v => { and lambdaValueToExternal = Internal.Lambda.toExternal
let p = v.parameters
let c = v.context->nameSpaceToTypeScriptBindings
let b = v.body
{parameters: p, context: c, body: b}
}
and nameSpaceToTypeScriptBindings = ( and nameSpaceToTypeScriptBindings = (
nameSpace: nameSpace, nameSpace: nameSpace,
): ReducerInterface_ExternalExpressionValue.externalBindings => { ): ReducerInterface_ExternalExpressionValue.externalBindings => {
@ -284,12 +302,7 @@ let rec toInternal = (ev: ExternalExpressionValue.t): t => {
} }
and recordToInternal = v => and recordToInternal = v =>
v->Js.Dict.entries->Belt.Map.String.fromArray->Belt.Map.String.map(e => toInternal(e)) v->Js.Dict.entries->Belt.Map.String.fromArray->Belt.Map.String.map(e => toInternal(e))
and lambdaValueToInternal = v => { and lambdaValueToInternal = Internal.Lambda.toInternal
let p = v.parameters
let c = v.context->nameSpaceFromTypeScriptBindings
let b = v.body
{parameters: p, context: c, body: b}
}
and nameSpaceFromTypeScriptBindings = ( and nameSpaceFromTypeScriptBindings = (
r: ReducerInterface_ExternalExpressionValue.externalBindings, r: ReducerInterface_ExternalExpressionValue.externalBindings,
): nameSpace => ): nameSpace =>