From f2dccd4f1ea3a72d55afd7814e7100c6ace80562 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Sun, 11 Sep 2022 20:57:28 +0400 Subject: [PATCH] still WIP, FR works --- .../SquiggleViewer/ExpressionViewer.tsx | 34 +++++++++---------- .../Reducer_Expression/Reducer_Expression.res | 6 ++-- .../Reducer_Expression_Lambda.res | 11 +++--- .../Reducer_Peggy_GeneratedParser.peggy | 23 +++---------- .../src/rescript/Reducer/Reducer_T.res | 11 +++--- ...ducerInterface_InternalExpressionValue.res | 5 +-- .../ReducerInterface_StdLib.res | 34 +++++++++++++++++-- .../ReducerProject/ReducerProject.res | 2 +- .../src/rescript/Utility/Mathjs.res | 16 ++++----- 9 files changed, 77 insertions(+), 65 deletions(-) diff --git a/packages/components/src/components/SquiggleViewer/ExpressionViewer.tsx b/packages/components/src/components/SquiggleViewer/ExpressionViewer.tsx index 1aa106c8..1ef5e150 100644 --- a/packages/components/src/components/SquiggleViewer/ExpressionViewer.tsx +++ b/packages/components/src/components/SquiggleViewer/ExpressionViewer.tsx @@ -135,23 +135,23 @@ export const ExpressionViewer: React.FC = ({ value, width }) => { {() => value.value.toString()} ); - case SqValueTag.Symbol: - return ( - - {() => ( - <> - Undefined Symbol: - {value.value} - - )} - - ); - case SqValueTag.Call: - return ( - - {() => value.value} - - ); + // case SqValueTag.Symbol: + // return ( + // + // {() => ( + // <> + // Undefined Symbol: + // {value.value} + // + // )} + // + // ); + // case SqValueTag.Call: + // return ( + // + // {() => value.value} + // + // ); case SqValueTag.ArrayString: return ( diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression.res index 1bfd330d..62a1f200 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression.res @@ -14,7 +14,7 @@ let rec evaluate: T.reducerFn = ( expression, context ) => { - Js.log(`reduce: ${expression->Reducer_Expression_T.toString}`) + // Js.log(`reduce: ${expression->Reducer_Expression_T.toString}`) switch expression { | T.EBlock(statements) => { let innerContext = {...context, bindings: context.bindings->Bindings.extend} @@ -25,12 +25,12 @@ let rec evaluate: T.reducerFn = ( } | T.EProgram(statements) => { - Js.log(`bindings: ${context.bindings->Reducer_Bindings.toString}`) + // Js.log(`bindings: ${context.bindings->Reducer_Bindings.toString}`) let res = statements->Js.Array2.reduce( (acc, statement) => statement->evaluate(context), T.IEvVoid ) - Js.log(`bindings after: ${context.bindings->Reducer_Bindings.toString}`) + // Js.log(`bindings after: ${context.bindings->Reducer_Bindings.toString}`) res } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res index 8316998c..e9460f69 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res @@ -43,11 +43,14 @@ let makeLambda = ( reducer(body, { bindings: localBindings, environment }) } - LNoFFI({ - context: bindings, + { + // context: bindings, body: lambda, parameters, - }) + } } -let makeFFILambda = () => raise(Not_found) +let makeFFILambda = (body: Reducer_T.lambdaBody): Reducer_T.lambdaValue => { + body, + parameters: ["..."] +} diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_GeneratedParser.peggy b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_GeneratedParser.peggy index a8c61d59..4beba469 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_GeneratedParser.peggy +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_GeneratedParser.peggy @@ -11,25 +11,10 @@ zeroOMoreArgumentsBlockOrExpression = innerBlockOrExpression / lambda outerBlock = statements:array_statements finalExpression: (statementSeparator @expression)? - { if (finalExpression) statements.push(finalExpression) - return h.nodeProgram(statements) } - // / '{' _nl finalExpression: expression _nl '}' - // { return h.nodeBlock([finalExpression]) } - // { if (finalExpression != null) - // { - // var newFinalExpression = h.makeFunctionCall('$_endOfOuterBlock_$', [h.nodeVoid(), finalExpression]); - // statements.push(newFinalExpression); - // } - // else - // { - // var newFinalStatement = h.makeFunctionCall('$_endOfOuterBlock_$', [h.nodeVoid(), h.nodeVoid()]); - // statements.push(newFinalStatement); - // } - // return h.nodeBlock(statements) } - // / finalExpression: expression - // { - // var newFinalExpression = h.makeFunctionCall('$_endOfOuterBlock_$', [h.nodeVoid(), finalExpression]); - // return h.nodeBlock([newFinalExpression])} + { if (finalExpression) statements.push(finalExpression) + return h.nodeProgram(statements) } + / finalExpression: expression + { return h.nodeProgram([finalExpression]) } innerBlockOrExpression = quotedInnerBlock diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_T.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_T.res index 19eedf39..a70e0183 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_T.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_T.res @@ -22,16 +22,13 @@ type rec value = @genType.opaque and arrayValue = array @genType.opaque and map = Belt.Map.String.t @genType.opaque and nameSpace = NameSpace(Belt.MutableMap.String.t, option) +and lambdaBody = (array, environment, reducerFn) => value @genType.opaque -and lambdaValue = - | LNoFFI({ +and lambdaValue = { parameters: array, - context: nameSpace, + // context: nameSpace, body: (array, environment, reducerFn) => value, - }) - | LFFI({ - body: (array, environment, reducerFn) => value, - }) + } @genType.opaque and lambdaDeclaration = Declaration.declaration and expression = | EBlock(array) diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res index 661b69e1..9eacb8df 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res @@ -49,10 +49,7 @@ and toStringDate = date => DateTime.Date.toString(date) and toStringDeclaration = d => Declaration.toString(d, r => toString(IEvLambda(r))) and toStringDistribution = dist => GenericDist.toString(dist) and toStringLambda = (lambdaValue: T.lambdaValue) => - switch lambdaValue { - | LNoFFI({ parameters }) => `lambda(${parameters->Js.Array2.toString}=>internal code)` // TODO - serialize code too? - | LFFI(_) => `standard function` // TODO - serialize name, etc? - } + `lambda(${Js.Array2.toString(lambdaValue.parameters)}=>internal code)` and toStringFunction = (lambdaValue: T.lambdaValue) => `function(${Js.Array2.toString(lambdaValue.parameters)})` and toStringNumber = aNumber => Js.String.make(aNumber) and toStringRecord = aMap => aMap->toStringMap diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_StdLib.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_StdLib.res index 3a8c41cc..81daff42 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_StdLib.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_StdLib.res @@ -1,4 +1,34 @@ -let internalStdLib: Reducer_Bindings.t = - Reducer_Bindings.makeEmptyBindings() +let internalStdLib: Reducer_Bindings.t = { + let res = Reducer_Bindings.makeEmptyBindings() ->SquiggleLibrary_Math.makeBindings ->SquiggleLibrary_Versions.makeBindings + + let _ = res->Reducer_Bindings.set("multiply", Reducer_Expression_Lambda.makeFFILambda( + (arguments, _, _) => { + switch arguments { + | [IEvNumber(x), IEvNumber(y)] => IEvNumber(x *. y) + | _ => raise(Not_found) + } + } + )->Reducer_T.IEvLambda) + + FunctionRegistry_Library.registry.fnNameDict->Js.Dict.keys->Js.Array2.forEach( + (name) => { + let _ = res->Reducer_Bindings.set(name, Reducer_Expression_Lambda.makeFFILambda( + (arguments, environment, reducer) => { + switch FunctionRegistry_Library.dispatch((name, arguments), environment, reducer) { + | Some(result) => { + switch result { + | Ok(value) => value + | Error(error) => error->Reducer_ErrorValue.ErrorException->raise + } + } + | None => Reducer_ErrorValue.RESymbolNotFound("Not found in registry")->Reducer_ErrorValue.ErrorException->raise + } + } + )->Reducer_T.IEvLambda) + } + ) + + res +} diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res index bb314f80..5e7632b0 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res @@ -193,7 +193,7 @@ let doLinkAndRun = (project: t, sourceId: string): unit => { // FIXME: fill context with dependencies // let continuation = linkDependencies(project, sourceId) let newItem = project->getItem(sourceId)->ProjectItem.run(context) - Js.log("after run " ++ newItem.continuation->Reducer_Bindings.toString) + // Js.log("after run " ++ newItem.continuation->Reducer_Bindings.toString) project->setItem(sourceId, newItem) } diff --git a/packages/squiggle-lang/src/rescript/Utility/Mathjs.res b/packages/squiggle-lang/src/rescript/Utility/Mathjs.res index 5e1be176..4a792af4 100644 --- a/packages/squiggle-lang/src/rescript/Utility/Mathjs.res +++ b/packages/squiggle-lang/src/rescript/Utility/Mathjs.res @@ -1,9 +1,9 @@ -@module("./MathjsWrapper.js") -external parseMathExt: string => Js.Json.t = "parseMath" +// @module("./MathjsWrapper.js") +// external parseMathExt: string => Js.Json.t = "parseMath" -let parseMath = (str: string): result => - switch parseMathExt(str) { - | exception Js.Exn.Error(err) => Error(Js.Exn.message(err) |> E.O.default("MathJS Parse Error")) - | exception _ => Error("MathJS Parse Error") - | j => Ok(j) - } +// let parseMath = (str: string): result => +// switch parseMathExt(str) { +// | exception Js.Exn.Error(err) => Error(Js.Exn.message(err) |> E.O.default("MathJS Parse Error")) +// | exception _ => Error("MathJS Parse Error") +// | j => Ok(j) +// }