From 1dfc9fe820e117e04f21df2879c2628d2beaf1d3 Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Thu, 25 Aug 2022 10:43:51 +0200 Subject: [PATCH] RENeedToRun->Error --- .../ReducerProject/ReducerProject_test.res | 2 +- .../ReducerProject_tutorial_0_test.res | 6 +++--- .../ReducerProject_tutorial_1_multisource_test.res | 6 +++--- .../ReducerProject_tutorial_2_includes_test.res | 2 +- ...cerProject_tutorial_3_injecting_user_values.res | 2 +- ...ReducerProject_tutorial_4_calling_functions.res | 2 +- .../src/rescript/ForTS/ForTS_ReducerProject.res | 5 +++-- .../src/rescript/Reducer/Reducer_ErrorValue.res | 2 ++ .../src/rescript/ReducerProject/ReducerProject.res | 14 ++++++++++---- 9 files changed, 25 insertions(+), 16 deletions(-) diff --git a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_test.res b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_test.res index 7b3a75ff..d258d71a 100644 --- a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_test.res +++ b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_test.res @@ -11,7 +11,7 @@ open Expect.Operators let runFetchResult = (project, sourceId) => { Project.run(project, sourceId) - Project.getResult(project, sourceId)->InternalExpressionValue.toStringOptionResult + Project.getResult(project, sourceId)->InternalExpressionValue.toStringResult } let runFetchBindings = (project, sourceId) => { diff --git a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_0_test.res b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_0_test.res index 61dbc11e..d6f43904 100644 --- a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_0_test.res +++ b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_0_test.res @@ -50,7 +50,7 @@ Case "Running a single source". /* Let's display the result and bindings */ ( - result->InternalExpressionValue.toStringOptionResult, + result->InternalExpressionValue.toStringResult, bindings->InternalExpressionValue.IEvBindings->InternalExpressionValue.toString, )->expect == ("Ok(3)", "@{}") /* You've got 3 with empty bindings. */ @@ -64,7 +64,7 @@ Case "Running a single source". let bindings = Project.getBindings(project, "main") /* Now you have external bindings and external result. */ ( - result->InternalExpressionValue.toStringOptionResult, + result->InternalExpressionValue.toStringResult, bindings->InternalExpressionValue.IEvBindings->InternalExpressionValue.toString, )->expect == ("Ok(3)", "@{}") }) @@ -80,7 +80,7 @@ Case "Running a single source". Project.runAll(project) let result = Project.getResult(project, "main") let _bindings = Project.getBindings(project, "main") - result->InternalExpressionValue.toStringOptionResult->expect == "Ok(3)" + result->InternalExpressionValue.toStringResult->expect == "Ok(3)" }) test("shortcut", () => { diff --git a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_1_multisource_test.res b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_1_multisource_test.res index b0b44892..4362fcf6 100644 --- a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_1_multisource_test.res +++ b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_1_multisource_test.res @@ -32,7 +32,7 @@ describe("ReducerProject Tutorial", () => { let bindings3 = Project.getBindings(project, "source3") ( - result3->InternalExpressionValue.toStringOptionResult, + result3->InternalExpressionValue.toStringResult, bindings3->InternalExpressionValue.IEvBindings->InternalExpressionValue.toString, )->expect == ("Ok(())", "@{x: 1,y: 2,z: 3}") }) @@ -58,7 +58,7 @@ describe("ReducerProject Tutorial", () => { let bindings3 = Project.getBindings(project, "source3") ( - result3->InternalExpressionValue.toStringOptionResult, + result3->InternalExpressionValue.toStringResult, bindings3->InternalExpressionValue.IEvBindings->InternalExpressionValue.toString, )->expect == ("Ok(())", "@{x: 1,y: 2,z: 3}") }) @@ -94,7 +94,7 @@ describe("ReducerProject Tutorial", () => { let bindings3 = Project.getBindings(project, "source3") ( - result3->InternalExpressionValue.toStringOptionResult, + result3->InternalExpressionValue.toStringResult, bindings3->InternalExpressionValue.IEvBindings->InternalExpressionValue.toString, )->expect == ("Ok(())", "@{x: 1,y: 2,z: 3}") /* diff --git a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_2_includes_test.res b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_2_includes_test.res index ce953ee0..a4c2e0a3 100644 --- a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_2_includes_test.res +++ b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_2_includes_test.res @@ -146,7 +146,7 @@ Here we will finally proceed to a real life scenario. */ /* And see the result and bindings.. */ test("recursive includes", () => { ( - result->InternalExpressionValue.toStringOptionResult, + result->InternalExpressionValue.toStringResult, bindings->InternalExpressionValue.IEvBindings->InternalExpressionValue.toString, )->expect == ("Ok(6)", "@{doubleX: 2,x: 1,y: 2,z: 3}") /* Everything as expected */ diff --git a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_3_injecting_user_values.res b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_3_injecting_user_values.res index f47396b0..38d655c1 100644 --- a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_3_injecting_user_values.res +++ b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_3_injecting_user_values.res @@ -30,7 +30,7 @@ describe("ReducerProject Tutorial", () => { /* We can now run the project */ Project.runAll(project) let result = Project.getResult(project, "main") - result->InternalExpressionValue.toStringOptionResult->expect == "Ok(6)" + result->InternalExpressionValue.toStringResult->expect == "Ok(6)" }) }) diff --git a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_4_calling_functions.res b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_4_calling_functions.res index d595f35a..7e2471b5 100644 --- a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_4_calling_functions.res +++ b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_4_calling_functions.res @@ -32,7 +32,7 @@ describe("ReducerProject Tutorial", () => { test("userResults", () => { let userResultsAsString = Belt.Array.map(userResults, aResult => - aResult->InternalExpressionValue.toStringOptionResult + aResult->InternalExpressionValue.toStringResult ) userResultsAsString->expect == ["Ok(2)", "Ok(4)", "Ok(6)", "Ok(8)", "Ok(10)"] }) diff --git a/packages/squiggle-lang/src/rescript/ForTS/ForTS_ReducerProject.res b/packages/squiggle-lang/src/rescript/ForTS/ForTS_ReducerProject.res index d35dd156..daf945b9 100644 --- a/packages/squiggle-lang/src/rescript/ForTS/ForTS_ReducerProject.res +++ b/packages/squiggle-lang/src/rescript/ForTS/ForTS_ReducerProject.res @@ -194,8 +194,9 @@ let getBindings = (project: reducerProject, sourceId: string): squiggleValue_Mod Get the result after running this source file or the project */ @genType -let getResult = (project: reducerProject, sourceId: string): option< - result, +let getResult = (project: reducerProject, sourceId: string): result< + squiggleValue, + reducerErrorValue, > => project->T.Private.castToInternalProject->Private.getResult(sourceId) /* diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_ErrorValue.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_ErrorValue.res index a3d1acff..55cde709 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_ErrorValue.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_ErrorValue.res @@ -21,6 +21,7 @@ type errorValue = | RESyntaxError(string, option) | RETodo(string) // To do | REUnitNotFound(string) + | RENeedToRun type t = errorValue @@ -57,4 +58,5 @@ let errorToString = err => | RETodo(msg) => `TODO: ${msg}` | REExpectedType(typeName, valueString) => `Expected type: ${typeName} but got: ${valueString}` | REUnitNotFound(unitName) => `Unit not found: ${unitName}` + | RENeedToRun => "Need to run" } diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res index b09a44d9..5f2fdce0 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res @@ -96,9 +96,15 @@ module Private = { Belt.Map.String.set(project["items"], sourceId, newItem)->T.Private.setFieldItems(project, _) } - let getResult = (project: t, sourceId: string): ProjectItem.T.resultType => + let getResultOption = (project: t, sourceId: string): ProjectItem.T.resultType => project->getItem(sourceId)->ProjectItem.getResult + let getResult = (project: t, sourceId: string): ProjectItem.T.resultArgumentType => + switch getResultOption(project, sourceId) { + | None => RENeedToRun->Error + | Some(result) => result + } + let setResult = (project: t, sourceId: string, value: ProjectItem.T.resultArgumentType): unit => { let newItem = project->getItem(sourceId)->ProjectItem.setResult(value) Belt.Map.String.set(project["items"], sourceId, newItem)->T.Private.setFieldItems(project, _) @@ -154,7 +160,7 @@ module Private = { sourceId: string, (rPrevResult: ProjectItem.T.resultArgumentType, continuation: ProjectItem.T.continuation), ): (ProjectItem.T.resultArgumentType, ProjectItem.T.continuation) => { - switch getResult(project, sourceId) { + switch getResultOption(project, sourceId) { | Some(result) => (result, getContinuation(project, sourceId)) // already ran | None => switch rPrevResult { @@ -165,7 +171,7 @@ module Private = { | Ok(_prevResult) => { doRunWithContinuation(project, sourceId, continuation) ( - getResult(project, sourceId)->Belt.Option.getWithDefault(rPrevResult), + getResultOption(project, sourceId)->Belt.Option.getWithDefault(rPrevResult), getContinuation(project, sourceId), ) } @@ -197,6 +203,6 @@ module Private = { let these = project->getStdLib let ofUser = Continuation.minus(those, these) - (getResult(project, "main")->Belt.Option.getWithDefault(IEvVoid->Ok), ofUser) + (getResultOption(project, "main")->Belt.Option.getWithDefault(IEvVoid->Ok), ofUser) } }