ternary with bindings (failed)

This commit is contained in:
Umur Ozkul 2022-04-25 17:15:28 +02:00
parent cbc2b73b20
commit ac8827da28
4 changed files with 25 additions and 5 deletions

View File

@ -4,9 +4,18 @@ open Reducer_TestHelpers
describe("Parse ternary operator", () => { describe("Parse ternary operator", () => {
testParseToBe("true ? 'YES' : 'NO'", "Ok((:$$ternary true 'YES' 'NO'))") testParseToBe("true ? 'YES' : 'NO'", "Ok((:$$ternary true 'YES' 'NO'))")
testParseToBe("2>1 ? 'YES' : 'NO'", "Ok((:$$ternary (:larger 2 1) 'YES' 'NO'))") testParseToBe("2>1 ? 'YES' : 'NO'", "Ok((:$$ternary (:larger 2 1) 'YES' 'NO'))")
MyOnly.testParseToBe(
"x = true; x ? 'YES' : 'NO' in x",
"Ok((:$$bindExpression (:$$bindStatement (:$$bindings) (:$let :x true)) (:$$ternary :x 'YES' (:multiply (:multiply 'NO' :in) :x))))",
)
}) })
describe("Evaluate ternary operator", () => { describe("Evaluate ternary operator", () => {
testEvalToBe("true ? 'YES' : 'NO'", "Ok('YES')") testEvalToBe("true ? 'YES' : 'NO'", "Ok('YES')")
MyOnly.testEvalToBe("x = true; x ? 'YES' : 'NO' in x", "Ok('YES')")
testEvalToBe("false ? 'YES' : 'NO'", "Ok('NO')") testEvalToBe("false ? 'YES' : 'NO'", "Ok('NO')")
}) })
describe("Evaluate ternary operator in subscope", () => {
testEvalToBe("x = true ? 'YES' : 'NO' in x", "Ok('YES')")
})

View File

@ -3,6 +3,7 @@
they take expressions as parameters and return a new expression. they take expressions as parameters and return a new expression.
Macros are used to define language building blocks. They are like Lisp macros. Macros are used to define language building blocks. They are like Lisp macros.
*/ */
module Builder = Reducer_Expression_Builder
module ExpressionT = Reducer_Expression_T module ExpressionT = Reducer_Expression_T
module ExpressionValue = ReducerInterface.ExpressionValue module ExpressionValue = ReducerInterface.ExpressionValue
module Result = Belt.Result module Result = Belt.Result
@ -164,6 +165,17 @@ let dispatchMacroCall = (
falseExpression: expression, falseExpression: expression,
bindings: ExpressionT.bindings, bindings: ExpressionT.bindings,
) => { ) => {
// let rCondition = conditionExpr->Builder.asBindableExpression->reduceExpression(bindings)
let xString = switch Belt.Map.String.get(bindings, "x") {
| Some(EValue(value)) => value->ExpressionValue.toString
| None => "x Not Found"
}
let bindingsString= Belt.Map.String.toArray(bindings)->Js.Array2.map( keyValue => {
let (key, ExpressionT.EValue(value)) = keyValue
`${key} = ${value->ExpressionValue.toString}`
})->Js.Array2.toString
Js.log(`ternary; bindings: {${bindingsString}}`)
let rCondition = conditionExpr->reduceExpression(bindings) let rCondition = conditionExpr->reduceExpression(bindings)
rCondition->Result.flatMap(condition => rCondition->Result.flatMap(condition =>
switch condition { switch condition {

View File

@ -77,11 +77,7 @@ let rec reduceExpression = (expression: t, bindings: T.bindings): result<express
let bindings = Belt.List.reduce(zippedParameterList, defaultBindings, (a, (p, e)) => let bindings = Belt.List.reduce(zippedParameterList, defaultBindings, (a, (p, e)) =>
a->Belt.Map.String.set(p, e->T.EValue) a->Belt.Map.String.set(p, e->T.EValue)
) )
let newExpression = Builder.passToFunction( reduceExpression(Builder.asBindableExpression(expr), bindings)
"$$bindExpression",
list{Builder.passToFunction("$$bindings", list{}), expr},
)
reduceExpression(newExpression, bindings)
} }
/* /*

View File

@ -14,3 +14,6 @@ let passToFunction = (fName: string, lispArgs: list<expression>): expression =>
let toEvSymbolValue = (name: string): expression => let toEvSymbolValue = (name: string): expression =>
name->ExpressionValue.EvSymbol->ExpressionT.EValue name->ExpressionValue.EvSymbol->ExpressionT.EValue
let asBindableExpression = (expr: expression): expression =>
passToFunction("$$bindExpression", list{passToFunction("$$bindings", list{}), expr})