squiggle/packages/squiggle-lang/src/rescript/Reducer/Reducer_Module/Reducer_Module.res

126 lines
4.0 KiB
Plaintext
Raw Normal View History

module ExpressionT = Reducer_Expression_T
open ReducerInterface_InternalExpressionValue
2022-06-27 00:40:31 +00:00
let expressionValueToString = toString
type t = ReducerInterface_InternalExpressionValue.nameSpace
let typeAliasesKey = "_typeAliases_"
let typeReferencesKey = "_typeReferences_"
let getType = (nameSpace: t, id: string) => {
let NameSpace(container) = nameSpace
Belt.Map.String.get(container, typeAliasesKey)->Belt.Option.flatMap(aliases =>
switch aliases {
2022-06-23 18:38:07 +00:00
| IEvRecord(r) => Belt.Map.String.get(r, id)
| _ => None
}
)
}
let getTypeOf = (nameSpace: t, id: string) => {
let NameSpace(container) = nameSpace
Belt.Map.String.get(container, typeReferencesKey)->Belt.Option.flatMap(defs =>
switch defs {
2022-06-23 18:38:07 +00:00
| IEvRecord(r) => Belt.Map.String.get(r, id)
| _ => None
}
)
}
let getWithDefault = (nameSpace: t, id: string, default) => {
let NameSpace(container) = nameSpace
Belt.Map.String.getWithDefault(container, id, default)
}
let get = (nameSpace: t, id: string) => {
let NameSpace(container) = nameSpace
Belt.Map.String.get(container, id)
}
let emptyMap: map = Belt.Map.String.empty
let setTypeAlias = (nameSpace: t, id: string, value): t => {
let NameSpace(container) = nameSpace
2022-06-23 18:38:07 +00:00
let rValue = Belt.Map.String.getWithDefault(container, typeAliasesKey, IEvRecord(emptyMap))
let r = switch rValue {
2022-06-23 18:38:07 +00:00
| IEvRecord(r) => r
| _ => emptyMap
}
2022-06-23 18:38:07 +00:00
let r2 = Belt.Map.String.set(r, id, value)->IEvRecord
Belt.Map.String.set(container, typeAliasesKey, r2)->NameSpace
}
let setTypeOf = (nameSpace: t, id: string, value): t => {
let NameSpace(container) = nameSpace
2022-06-23 18:38:07 +00:00
let rValue = Belt.Map.String.getWithDefault(container, typeReferencesKey, IEvRecord(emptyMap))
let r = switch rValue {
2022-06-23 18:38:07 +00:00
| IEvRecord(r) => r
| _ => emptyMap
}
2022-06-23 18:38:07 +00:00
let r2 = Belt.Map.String.set(r, id, value)->IEvRecord
Belt.Map.String.set(container, typeReferencesKey, r2)->NameSpace
}
let set = (nameSpace: t, id: string, value): t => {
let NameSpace(container) = nameSpace
Belt.Map.String.set(container, id, value)->NameSpace
}
2022-06-27 00:40:31 +00:00
let emptyModule: t = NameSpace(emptyMap)
let fromTypeScriptBindings = ReducerInterface_InternalExpressionValue.nameSpaceFromTypeScriptBindings
let toTypeScriptBindings = ReducerInterface_InternalExpressionValue.nameSpaceToTypeScriptBindings
2022-06-24 10:15:38 +00:00
let toExpressionValue = (nameSpace: t): internalExpressionValue => IEvModule(nameSpace)
let fromExpressionValue = (aValue: internalExpressionValue): t =>
switch aValue {
2022-06-23 18:38:07 +00:00
| IEvModule(nameSpace) => nameSpace
| _ => emptyModule
}
let fromArray = a => Belt.Map.String.fromArray(a)->NameSpace
let merge = (nameSpace: t, other: t): t => {
let NameSpace(container) = nameSpace
let NameSpace(otherContainer) = other
otherContainer
->Belt.Map.String.reduce(container, (container, key, value) =>
Belt.Map.String.set(container, key, value)
)
->NameSpace
}
let removeOther = (nameSpace: t, other: t): t => {
let NameSpace(container) = nameSpace
let NameSpace(otherContainer) = other
let keys = Belt.Map.String.keysToArray(otherContainer)
Belt.Map.String.keep(container, (key, _value) => {
let removeThis = Js.Array2.includes(keys, key)
!removeThis
})->NameSpace
}
2022-06-27 00:40:31 +00:00
external castExpressionToInternalCode: ExpressionT.expressionOrFFI => internalCode = "%identity"
let eLambdaFFIValue = (ffiFn: ExpressionT.ffiFn) => {
IEvLambda({
parameters: [],
context: emptyModule,
body: FFI(ffiFn)->castExpressionToInternalCode,
})
}
// -- Module definition
2022-06-24 10:15:38 +00:00
let define = (nameSpace: t, identifier: string, ev: internalExpressionValue): t => {
let NameSpace(container) = nameSpace
2022-06-27 00:40:31 +00:00
Belt.Map.String.set(container, identifier, ev)->NameSpace
}
let defineNumber = (nameSpace: t, identifier: string, value: float): t =>
2022-06-23 18:38:07 +00:00
nameSpace->define(identifier, IEvNumber(value))
let defineModule = (nameSpace: t, identifier: string, value: t): t =>
nameSpace->define(identifier, toExpressionValue(value))
2022-06-27 00:40:31 +00:00
let defineFFI = (nameSpace: t, identifier: string, value: ExpressionT.ffiFn): t =>
nameSpace->define(identifier, value->eLambdaFFIValue)