demolish ExternalValue

This commit is contained in:
Umur Ozkul 2022-08-19 20:56:45 +02:00
parent decc12637f
commit f573c326d2
8 changed files with 31 additions and 132 deletions

View File

@ -15,8 +15,4 @@ let removeDefaultsInternal = (iev: InternalExpressionValue.t) => {
} }
} }
let removeDefaultsExternal = (ev: ExternalExpressionValue.t): ExternalExpressionValue.t =>
ev->InternalExpressionValue.toInternal->removeDefaultsInternal->InternalExpressionValue.toExternal
let rRemoveDefaultsInternal = r => Belt.Result.map(r, removeDefaultsInternal) let rRemoveDefaultsInternal = r => Belt.Result.map(r, removeDefaultsInternal)
let rRemoveDefaultsExternal = r => Belt.Result.map(r, removeDefaultsExternal)

View File

@ -1,7 +1,7 @@
module ErrorValue = Reducer_ErrorValue module ErrorValue = Reducer_ErrorValue
module Expression = Reducer_Expression module Expression = Reducer_Expression
module ExpressionT = Reducer_Expression_T module ExpressionT = Reducer_Expression_T
module ExternalExpressionValue = ReducerInterface.ExternalExpressionValue module InternalExpressionValue = ReducerInterface.InternalExpressionValue
open Jest open Jest
open Expect open Expect
@ -9,8 +9,8 @@ open Expect
let unwrapRecord = rValue => let unwrapRecord = rValue =>
rValue->Belt.Result.flatMap(value => rValue->Belt.Result.flatMap(value =>
switch value { switch value {
| ExternalExpressionValue.EvRecord(aRecord) => Ok(aRecord) | InternalExpressionValue.IEvRecord(aRecord) => Ok(aRecord)
| _ => ErrorValue.RETodo("TODO: External bindings must be returned")->Error | _ => ErrorValue.RETodo("TODO: Internal bindings must be returned")->Error
} }
) )
@ -18,15 +18,15 @@ let expectParseToBe = (code: string, answer: string) =>
Expression.BackCompatible.parse(code)->ExpressionT.toStringResult->expect->toBe(answer) Expression.BackCompatible.parse(code)->ExpressionT.toStringResult->expect->toBe(answer)
let expectEvalToBe = (code: string, answer: string) => let expectEvalToBe = (code: string, answer: string) =>
Expression.BackCompatible.evaluateStringAsExternal(code) Expression.BackCompatible.evaluateString(code)
->Reducer_Helpers.rRemoveDefaultsExternal ->Reducer_Helpers.rRemoveDefaultsInternal
->ExternalExpressionValue.toStringResult ->InternalExpressionValue.toStringResult
->expect ->expect
->toBe(answer) ->toBe(answer)
let expectEvalError = (code: string) => let expectEvalError = (code: string) =>
Expression.BackCompatible.evaluateStringAsExternal(code) Expression.BackCompatible.evaluateString(code)
->ExternalExpressionValue.toStringResult ->InternalExpressionValue.toStringResult
->expect ->expect
->toMatch("Error\(") ->toMatch("Error\(")

View File

