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 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((:$$block (:$$ternary true 'YES' 'NO')))")
testParseToBe("false ? 'YES' : 'NO'", "Ok('NO')")
}) })
Skip.describe("Evaluate ternary operator", () => { describe("Evaluate ternary operator", () => {
testEvalToBe("true ? 'YES' : 'NO'", "Ok('YES')") testEvalToBe("true ? 'YES' : 'NO'", "Ok('YES')")
testEvalToBe("false ? 'YES' : 'NO'", "Ok('NO')") 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>, argArray: array<expressionValue>,
environment: ExpressionValue.environment, environment: ExpressionValue.environment,
) => { ) => {
Lambda.foreignFunctionInterface( Lambda.foreignFunctionInterface(lambdaValue, argArray, environment, Expression.reduceExpression)
lambdaValue,
argArray,
environment,
Expression.reduceExpression,
)
} }
let defaultEnvironment = ExpressionValue.defaultEnvironment let defaultEnvironment = ExpressionValue.defaultEnvironment

View File

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

View File

@ -132,6 +132,23 @@ let dispatchMacroCall = (
eLambda(parameters, bindings->Bindings.toExternalBindings, lambdaDefinition), eLambda(parameters, bindings->Bindings.toExternalBindings, lambdaDefinition),
)->Ok )->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< let expandExpressionList = (aList, bindings: ExpressionT.bindings, environment): result<
expressionWithContext, expressionWithContext,
errorValue, errorValue,
@ -162,6 +179,8 @@ let dispatchMacroCall = (
lambdaDefinition, lambdaDefinition,
} => } =>
doLambdaDefinition(bindings, parameters, 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 | _ => ExpressionWithContext.noContext(ExpressionT.EList(aList))->Ok
} }

View File

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