Merge pull request #600 from quantified-uncertainty/issue597

fixed #597
This commit is contained in:
Ozzie Gooen 2022-05-28 11:10:27 -04:00 committed by GitHub
commit 178ec6f89d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 5 deletions

View File

@ -148,6 +148,29 @@ describe("Peggy to Expression", () => {
~v="0", ~v="0",
(), (),
) // nested ternary ) // 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", () => { describe("if then else", () => {

View File

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

View File

@ -22,10 +22,15 @@ let callReducer = (
bindings: bindings, bindings: bindings,
environment: environment, environment: environment,
reducer: reducerFn, reducer: reducerFn,
): result<expressionValue, errorValue> => ): result<expressionValue, errorValue> => {
switch expressionWithContext { switch expressionWithContext {
| ExpressionNoContext(expr) => reducer(expr, bindings, environment) | ExpressionNoContext(expr) =>
| ExpressionWithContext(expr, context) => reducer(expr, context, environment) // 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) let withContext = (expression, context) => ExpressionWithContext(expression, context)