From 53c44cdc9d08d596fcdb1940de012483f95d9176 Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Thu, 14 Apr 2022 18:17:45 +0200 Subject: [PATCH] refactor macro definitions --- .../Reducer_Dispatch/Reducer_Dispatch.res | 1 + .../Reducer_Dispatch_BuiltInMacros.res | 79 +++++++++++++++++++ .../Reducer_Expression/Reducer_Expression.res | 59 +------------- 3 files changed, 82 insertions(+), 57 deletions(-) create mode 100644 packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch.res index 23ab4451..ac74f121 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch.res @@ -1 +1,2 @@ module Builtin = Reducer_Dispatch_BuiltIn +module BuiltinMacros = Reducer_Dispatch_BuiltInMacros \ No newline at end of file diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res new file mode 100644 index 00000000..bae836bb --- /dev/null +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res @@ -0,0 +1,79 @@ +module ExpressionValue = ReducerInterface.ExpressionValue +module Result = Belt.Result +module ExpressionT = Reducer_Expression_T +open Reducer_ErrorValue + +type expression = ExpressionT.expression + +let dispatchMacroCall = (list: list, bindings: ExpressionT.bindings): result< + expression, + 'e, +> => { + let rec replaceSymbols = (expression: expression, bindings: ExpressionT.bindings): result< + expression, + errorValue, + > => + switch expression { + | ExpressionT.EValue(EvSymbol(aSymbol)) => + switch bindings->Belt.Map.String.get(aSymbol) { + | Some(boundExpression) => boundExpression->Ok + | None => RESymbolNotFound(aSymbol)->Error + } + | ExpressionT.EValue(_) => expression->Ok + | ExpressionT.EBindings(_) => expression->Ok + | ExpressionT.EList(list) => { + let racc = list->Belt.List.reduceReverse(Ok(list{}), (racc, each: expression) => + racc->Result.flatMap(acc => { + each + ->replaceSymbols(bindings) + ->Result.flatMap(newNode => { + acc->Belt.List.add(newNode)->Ok + }) + }) + ) + racc->Result.map(acc => acc->ExpressionT.EList) + } + } + + let doBindStatement = (statement: expression, bindings: ExpressionT.bindings) => { + switch statement { + | ExpressionT.EList(list{ + ExpressionT.EValue(EvCall("$let")), + ExpressionT.EValue(EvSymbol(aSymbol)), + expression, + }) => { + let rNewExpression = replaceSymbols(expression, bindings) + rNewExpression->Result.map(newExpression => + Belt.Map.String.set(bindings, aSymbol, newExpression)->ExpressionT.EBindings + ) + } + | _ => REAssignmentExpected->Error + } + } + + let doBindExpression = (expression: expression, bindings: ExpressionT.bindings) => { + switch expression { + | ExpressionT.EList(list{ExpressionT.EValue(EvCall("$let")), ..._}) => + REExpressionExpected->Error + | _ => replaceSymbols(expression, bindings) + } + } + + switch list { + | list{ExpressionT.EValue(EvCall("$$bindings"))} => bindings->ExpressionT.EBindings->Ok + + | list{ + ExpressionT.EValue(EvCall("$$bindStatement")), + ExpressionT.EBindings(bindings), + statement, + } => + doBindStatement(statement, bindings) + | list{ + ExpressionT.EValue(EvCall("$$bindExpression")), + ExpressionT.EBindings(bindings), + expression, + } => + doBindExpression(expression, bindings) + | _ => list->ExpressionT.EList->Ok + } +} 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 3a841654..b2b90633 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 @@ -59,63 +59,8 @@ let reduceExpression = (expression: t, bindings: T.bindings): result, bindings: T.bindings): result => { - let dispatchMacroCall = (list: list, bindings: T.bindings): result => { - let rec replaceSymbols = (expression: t, bindings: T.bindings): result => - switch expression { - | T.EValue(EvSymbol(aSymbol)) => - switch bindings->Belt.Map.String.get(aSymbol) { - | Some(boundExpression) => boundExpression->Ok - | None => RESymbolNotFound(aSymbol)->Error - } - | T.EValue(_) => expression->Ok - | T.EBindings(_) => expression->Ok - | T.EList(list) => { - let racc = list->Belt.List.reduceReverse(Ok(list{}), (racc, each: expression) => - racc->Result.flatMap(acc => { - each - ->replaceSymbols(bindings) - ->Result.flatMap(newNode => { - acc->Belt.List.add(newNode)->Ok - }) - }) - ) - racc->Result.map(acc => acc->T.EList) - } - } - - let doBindStatement = (statement: t, bindings: T.bindings) => { - switch statement { - | T.EList(list{T.EValue(EvCall("$let")), T.EValue(EvSymbol(aSymbol)), expression}) => { - let rNewExpression = replaceSymbols(expression, bindings) - rNewExpression->Result.map(newExpression => - Belt.Map.String.set(bindings, aSymbol, newExpression)->T.EBindings - ) - } - | _ => REAssignmentExpected->Error - } - } - - let doBindExpression = (expression: t, bindings: T.bindings) => { - switch expression { - | T.EList(list{T.EValue(EvCall("$let")), ..._}) => REExpressionExpected->Error - | _ => replaceSymbols(expression, bindings) - } - } - - switch list { - | list{T.EValue(EvCall("$$bindings"))} => bindings->T.EBindings->Ok - - | list{T.EValue(EvCall("$$bindStatement")), T.EBindings(bindings), statement} => - doBindStatement(statement, bindings) - | list{T.EValue(EvCall("$$bindExpression")), T.EBindings(bindings), expression} => - doBindExpression(expression, bindings) - | _ => list->T.EList->Ok - } - } - - list->dispatchMacroCall(bindings) - } + let doMacroCall = (list: list, bindings: T.bindings): result => + list->Reducer_Dispatch_BuiltInMacros.dispatchMacroCall(bindings) let rec seekMacros = (expression: t, bindings: T.bindings): result => switch expression {