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

View File

@ -116,6 +116,19 @@ let rec fromNode = (mathJsNode: Parse.node): result<expression, errorValue> =>
rExpr rExpr
} }
| MjBlockNode(bNode) => bNode["blocks"]->Belt.Array.map(toTagOrNode)->caseTagOrNodes | 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) => | MjConstantNode(cNode) =>
cNode["value"]->JavaScript.Gate.jsToEv->Result.flatMap(v => v->ExpressionT.EValue->Ok) cNode["value"]->JavaScript.Gate.jsToEv->Result.flatMap(v => v->ExpressionT.EValue->Ok)
| MjFunctionAssignmentNode(faNode) => caseFunctionAssignmentNode(faNode) | MjFunctionAssignmentNode(faNode) => caseFunctionAssignmentNode(faNode)