From ac8827da287ab1d3c88776800f20174eb93145b3 Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Mon, 25 Apr 2022 17:15:28 +0200 Subject: [PATCH] ternary with bindings (failed) --- .../Reducer/Reducer_ternaryOperator_test.res | 9 +++++++++ .../Reducer_Dispatch_BuiltInMacros.res | 12 ++++++++++++ .../Reducer_Expression/Reducer_Expression.res | 6 +----- .../Reducer_Expression_Builder.res | 3 +++ 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_ternaryOperator_test.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_ternaryOperator_test.res index 940ff3c7..79bd870a 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_ternaryOperator_test.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_ternaryOperator_test.res @@ -4,9 +4,18 @@ open Reducer_TestHelpers describe("Parse ternary operator", () => { testParseToBe("true ? 'YES' : 'NO'", "Ok((:$$ternary true 'YES' 'NO'))") testParseToBe("2>1 ? 'YES' : 'NO'", "Ok((:$$ternary (:larger 2 1) 'YES' 'NO'))") + MyOnly.testParseToBe( + "x = true; x ? 'YES' : 'NO' in x", + "Ok((:$$bindExpression (:$$bindStatement (:$$bindings) (:$let :x true)) (:$$ternary :x 'YES' (:multiply (:multiply 'NO' :in) :x))))", + ) }) describe("Evaluate ternary operator", () => { testEvalToBe("true ? 'YES' : 'NO'", "Ok('YES')") + MyOnly.testEvalToBe("x = true; x ? 'YES' : 'NO' in x", "Ok('YES')") testEvalToBe("false ? 'YES' : 'NO'", "Ok('NO')") }) + +describe("Evaluate ternary operator in subscope", () => { + testEvalToBe("x = true ? 'YES' : 'NO' in x", "Ok('YES')") +}) 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 index 7174a5f7..b174eb88 100644 --- 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 @@ -3,6 +3,7 @@ they take expressions as parameters and return a new expression. Macros are used to define language building blocks. They are like Lisp macros. */ +module Builder = Reducer_Expression_Builder module ExpressionT = Reducer_Expression_T module ExpressionValue = ReducerInterface.ExpressionValue module Result = Belt.Result @@ -164,6 +165,17 @@ let dispatchMacroCall = ( falseExpression: expression, bindings: ExpressionT.bindings, ) => { + // let rCondition = conditionExpr->Builder.asBindableExpression->reduceExpression(bindings) + let xString = switch Belt.Map.String.get(bindings, "x") { + | Some(EValue(value)) => value->ExpressionValue.toString + | None => "x Not Found" + } + + let bindingsString= Belt.Map.String.toArray(bindings)->Js.Array2.map( keyValue => { + let (key, ExpressionT.EValue(value)) = keyValue + `${key} = ${value->ExpressionValue.toString}` + })->Js.Array2.toString + Js.log(`ternary; bindings: {${bindingsString}}`) let rCondition = conditionExpr->reduceExpression(bindings) rCondition->Result.flatMap(condition => switch condition { 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 d27ece78..8829b4af 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 @@ -77,11 +77,7 @@ let rec reduceExpression = (expression: t, bindings: T.bindings): result a->Belt.Map.String.set(p, e->T.EValue) ) - let newExpression = Builder.passToFunction( - "$$bindExpression", - list{Builder.passToFunction("$$bindings", list{}), expr}, - ) - reduceExpression(newExpression, bindings) + reduceExpression(Builder.asBindableExpression(expr), bindings) } /* diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Builder.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Builder.res index 4e5a988e..eab60bd2 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Builder.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Builder.res @@ -14,3 +14,6 @@ let passToFunction = (fName: string, lispArgs: list): expression => let toEvSymbolValue = (name: string): expression => name->ExpressionValue.EvSymbol->ExpressionT.EValue + +let asBindableExpression = (expr: expression): expression => + passToFunction("$$bindExpression", list{passToFunction("$$bindings", list{}), expr})