@ -3,8 +3,8 @@ open Expect
open Reducer_TestHelpers open Reducer_TestHelpers
let expectEvalToBeOk = (code: string) => let expectEvalToBeOk = (code: string) =>
Reducer_Expression.BackCompatible.evaluateStringAsExternal(code) Reducer_Expression.BackCompatible.evaluateString(code)
->Reducer_Helpers.rRemoveDefaultsExternal ->Reducer_Helpers.rRemoveDefaultsInternal
->E.R.isOk ->E.R.isOk
->expect ->expect
->toBe(true) ->toBe(true)
@ -92,8 +92,8 @@ describe("FunctionRegistry Library", () => {
((fn, example)) => { ((fn, example)) => {
let responseType = let responseType =
example example
->Reducer_Expression.BackCompatible.evaluateStringAsExternal ->Reducer_Expression.BackCompatible.evaluateString
->E.R2.fmap(ReducerInterface_InternalExpressionValue.externalValueToValueType) ->E.R2.fmap(ReducerInterface_InternalExpressionValue.valueToValueType)
let expectedOutputType = fn.output |> E.O.toExn("") let expectedOutputType = fn.output |> E.O.toExn("")
expect(responseType)->toEqual(Ok(expectedOutputType)) expect(responseType)->toEqual(Ok(expectedOutputType))
}, },

View File

@ -2,4 +2,4 @@ open ForTS__Types
@genType @genType
let getKeyValuePairs = (v: squiggleValue_Module): array<(string, squiggleValue)> => let getKeyValuePairs = (v: squiggleValue_Module): array<(string, squiggleValue)> =>
ReducerInterface_InternalExpressionValue.nameSpaceToKeyValueArray(v) ReducerInterface_InternalExpressionValue.nameSpaceToKeyValuePairs(v)

View File

@ -76,8 +76,8 @@ let emptyModule: t = NameSpace(emptyMap)
let emptyBindings = emptyModule let emptyBindings = emptyModule
let emptyNameSpace = emptyModule let emptyNameSpace = emptyModule
let fromTypeScriptBindings = ReducerInterface_InternalExpressionValue.nameSpaceFromTypeScriptBindings // let fromTypeScriptBindings = ReducerInterface_InternalExpressionValue.nameSpaceFromTypeScriptBindings
let toTypeScriptBindings = ReducerInterface_InternalExpressionValue.nameSpaceToTypeScriptBindings // let toTypeScriptBindings = ReducerInterface_InternalExpressionValue.nameSpaceToTypeScriptBindings
let toExpressionValue = (nameSpace: t): internalExpressionValue => IEvBindings(nameSpace) let toExpressionValue = (nameSpace: t): internalExpressionValue => IEvBindings(nameSpace)
let fromExpressionValue = (aValue: internalExpressionValue): t => let fromExpressionValue = (aValue: internalExpressionValue): t =>

View File

@ -125,15 +125,4 @@ module BackCompatible = {
let evaluateString = (peggyCode: string): result<InternalExpressionValue.t, errorValue> => let evaluateString = (peggyCode: string): result<InternalExpressionValue.t, errorValue> =>
parse(peggyCode)->Result.flatMap(evaluate) parse(peggyCode)->Result.flatMap(evaluate)
let evaluateAsExternal = (expression: t): result<ExternalExpressionValue.t, errorValue> =>
{
let accessors = ProjectAccessorsT.identityAccessors
expression->reduceExpressionInProject(accessors.stdLib, accessors)
}->Result.map(InternalExpressionValue.toExternal)
let evaluateStringAsExternal = (peggyCode: string): result<
ExternalExpressionValue.t,
errorValue,
> => parse(peggyCode)->Result.flatMap(evaluateAsExternal)
} }

View File

@ -1,9 +1,9 @@
module ErrorValue = Reducer_ErrorValue module ErrorValue = Reducer_ErrorValue
module Extra_Array = Reducer_Extra_Array module Extra_Array = Reducer_Extra_Array
type internalCode = ReducerInterface_ExternalExpressionValue.internalCode type internalCode = Object
type environment = ReducerInterface_ExternalExpressionValue.environment type environment = GenericDist.env
let defaultEnvironment = ReducerInterface_ExternalExpressionValue.defaultEnvironment let defaultEnvironment: environment = DistributionOperation.defaultEnv
type rec t = type rec t =
| IEvArray(array<t>) // FIXME: Convert to MapInt | IEvArray(array<t>) // FIXME: Convert to MapInt
@ -38,29 +38,6 @@ type internalExpressionValue = t
type functionCall = (string, array<t>) type functionCall = (string, array<t>)
module Internal = {
module NameSpace = {
external castNameSpaceToHidden: nameSpace => ReducerInterface_ExternalExpressionValue.hiddenNameSpace =
"%identity"
external castHiddenToNameSpace: ReducerInterface_ExternalExpressionValue.hiddenNameSpace => nameSpace =
"%identity"
}
module Lambda = {
let toInternal = (v: ReducerInterface_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): ReducerInterface_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) => {
@ -147,7 +124,7 @@ let toStringResultOkless = (codeResult: result<t, ErrorValue.errorValue>): strin
let toStringResultRecord = x => let toStringResultRecord = x =>
switch x { switch x {
| Ok(a) => `Ok(${ReducerInterface_ExternalExpressionValue.toStringRecord(a)})` | Ok(a) => `Ok(${toStringMap(a)})`
| Error(m) => `Error(${ErrorValue.errorToString(m)})` | Error(m) => `Error(${ErrorValue.errorToString(m)})`
} }
@ -247,79 +224,18 @@ let functionCallSignatureToString = (functionCallSignature: functionCallSignatur
`${fn}(${args->Js.Array2.map(valueTypeToString)->Js.Array2.toString})` `${fn}(${args->Js.Array2.map(valueTypeToString)->Js.Array2.toString})`
} }
let rec toExternal = (iev: t): ReducerInterface_ExternalExpressionValue.t => {
switch iev {
| IEvArray(v) => v->Belt.Array.map(e => toExternal(e))->EvArray
| IEvArrayString(v) => EvArrayString(v)
| IEvBool(v) => EvBool(v)
| IEvCall(v) => EvCall(v)
| IEvDeclaration(v) => {
let fn = lambdaValueToExternal(v.fn)
let args = v.args
EvDeclaration({fn: fn, args: args})
}
| 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)
| IEvType(v) => v->mapToExternal->EvType
| 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
and lambdaValueToExternal = Internal.Lambda.toExternal
and nameSpaceToTypeScriptBindings = (
nameSpace: nameSpace,
): ReducerInterface_ExternalExpressionValue.externalBindings => {
let NameSpace(container) = nameSpace
Belt.Map.String.map(container, e => toExternal(e))->Belt.Map.String.toArray->Js.Dict.fromArray
}
let rec toInternal = (ev: ReducerInterface_ExternalExpressionValue.t): t => {
switch ev {
| EvArray(v) => v->Belt.Array.map(e => toInternal(e))->IEvArray
| EvArrayString(v) => IEvArrayString(v)
| EvBool(v) => IEvBool(v)
| EvCall(v) => IEvCall(v)
| EvDate(v) => IEvDate(v)
| EvDeclaration(v) => {
let fn = lambdaValueToInternal(v.fn)
let args = v.args
IEvDeclaration({fn: fn, args: args})
}
| EvDistribution(v) => IEvDistribution(v)
| EvLambda(v) => IEvLambda(lambdaValueToInternal(v))
| EvModule(v) => v->nameSpaceFromTypeScriptBindings->IEvBindings
| EvNumber(v) => IEvNumber(v)
| EvRecord(v) => v->recordToInternal->IEvRecord
| EvString(v) => IEvString(v)
| EvSymbol(v) => IEvSymbol(v)
| EvTimeDuration(v) => IEvTimeDuration(v)
| EvType(v) => v->recordToInternal->IEvType
| EvTypeIdentifier(v) => IEvTypeIdentifier(v)
| EvVoid => IEvVoid
}
}
and recordToInternal = v =>
v->Js.Dict.entries->Belt.Map.String.fromArray->Belt.Map.String.map(e => toInternal(e))
and lambdaValueToInternal = Internal.Lambda.toInternal
and nameSpaceFromTypeScriptBindings = (
r: ReducerInterface_ExternalExpressionValue.externalBindings,
): nameSpace =>
r->Js.Dict.entries->Belt.Map.String.fromArray->Belt.Map.String.map(e => toInternal(e))->NameSpace
let nameSpaceToKeyValueArray = (nameSpace: nameSpace): array<(string, t)> => {
let NameSpace(container) = nameSpace
container->Belt.Map.String.toArray
}
let arrayToValueArray = (arr: array<t>): array<t> => arr let arrayToValueArray = (arr: array<t>): array<t> => arr
let recordToKeyValuePairs = (record: map): array<(string, t)> => record->Belt.Map.String.toArray let recordToKeyValuePairs = (record: map): array<(string, t)> => record->Belt.Map.String.toArray
// let nameSpaceToTypeScriptBindings = (
// nameSpace: nameSpace,
// ) => {
// let NameSpace(container) = nameSpace
// Belt.Map.String.map(container, e => e->Belt.Map.String.toArray->Js.Dict.fromArray)
// }
let nameSpaceToKeyValuePairs = (nameSpace: nameSpace): array<(string, t)> => {
let NameSpace(container) = nameSpace
container->Belt.Map.String.toArray
}

View File

@ -2,5 +2,3 @@ module Bindings = Reducer_Bindings
let internalStdLib: Bindings.t = let internalStdLib: Bindings.t =
Bindings.emptyBindings->SquiggleLibrary_Math.makeBindings->SquiggleLibrary_Versions.makeBindings Bindings.emptyBindings->SquiggleLibrary_Math.makeBindings->SquiggleLibrary_Versions.makeBindings
let externalStdLib = internalStdLib->Bindings.toTypeScriptBindings