diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_ternaryOperator_test.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_ternaryOperator_test.res index b04944b9..940ff3c7 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_ternaryOperator_test.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_ternaryOperator_test.res @@ -6,7 +6,7 @@ describe("Parse ternary operator", () => { testParseToBe("2>1 ? 'YES' : 'NO'", "Ok((:$$ternary (:larger 2 1) 'YES' 'NO'))") }) -Skip.describe("Evaluate ternary operator", () => { +describe("Evaluate ternary operator", () => { testEvalToBe("true ? 'YES' : 'NO'", "Ok('YES')") testEvalToBe("false ? 'YES' : 'NO'", "Ok('NO')") }) 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 5facc9e7..7174a5f7 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 @@ -158,6 +158,21 @@ let dispatchMacroCall = ( | _ => replaceSymbols(expression, bindings) } + let doTernary = ( + conditionExpr: expression, + trueExpression: expression, + falseExpression: expression, + bindings: ExpressionT.bindings, + ) => { + let rCondition = conditionExpr->reduceExpression(bindings) + rCondition->Result.flatMap(condition => + switch condition { + | EvBool(true) => trueExpression->Ok + | EvBool(false) => falseExpression->Ok + | _ => RESyntaxError("Boolean value expected for conditional")->Error + } + ) + } switch list { | list{ExpressionT.EValue(EvCall("$$bindings"))} => bindings->ExpressionT.EBindings->Ok @@ -173,6 +188,8 @@ let dispatchMacroCall = ( expression, } => doBindExpression(expression, bindings) + | list{ExpressionT.EValue(EvCall("$$ternary")), conditionExpr, trueExpr, falseExpr} => + doTernary(conditionExpr, trueExpr, falseExpr, bindings) | _ => list->ExpressionT.EList->Ok } }