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

177 lines
5.2 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)
2022-05-22 22:37:07 +00:00
| EvDate(Js.Date.t)
| EvTimeDuration(float)
2022-05-24 21:02:27 +00:00
| EvDeclaration(lambdaDeclaration)
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-05-24 21:02:27 +00:00
and lambdaDeclaration = Declaration.declaration<lambdaValue>
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)
| EvDate(date) => DateTime.Date.toString(date)
| EvTimeDuration(t) => DateTime.Duration.toString(t)
2022-05-24 21:02:27 +00:00
| EvDeclaration(t) => "Declaration"
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-05-22 22:37:07 +00:00
| EvDate(_) => `Date::${toString(aValue)}`
| EvTimeDuration(_) => `Date::${toString(aValue)}`
2022-05-24 21:02:27 +00:00
| EvDeclaration(_) => `Declaration::${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
}
2022-05-22 17:02:20 +00:00
let toStringResultOkless = (codeResult: result<expressionValue, ErrorValue.errorValue>): string =>
parser toFunction additive multiplicative compact whitespace pow relational equality boolean whitespace separator left associative operators expression not identifier function call array constructor string indexed values ident priority block outerBlock optional final expression statement separator outerBlock innerBlock better errors note xor white space and record unary minus inner/outer block statement lambda sort lambda is a value constructor lambdaCall ternary ternary basicValue cleanup quotes chained Functions dot operators unify unary operators unify unary operatos notes notes notes notes parser priorities set white space or newline defined allow newlines notes function call has become a post operator recordElement recursive index postOperatorToFunction better integer comments notes record priority comment atom finalComment generated parser type cast format initiate test file test initiated; todo nodeCall; nodeExpression callIdentifier recover extra initiate testing initial tests pass tests pass remove function node ternary test parse passed to tests pass notes sort toExpression format notes remove unused modules remove unnecessary nodeLambdaCall notes note fix construct array comment test todo elixir pipe fix toString notes initial to expression test value test parsing records records comments ternary ifthenelse inner block passed inner block lambda lambda new parser tested now test tricks ternary in expression to test lambda as argument to test lambda in structures Use peggy Parser expectEvalError macros tested remove mathjs parse reducer test comparison operator
2022-05-05 19:45:25 +00:00
switch codeResult {
| Ok(a) => toString(a)
2022-05-19 18:25:18 +00:00
| Error(m) => `Error(${ErrorValue.errorToString(m)})`
parser toFunction additive multiplicative compact whitespace pow relational equality boolean whitespace separator left associative operators expression not identifier function call array constructor string indexed values ident priority block outerBlock optional final expression statement separator outerBlock innerBlock better errors note xor white space and record unary minus inner/outer block statement lambda sort lambda is a value constructor lambdaCall ternary ternary basicValue cleanup quotes chained Functions dot operators unify unary operators unify unary operatos notes notes notes notes parser priorities set white space or newline defined allow newlines notes function call has become a post operator recordElement recursive index postOperatorToFunction better integer comments notes record priority comment atom finalComment generated parser type cast format initiate test file test initiated; todo nodeCall; nodeExpression callIdentifier recover extra initiate testing initial tests pass tests pass remove function node ternary test parse passed to tests pass notes sort toExpression format notes remove unused modules remove unnecessary nodeLambdaCall notes note fix construct array comment test todo elixir pipe fix toString notes initial to expression test value test parsing records records comments ternary ifthenelse inner block passed inner block lambda lambda new parser tested now test tricks ternary in expression to test lambda as argument to test lambda in structures Use peggy Parser expectEvalError macros tested remove mathjs parse reducer test comparison operator
2022-05-05 19:45:25 +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
2022-05-22 17:02:20 +00:00
type expressionValueType =
| EvtArray
| EvtArrayString
| EvtBool
| EvtCall
| EvtDistribution
| EvtLambda
| EvtNumber
| EvtRecord
| EvtString
| EvtSymbol
2022-05-22 22:37:07 +00:00
| EvtDate
| EvtTimeDuration
2022-05-22 17:02:20 +00:00
type functionCallSignature = CallSignature(string, array<expressionValueType>)
type functionDefinitionSignature =
2022-05-22 17:02:20 +00:00
FunctionDefinitionSignature(functionCallSignature, expressionValueType)
let valueToValueType = value =>
switch value {
| EvArray(_) => EvtArray
| EvArrayString(_) => EvtArray
| EvBool(_) => EvtBool
| EvCall(_) => EvtCall
| EvDistribution(_) => EvtDistribution
| EvLambda(_) => EvtLambda
| EvNumber(_) => EvtNumber
| EvRecord(_) => EvtRecord
| EvString(_) => EvtArray
| EvSymbol(_) => EvtSymbol
2022-05-22 22:37:07 +00:00
| EvDate(_) => EvtDate
| EvTimeDuration(_) => EvtTimeDuration
}
let functionCallToCallSignature = (functionCall: functionCall): functionCallSignature => {
let (fn, args) = functionCall
CallSignature(fn, args->Js.Array2.map(valueToValueType))
}
2022-05-22 17:02:20 +00:00
let valueTypeToString = (valueType: expressionValueType): string =>
switch valueType {
| EvtArray => `Array`
| EvtArrayString => `ArrayString`
| EvtBool => `Bool`
| EvtCall => `Call`
| EvtDistribution => `Distribution`
| EvtLambda => `Lambda`
| EvtNumber => `Number`
| EvtRecord => `Record`
| EvtString => `String`
| EvtSymbol => `Symbol`
2022-05-22 22:37:07 +00:00
| EvtDate => `Date`
| EvtTimeDuration => `Duration`
}
let functionCallSignatureToString = (functionCallSignature: functionCallSignature): string => {
let CallSignature(fn, args) = functionCallSignature
`${fn}(${args->Js.Array2.map(valueTypeToString)->Js.Array2.toString})`
}