ternary operator (tested)

This commit is contained in:
Umur Ozkul 2022-05-04 22:36:34 +02:00
parent 888b615445
commit 99f54f596c
5 changed files with 28 additions and 11 deletions

View File

@ -1,12 +1,14 @@
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((:$$block (:$$ternary true 'YES' 'NO')))")
})
Skip.describe("Evaluate ternary operator", () => {
describe("Evaluate ternary operator", () => {
testEvalToBe("true ? 'YES' : 'NO'", "Ok('YES')")
testEvalToBe("false ? 'YES' : 'NO'", "Ok('NO')")
testEvalToBe("2 > 1 ? 'YES' : 'NO'", "Ok('YES')")
testEvalToBe("2 <= 1 ? 'YES' : 'NO'", "Ok('NO')")
testEvalToBe("1+1 ? 'YES' : 'NO'", "Error(Expected type: Boolean)")
})

View File

@ -19,12 +19,7 @@ let foreignFunctionInterface = (
argArray: array<expressionValue>,
environment: ExpressionValue.environment,
) => {
Lambda.foreignFunctionInterface(
lambdaValue,
argArray,
environment,
Expression.reduceExpression,
)
Lambda.foreignFunctionInterface(lambdaValue, argArray, environment, Expression.reduceExpression)
}
let defaultEnvironment = ExpressionValue.defaultEnvironment

View File

@ -12,7 +12,6 @@ type externalBindings = ReducerInterface_ExpressionValue.externalBindings
@genType
type lambdaValue = ReducerInterface_ExpressionValue.lambdaValue
@genType
let evaluateUsingOptions: (
~environment: option<QuriSquiggleLang.ReducerInterface_ExpressionValue.environment>,

View File

@ -132,6 +132,23 @@ let dispatchMacroCall = (
eLambda(parameters, bindings->Bindings.toExternalBindings, lambdaDefinition),
)->Ok
let doTernary = (
condition: expression,
ifTrue: expression,
ifFalse: expression,
bindings: ExpressionT.bindings,
environment,
): result<expressionWithContext, errorValue> => {
let rCondition = reduceExpression(condition, bindings, environment)
rCondition->Result.flatMap(conditionValue =>
switch conditionValue {
| ExpressionValue.EvBool(false) => ExpressionWithContext.noContext(ifFalse)->Ok
| ExpressionValue.EvBool(true) => ExpressionWithContext.noContext(ifTrue)->Ok
| _ => REExpectedType("Boolean")->Error
}
)
}
let expandExpressionList = (aList, bindings: ExpressionT.bindings, environment): result<
expressionWithContext,
errorValue,
@ -162,6 +179,8 @@ let dispatchMacroCall = (
lambdaDefinition,
} =>
doLambdaDefinition(bindings, parameters, lambdaDefinition)
| list{ExpressionT.EValue(EvCall("$$ternary")), condition, ifTrue, ifFalse} =>
doTernary(condition, ifTrue, ifFalse, bindings, environment)
| _ => ExpressionWithContext.noContext(ExpressionT.EList(aList))->Ok
}

View File

@ -13,6 +13,7 @@ type errorValue =
| RESymbolNotFound(string)
| RESyntaxError(string)
| RETodo(string) // To do
| REExpectedType(string)
type t = errorValue
@ -46,4 +47,5 @@ let errorToString = err =>
| RESymbolNotFound(symbolName) => `${symbolName} is not defined`
| RESyntaxError(desc) => `Syntax Error: ${desc}`
| RETodo(msg) => `TODO: ${msg}`
| REExpectedType(typeName) => `Expected type: ${typeName}`
}