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

101 lines
2.9 KiB
Plaintext
Raw Normal View History

2022-03-29 09:09:59 +00:00
/*
Irreducible values. Reducer does not know about those. Only used for external calls
This is a configuration to to make external calls of those types
*/
2022-03-30 10:53:36 +00:00
module Extra_Array = Reducer_Extra_Array
2022-03-29 09:09:59 +00:00
module ErrorValue = Reducer_ErrorValue
2022-04-24 23:09:51 +00:00
@genType.opaque
type internalCode = Object
2022-04-11 03:16:31 +00:00
@genType
2022-03-29 09:09:59 +00:00
type rec expressionValue =
| EvArray(array<expressionValue>)
| EvArrayString(array<string>)
2022-03-29 09:09:59 +00:00
| EvBool(bool)
| EvCall(string) // External function call
| EvDistribution(DistributionTypes.genericDist)
2022-05-02 10:10:31 +00:00
| EvLambda(lambdaValue)
2022-03-29 09:09:59 +00:00
| EvNumber(float)
| EvRecord(record)
2022-03-29 09:09:59 +00:00
| EvString(string)
| EvSymbol(string)
and record = Js.Dict.t<expressionValue>
2022-05-02 10:10:31 +00:00
and externalBindings = record
and lambdaValue = {
parameters: array<string>,
context: externalBindings,
body: internalCode,
}
2022-03-29 09:09:59 +00:00
@genType
let defaultExternalBindings: externalBindings = Js.Dict.empty()
2022-03-29 09:09:59 +00:00
type functionCall = (string, array<expressionValue>)
2022-03-30 10:53:36 +00:00
let rec toString = aValue =>
2022-03-29 09:09:59 +00:00
switch aValue {
| EvArray(anArray) => {
let args = anArray->Js.Array2.map(each => toString(each))->Js.Array2.toString
`[${args}]`
}
| EvArrayString(anArray) => {
let args = anArray->Js.Array2.toString
`[${args}]`
}
2022-03-29 09:09:59 +00:00
| EvBool(aBool) => Js.String.make(aBool)
| EvCall(fName) => `:${fName}`
2022-05-02 10:55:28 +00:00
| EvLambda(lambdaValue) => `lambda(${Js.Array2.toString(lambdaValue.parameters)}=>internal code)`
2022-03-29 09:09:59 +00:00
| EvNumber(aNumber) => Js.String.make(aNumber)
| EvString(aString) => `'${aString}'`
| EvSymbol(aString) => `:${aString}`
| EvRecord(aRecord) => aRecord->toStringRecord
| EvDistribution(dist) => GenericDist.toString(dist)
2022-03-29 09:09:59 +00:00
}
and toStringRecord = aRecord => {
let pairs =
aRecord
->Js.Dict.entries
->Js.Array2.map(((eachKey, eachValue)) => `${eachKey}: ${toString(eachValue)}`)
->Js.Array2.toString
`{${pairs}}`
}
2022-03-29 09:09:59 +00:00
2022-03-30 10:53:36 +00:00
let toStringWithType = aValue =>
2022-03-29 09:09:59 +00:00
switch aValue {
2022-04-25 00:37:35 +00:00
| EvArray(_) => `Array::${toString(aValue)}`
| EvArrayString(_) => `ArrayString::${toString(aValue)}`
2022-03-30 10:53:36 +00:00
| EvBool(_) => `Bool::${toString(aValue)}`
| EvCall(_) => `Call::${toString(aValue)}`
2022-04-25 00:37:35 +00:00
| EvDistribution(_) => `Distribution::${toString(aValue)}`
2022-05-02 10:10:31 +00:00
| EvLambda(_) => `Lambda::${toString(aValue)}`
2022-03-30 10:53:36 +00:00
| EvNumber(_) => `Number::${toString(aValue)}`
2022-04-25 00:37:35 +00:00
| EvRecord(_) => `Record::${toString(aValue)}`
2022-03-30 10:53:36 +00:00
| EvString(_) => `String::${toString(aValue)}`
| EvSymbol(_) => `Symbol::${toString(aValue)}`
2022-03-29 09:09:59 +00:00
}
2022-03-30 10:53:36 +00:00
let argsToString = (args: array<expressionValue>): string => {
args->Js.Array2.map(arg => arg->toString)->Js.Array2.toString
2022-03-29 09:09:59 +00:00
}
2022-03-30 10:53:36 +00:00
let toStringFunctionCall = ((fn, args)): string => `${fn}(${argsToString(args)})`
2022-03-29 09:09:59 +00:00
2022-03-30 10:53:36 +00:00
let toStringResult = x =>
2022-03-29 09:09:59 +00:00
switch x {
2022-03-30 10:53:36 +00:00
| Ok(a) => `Ok(${toString(a)})`
| Error(m) => `Error(${ErrorValue.errorToString(m)})`
2022-03-29 09:09:59 +00:00
}
let toStringResultRecord = x =>
switch x {
| Ok(a) => `Ok(${toStringRecord(a)})`
| Error(m) => `Error(${ErrorValue.errorToString(m)})`
}
@genType
type environment = DistributionOperation.env
@genType
let defaultEnvironment: environment = DistributionOperation.defaultEnv