reducer semantics ternary (tested)

This commit is contained in:
Umur Ozkul 2022-04-25 15:30:27 +02:00
parent 4d801c0b10
commit c810c4576d
2 changed files with 16 additions and 3 deletions

View File

@ -1,9 +1,9 @@
open Jest
open Reducer_TestHelpers
Skip.describe("Parse ternary operator", () => {
testParseToBe("true ? 'YES' : 'NO'", "Ok('YES')")
testParseToBe("false ? 'YES' : 'NO'", "Ok('NO')")
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'))")
})
Skip.describe("Evaluate ternary operator", () => {

View File

@ -116,6 +116,19 @@ let rec fromNode = (mathJsNode: Parse.node): result<expression, errorValue> =>
rExpr
}
| MjBlockNode(bNode) => bNode["blocks"]->Belt.Array.map(toTagOrNode)->caseTagOrNodes
| MjConditionalNode(cndNode) => {
let rCondition = fromNode(cndNode["condition"])
let rTrueExpr = fromNode(cndNode["trueExpr"])
let rFalse = fromNode(cndNode["falseExpr"])
rCondition->Result.flatMap(condition =>
rTrueExpr->Result.flatMap(trueExpr =>
rFalse->Result.flatMap(falseExpr =>
Builder.passToFunction("$$ternary", list{condition, trueExpr, falseExpr})->Ok
)
)
)
}
| MjConstantNode(cNode) =>
cNode["value"]->JavaScript.Gate.jsToEv->Result.flatMap(v => v->ExpressionT.EValue->Ok)
| MjFunctionAssignmentNode(faNode) => caseFunctionAssignmentNode(faNode)