This commit is contained in:
Umur Ozkul 2022-05-28 15:52:35 +02:00
parent 5caad73586
commit ce5f12360a
3 changed files with 42 additions and 5 deletions

View File

@ -148,6 +148,29 @@ describe("Peggy to Expression", () => {
~v="0",
(),
) // nested ternary
describe("ternary bindings", () => {
testToExpression(
// expression binding
"f(a) = a > 5 ? 1 : 0; f(6)",
"(:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [a] (:$$_block_$$ (:$$_ternary_$$ (:larger :a 5) 1 0)))) (:f 6))",
~v="1",
(),
)
testToExpression(
// when true binding
"f(a) = a > 5 ? a : 0; f(6)",
"(:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [a] (:$$_block_$$ (:$$_ternary_$$ (:larger :a 5) :a 0)))) (:f 6))",
~v="6",
(),
)
testToExpression(
// when false binding
"f(a) = a < 5 ? 1 : a; f(6)",
"(:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [a] (:$$_block_$$ (:$$_ternary_$$ (:smaller :a 5) 1 :a)))) (:f 6))",
~v="6",
(),
)
})
})
describe("if then else", () => {

View File

@ -144,8 +144,14 @@ let dispatchMacroCall = (
let rCondition = reduceExpression(blockCondition, bindings, environment)
rCondition->Result.flatMap(conditionValue =>
switch conditionValue {
| ExpressionValue.EvBool(false) => ExpressionWithContext.noContext(ifFalse)->Ok
| ExpressionValue.EvBool(true) => ExpressionWithContext.noContext(ifTrue)->Ok
| ExpressionValue.EvBool(false) => {
let ifFalseBlock = eBlock(list{ifFalse})
ExpressionWithContext.withContext(ifFalseBlock, bindings)->Ok
}
| ExpressionValue.EvBool(true) => {
let ifTrueBlock = eBlock(list{ifTrue})
ExpressionWithContext.withContext(ifTrueBlock, bindings)->Ok
}
| _ => REExpectedType("Boolean")->Error
}
)

View File

@ -22,10 +22,18 @@ let callReducer = (
bindings: bindings,
environment: environment,
reducer: reducerFn,
): result<expressionValue, errorValue> =>
): result<expressionValue, errorValue> => {
switch expressionWithContext {
| ExpressionNoContext(expr) => reducer(expr, bindings, environment)
| ExpressionWithContext(expr, context) => reducer(expr, context, environment)
| ExpressionNoContext(
expr,
) => // Js.log(`callReducer: bindings ${Bindings.toString(bindings)} expr ${ExpressionT.toString(expr)}`)
reducer(expr, bindings, environment)
| ExpressionWithContext(
expr,
context,
) => // Js.log(`callReducer: context ${Bindings.toString(context)} expr ${ExpressionT.toString(expr)}`)
reducer(expr, context, environment)
}
}
let withContext = (expression, context) => ExpressionWithContext(expression, context)