2022-07-25 14:34:33 +00:00
|
|
|
// TODO: Auto clean project based on topology
|
|
|
|
|
|
|
|
module Bindings = Reducer_Bindings
|
|
|
|
module ErrorValue = Reducer_ErrorValue
|
|
|
|
module InternalExpressionValue = ReducerInterface_InternalExpressionValue
|
|
|
|
module ProjectItem = ReducerProject_ProjectItem
|
|
|
|
module T = ReducerProject_T
|
2022-08-14 14:14:14 +00:00
|
|
|
module Topology = ReducerProject_Topology
|
2022-07-25 14:34:33 +00:00
|
|
|
|
|
|
|
type t = T.t
|
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let getItem = T.getItem
|
|
|
|
let getSourceIds = T.getSourceIds
|
|
|
|
let getDependents = Topology.getDependents
|
|
|
|
let getDependencies = Topology.getDependencies
|
|
|
|
let getRunOrder = Topology.getRunOrder
|
|
|
|
let getRunOrderFor = Topology.getRunOrderFor
|
|
|
|
|
|
|
|
let createProject = () => {
|
|
|
|
let project: t = {
|
|
|
|
items: Belt.MutableMap.String.make(),
|
|
|
|
stdLib: ReducerInterface_StdLib.internalStdLib,
|
|
|
|
environment: InternalExpressionValue.defaultEnvironment,
|
|
|
|
previousRunOrder: [],
|
|
|
|
}
|
|
|
|
project
|
|
|
|
}
|
2022-08-26 00:01:27 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
// will not be necessary when ProjectItem becomes mutable
|
|
|
|
let setItem = (project: t, sourceId: string, item: ProjectItem.t): unit => {
|
|
|
|
let _ = Belt.MutableMap.String.set(project.items, sourceId, item)
|
|
|
|
}
|
2022-08-26 00:01:27 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let rec touchSource_ = (project: t, sourceId: string): unit => {
|
|
|
|
let item = project->getItem(sourceId)
|
|
|
|
let newItem = ProjectItem.touchSource(item)
|
|
|
|
project->setItem(sourceId, newItem)
|
|
|
|
}
|
|
|
|
and touchDependents = (project: t, sourceId: string): unit => {
|
|
|
|
let _ = getDependents(project, sourceId)->Belt.Array.forEach(_, touchSource_(project, _))
|
|
|
|
}
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let touchSource = (project: t, sourceId: string): unit => {
|
|
|
|
touchSource_(project, sourceId)
|
|
|
|
touchDependents(project, sourceId)
|
|
|
|
}
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let handleNewTopology = (project: t): unit => {
|
|
|
|
let previousRunOrder = project.previousRunOrder
|
|
|
|
let currentRunOrder = Topology.getRunOrder(project)
|
|
|
|
let diff = Topology.runOrderDiff(currentRunOrder, previousRunOrder)
|
|
|
|
Belt.Array.forEach(diff, touchSource(project, _))
|
|
|
|
project.previousRunOrder = currentRunOrder
|
|
|
|
}
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let getSource = (project: t, sourceId: string): option<string> =>
|
|
|
|
Belt.MutableMap.String.get(project.items, sourceId)->Belt.Option.map(ProjectItem.getSource)
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let setSource = (project: t, sourceId: string, value: string): unit => {
|
|
|
|
let newItem = project->getItem(sourceId)->ProjectItem.setSource(value)
|
|
|
|
project->setItem(sourceId, newItem)
|
|
|
|
touchDependents(project, sourceId)
|
|
|
|
}
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let clean = (project: t, sourceId: string): unit => {
|
|
|
|
let newItem = project->getItem(sourceId)->ProjectItem.clean
|
|
|
|
project->setItem(sourceId, newItem)
|
|
|
|
}
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let cleanAll = (project: t): unit =>
|
|
|
|
project->getSourceIds->Belt.Array.forEach(sourceId => clean(project, sourceId))
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let cleanResults = (project: t, sourceId: string): unit => {
|
|
|
|
let newItem = project->getItem(sourceId)->ProjectItem.cleanResults
|
|
|
|
project->setItem(sourceId, newItem)
|
|
|
|
}
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let cleanAllResults = (project: t): unit =>
|
|
|
|
project->getSourceIds->Belt.Array.forEach(sourceId => project->cleanResults(sourceId))
|
2022-08-31 21:54:54 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let getIncludes = (project: t, sourceId: string): ProjectItem.T.includesType =>
|
|
|
|
project->getItem(sourceId)->ProjectItem.getIncludes
|
2022-08-31 21:54:54 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let getPastChain = (project: t, sourceId: string): array<string> =>
|
|
|
|
project->getItem(sourceId)->ProjectItem.getPastChain
|
2022-08-31 21:54:54 +00:00
|
|
|
|
2022-09-12 09:33:20 +00:00
|
|
|
let getIncludesAsVariables = (project: t, sourceId: string): ProjectItem.T.importAsVariablesType =>
|
2022-09-11 15:22:07 +00:00
|
|
|
project->getItem(sourceId)->ProjectItem.getIncludesAsVariables
|
2022-08-31 21:54:54 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let getDirectIncludes = (project: t, sourceId: string): array<string> =>
|
|
|
|
project->getItem(sourceId)->ProjectItem.getDirectIncludes
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let setContinues = (project: t, sourceId: string, continues: array<string>): unit => {
|
|
|
|
let newItem = project->getItem(sourceId)->ProjectItem.setContinues(continues)
|
|
|
|
project->setItem(sourceId, newItem)
|
|
|
|
handleNewTopology(project)
|
|
|
|
}
|
|
|
|
let getContinues = (project: t, sourceId: string): array<string> =>
|
2022-09-16 20:30:44 +00:00
|
|
|
project->getItem(sourceId)->ProjectItem.getContinues
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let removeContinues = (project: t, sourceId: string): unit => {
|
|
|
|
let newItem = project->getItem(sourceId)->ProjectItem.removeContinues
|
|
|
|
project->setItem(sourceId, newItem)
|
|
|
|
handleNewTopology(project)
|
|
|
|
}
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let setContinuation = (
|
|
|
|
project: t,
|
|
|
|
sourceId: string,
|
|
|
|
continuation: ProjectItem.T.continuationArgumentType,
|
|
|
|
): unit => {
|
|
|
|
let newItem = project->getItem(sourceId)->ProjectItem.setContinuation(continuation)
|
|
|
|
project->setItem(sourceId, newItem)
|
|
|
|
}
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let getResultOption = (project: t, sourceId: string): ProjectItem.T.resultType =>
|
|
|
|
project->getItem(sourceId)->ProjectItem.getResult
|
2022-08-25 08:43:51 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let getResult = (project: t, sourceId: string): ProjectItem.T.resultArgumentType =>
|
|
|
|
switch getResultOption(project, sourceId) {
|
|
|
|
| None => RENeedToRun->Error
|
|
|
|
| Some(result) => result
|
2022-07-25 14:34:33 +00:00
|
|
|
}
|
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let setResult = (project: t, sourceId: string, value: ProjectItem.T.resultArgumentType): unit => {
|
|
|
|
let newItem = project->getItem(sourceId)->ProjectItem.setResult(value)
|
|
|
|
project->setItem(sourceId, newItem)
|
|
|
|
}
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let parseIncludes = (project: t, sourceId: string): unit => {
|
|
|
|
let newItem = project->getItem(sourceId)->ProjectItem.parseIncludes
|
|
|
|
project->setItem(sourceId, newItem)
|
|
|
|
handleNewTopology(project)
|
|
|
|
}
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let rawParse = (project: t, sourceId): unit => {
|
|
|
|
let newItem = project->getItem(sourceId)->ProjectItem.rawParse
|
|
|
|
project->setItem(sourceId, newItem)
|
|
|
|
}
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-16 20:30:44 +00:00
|
|
|
let getStdLib = (project: t): Reducer_T.namespace => project.stdLib
|
|
|
|
let setStdLib = (project: t, value: Reducer_T.namespace): unit => {
|
2022-09-11 15:22:07 +00:00
|
|
|
project.stdLib = value
|
|
|
|
}
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let getEnvironment = (project: t): InternalExpressionValue.environment => project.environment
|
|
|
|
let setEnvironment = (project: t, value: InternalExpressionValue.environment): unit => {
|
|
|
|
project.environment = value
|
|
|
|
}
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-16 20:30:44 +00:00
|
|
|
let getBindings = (project: t, sourceId: string): Reducer_T.namespace => {
|
|
|
|
project->getItem(sourceId)->ProjectItem.getContinuation
|
|
|
|
}
|
|
|
|
|
|
|
|
let getBindingsAsRecord = (project: t, sourceId: string): Reducer_T.value => {
|
|
|
|
project->getBindings(sourceId)->Reducer_Namespace.toRecord
|
2022-09-11 15:22:07 +00:00
|
|
|
}
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-16 20:30:44 +00:00
|
|
|
let getContinuationsBefore = (project: t, sourceId: string): array<Reducer_T.namespace> => {
|
|
|
|
project->getPastChain(sourceId)->Belt.Array.map(project->getBindings)
|
2022-09-11 15:22:07 +00:00
|
|
|
}
|
2022-08-31 21:54:54 +00:00
|
|
|
|
2022-09-16 20:30:44 +00:00
|
|
|
let linkDependencies = (project: t, sourceId: string): Reducer_T.namespace => {
|
|
|
|
let nameSpace = Reducer_Namespace.mergeMany(
|
|
|
|
Belt.Array.concat(
|
|
|
|
[project->getStdLib],
|
|
|
|
project->getContinuationsBefore(sourceId)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let includesAsVariables = project->getIncludesAsVariables(sourceId)
|
2022-09-16 20:30:44 +00:00
|
|
|
Belt.Array.reduce(includesAsVariables, nameSpace, (acc, (variable, includeFile)) =>
|
|
|
|
acc->Reducer_Namespace.set(
|
2022-09-11 15:22:07 +00:00
|
|
|
variable,
|
2022-09-16 20:30:44 +00:00
|
|
|
project->getBindings(includeFile)->Reducer_Namespace.toRecord
|
2022-08-31 21:54:54 +00:00
|
|
|
)
|
2022-09-11 15:22:07 +00:00
|
|
|
)
|
|
|
|
}
|
2022-08-31 21:54:54 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let doLinkAndRun = (project: t, sourceId: string): unit => {
|
2022-09-16 20:30:44 +00:00
|
|
|
let context = Reducer_Context.createContext(
|
|
|
|
project->linkDependencies(sourceId),
|
|
|
|
project->getEnvironment
|
|
|
|
)
|
|
|
|
let newItem = project->getItem(sourceId)->ProjectItem.run(context)
|
2022-09-11 16:57:28 +00:00
|
|
|
// Js.log("after run " ++ newItem.continuation->Reducer_Bindings.toString)
|
2022-09-11 15:22:07 +00:00
|
|
|
project->setItem(sourceId, newItem)
|
|
|
|
}
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
type runState = ProjectItem.T.resultArgumentType
|
|
|
|
|
|
|
|
let tryRunWithResult = (
|
|
|
|
project: t,
|
|
|
|
sourceId: string,
|
|
|
|
rPrevResult: ProjectItem.T.resultArgumentType,
|
|
|
|
): ProjectItem.T.resultArgumentType => {
|
2022-09-17 22:19:08 +00:00
|
|
|
switch project->getResultOption(sourceId) {
|
2022-09-11 15:22:07 +00:00
|
|
|
| Some(result) => result // already ran
|
|
|
|
| None =>
|
|
|
|
switch rPrevResult {
|
|
|
|
| Error(error) => {
|
2022-09-17 22:19:08 +00:00
|
|
|
project->setResult(sourceId, Error(error))
|
2022-09-11 15:22:07 +00:00
|
|
|
Error(error)
|
|
|
|
}
|
|
|
|
| Ok(_prevResult) => {
|
2022-09-17 22:19:08 +00:00
|
|
|
project->doLinkAndRun(sourceId)
|
|
|
|
project->getResultOption(sourceId)->Belt.Option.getWithDefault(rPrevResult)
|
2022-07-25 14:34:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-11 15:22:07 +00:00
|
|
|
}
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let runAll = (project: t): unit => {
|
|
|
|
let runOrder = Topology.getRunOrder(project)
|
|
|
|
let initialState = Ok(Reducer_T.IEvVoid)
|
|
|
|
let _finalState = Belt.Array.reduce(runOrder, initialState, (currState, currId) =>
|
2022-09-17 22:19:08 +00:00
|
|
|
project->tryRunWithResult(currId, currState)
|
2022-09-11 15:22:07 +00:00
|
|
|
)
|
|
|
|
}
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let run = (project: t, sourceId: string): unit => {
|
|
|
|
let runOrder = Topology.getRunOrderFor(project, sourceId)
|
|
|
|
let initialState = Ok(Reducer_T.IEvVoid)
|
|
|
|
let _finalState = Belt.Array.reduce(runOrder, initialState, (currState, currId) =>
|
2022-09-17 22:19:08 +00:00
|
|
|
project->tryRunWithResult(currId, currState)
|
2022-09-11 15:22:07 +00:00
|
|
|
)
|
|
|
|
}
|
2022-07-25 14:34:33 +00:00
|
|
|
|
2022-09-11 15:22:07 +00:00
|
|
|
let evaluate = (sourceCode: string) => {
|
|
|
|
let project = createProject()
|
2022-09-17 22:19:08 +00:00
|
|
|
project->setSource("main", sourceCode)
|
|
|
|
project->runAll
|
2022-09-11 15:22:07 +00:00
|
|
|
|
|
|
|
(
|
2022-09-17 22:19:08 +00:00
|
|
|
project->getResultOption("main")->Belt.Option.getWithDefault(Reducer_T.IEvVoid->Ok),
|
2022-09-16 20:30:44 +00:00
|
|
|
project->getBindings("main")->Reducer_Namespace.toMap,
|
2022-09-11 15:22:07 +00:00
|
|
|
)
|
2022-07-25 14:34:33 +00:00
|
|
|
}
|