2022-06-16 12:08:59 +00:00
|
|
|
module ErrorValue = Reducer_ErrorValue
|
2022-06-24 10:15:38 +00:00
|
|
|
module ExternalExpressionValue = ReducerInterface_ExternalExpressionValue
|
2022-06-16 12:08:59 +00:00
|
|
|
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)
|
2022-07-18 14:11:10 +00:00
|
|
|
| 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)
|
2022-07-19 13:28:06 +00:00
|
|
|
| IEvVoid
|
2022-06-24 10:15:38 +00:00
|
|
|
and map = Belt.Map.String.t<t>
|
|
|
|
and nameSpace = NameSpace(Belt.Map.String.t<t>)
|
2022-06-16 12:08:59 +00:00
|
|
|
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-16 12:08:59 +00:00
|
|
|
|
2022-06-24 10:15:38 +00:00
|
|
|
type functionCall = (string, array<t>)
|
2022-06-16 12:08:59 +00:00
|
|
|
|
|
|
|
let rec toString = aValue =>
|
|
|
|
switch aValue {
|
2022-06-23 18:38:07 +00:00
|
|
|
| IEvArray(anArray) => {
|
2022-06-16 12:08:59 +00:00
|
|
|
let args = anArray->Js.Array2.map(each => toString(each))->Js.Array2.toString
|
|
|
|
`[${args}]`
|
|
|
|
}
|
2022-06-23 18:38:07 +00:00
|
|
|
| IEvArrayString(anArray) => {
|
2022-06-16 12:08:59 +00:00
|
|
|
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)`
|
2022-07-18 14:11:10 +00:00
|
|
|
| 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}`
|
2022-07-19 13:28:06 +00:00
|
|
|
| IEvVoid => `()`
|
2022-06-16 12:08:59 +00:00
|
|
|
}
|
|
|
|
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)}`
|
2022-07-19 13:28:06 +00:00
|
|
|
| IEvVoid => `Void`
|
2022-06-16 12:08:59 +00:00
|
|
|
}
|
|
|
|
|
2022-06-24 10:15:38 +00:00
|
|
|
let argsToString = (args: array<t>): string => {
|
2022-06-16 12:08:59 +00:00
|
|
|
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 =>
|
2022-06-16 12:08:59 +00:00
|
|
|
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 =
|
2022-06-16 12:08:59 +00:00
|
|
|
| EvtArray
|
|
|
|
| EvtArrayString
|
|
|
|
| EvtBool
|
|
|
|
| EvtCall
|
2022-06-27 00:40:31 +00:00
|
|
|
| EvtDate
|
|
|
|
| EvtDeclaration
|
2022-06-16 12:08:59 +00:00
|
|
|
| EvtDistribution
|
|
|
|
| EvtLambda
|
2022-06-27 00:40:31 +00:00
|
|
|
| EvtModule
|
2022-06-16 12:08:59 +00:00
|
|
|
| EvtNumber
|
|
|
|
| EvtRecord
|
|
|
|
| EvtString
|
|
|
|
| EvtSymbol
|
|
|
|
| EvtTimeDuration
|
2022-06-27 00:40:31 +00:00
|
|
|
| EvtType
|
2022-06-16 12:08:59 +00:00
|
|
|
| EvtTypeIdentifier
|
2022-07-19 13:28:06 +00:00
|
|
|
| EvtVoid
|
2022-06-16 12:08:59 +00:00
|
|
|
|
2022-06-24 10:15:38 +00:00
|
|
|
type functionCallSignature = CallSignature(string, array<internalExpressionValueType>)
|
2022-06-16 12:08:59 +00:00
|
|
|
type functionDefinitionSignature =
|
2022-06-24 10:15:38 +00:00
|
|
|
FunctionDefinitionSignature(functionCallSignature, internalExpressionValueType)
|
2022-06-16 12:08:59 +00:00
|
|
|
|
|
|
|
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
|
2022-07-18 14:11:10 +00:00
|
|
|
| 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
|
2022-07-19 13:28:06 +00:00
|
|
|
| IEvVoid => EvtVoid
|
2022-06-16 12:08:59 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 02:50:18 +00:00
|
|
|
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
|
2022-07-17 02:50:18 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 12:08:59 +00:00
|
|
|
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 =>
|
2022-06-16 12:08:59 +00:00
|
|
|
switch valueType {
|
|
|
|
| EvtArray => `Array`
|
|
|
|
| EvtArrayString => `ArrayString`
|
|
|
|
| EvtBool => `Bool`
|
|
|
|
| EvtCall => `Call`
|
2022-06-27 00:40:31 +00:00
|
|
|
| EvtDate => `Date`
|
|
|
|
| EvtDeclaration => `Declaration`
|
2022-06-16 12:08:59 +00:00
|
|
|
| EvtDistribution => `Distribution`
|
|
|
|
| EvtLambda => `Lambda`
|
2022-06-27 00:40:31 +00:00
|
|
|
| EvtModule => `Module`
|
2022-06-16 12:08:59 +00:00
|
|
|
| EvtNumber => `Number`
|
|
|
|
| EvtRecord => `Record`
|
|
|
|
| EvtString => `String`
|
|
|
|
| EvtSymbol => `Symbol`
|
|
|
|
| EvtTimeDuration => `Duration`
|
2022-06-27 00:40:31 +00:00
|
|
|
| EvtType => `Type`
|
2022-06-16 12:08:59 +00:00
|
|
|
| EvtTypeIdentifier => `TypeIdentifier`
|
2022-07-19 13:28:06 +00:00
|
|
|
| EvtVoid => `Void`
|
2022-06-16 12:08:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 => {
|
2022-06-16 12:08:59 +00:00
|
|
|
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)
|
2022-07-18 14:11:10 +00:00
|
|
|
| IEvBindings(v) => v->nameSpaceToTypeScriptBindings->EvModule
|
2022-07-19 13:28:06 +00:00
|
|
|
| IEvVoid => EvVoid
|
2022-06-16 12:08:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
and mapToExternal = v =>
|
|
|
|
v->Belt.Map.String.map(e => toExternal(e))->Belt.Map.String.toArray->Js.Dict.fromArray
|
|
|
|
and lambdaValueToExternal = v => {
|
|
|
|
let p = v.parameters
|
|
|
|
let c = v.context->nameSpaceToTypeScriptBindings
|
|
|
|
let b = v.body
|
|
|
|
{parameters: p, context: c, body: b}
|
|
|
|
}
|
|
|
|
and nameSpaceToTypeScriptBindings = (
|
|
|
|
nameSpace: nameSpace,
|
2022-06-24 10:15:38 +00:00
|
|
|
): ReducerInterface_ExternalExpressionValue.externalBindings => {
|
2022-06-16 12:08:59 +00:00
|
|
|
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 => {
|
2022-06-16 12:08:59 +00:00
|
|
|
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))
|
2022-07-18 14:11:10 +00:00
|
|
|
| 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)
|
2022-07-19 13:28:06 +00:00
|
|
|
| EvVoid => IEvVoid
|
2022-06-16 12:08:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
and recordToInternal = v =>
|
|
|
|
v->Js.Dict.entries->Belt.Map.String.fromArray->Belt.Map.String.map(e => toInternal(e))
|
|
|
|
and lambdaValueToInternal = v => {
|
|
|
|
let p = v.parameters
|
|
|
|
let c = v.context->nameSpaceFromTypeScriptBindings
|
|
|
|
let b = v.body
|
|
|
|
{parameters: p, context: c, body: b}
|
|
|
|
}
|
|
|
|
and nameSpaceFromTypeScriptBindings = (
|
2022-06-24 10:15:38 +00:00
|
|
|
r: ReducerInterface_ExternalExpressionValue.externalBindings,
|
2022-06-16 12:08:59 +00:00
|
|
|
): nameSpace =>
|
|
|
|
r->Js.Dict.entries->Belt.Map.String.fromArray->Belt.Map.String.map(e => toInternal(e))->NameSpace
|