This commit is contained in:
Umur Ozkul 2022-05-20 18:20:00 +02:00
parent 7e39e11fbd
commit feb0284a89
5 changed files with 21 additions and 7 deletions

View File

@ -131,7 +131,10 @@ describe("Peggy parse", () => {
describe("ternary operator", () => { describe("ternary operator", () => {
testParse("true ? 2 : 3", "{(::$$_ternary_$$ true 2 3)}") testParse("true ? 2 : 3", "{(::$$_ternary_$$ true 2 3)}")
testParse("false ? 2 : false ? 4 : 5", "{(::$$_ternary_$$ false 2 (::$$_ternary_$$ false 4 5))}") // nested ternary testParse(
"false ? 2 : false ? 4 : 5",
"{(::$$_ternary_$$ false 2 (::$$_ternary_$$ false 4 5))}",
) // nested ternary
}) })
describe("if then else", () => { describe("if then else", () => {

View File

@ -80,7 +80,12 @@ describe("Peggy to Expression", () => {
(), (),
) // Function definitions become lambda assignments ) // Function definitions become lambda assignments
testToExpression("identity(x)", "(:$$_block_$$ (:identity :x))", ()) // Note value returns error properly testToExpression("identity(x)", "(:$$_block_$$ (:identity :x))", ()) // Note value returns error properly
testToExpression("f(x) = x> 2 ? 0 : 1; f(3)", "(:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [x] (:$$_block_$$ (:$$_ternary_$$ (:larger :x 2) 0 1)))) (:f 3))", ~v="0", ()) testToExpression(
"f(x) = x> 2 ? 0 : 1; f(3)",
"(:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [x] (:$$_block_$$ (:$$_ternary_$$ (:larger :x 2) 0 1)))) (:f 3))",
~v="0",
(),
)
}) })
describe("arrays", () => { describe("arrays", () => {
@ -184,7 +189,6 @@ describe("Peggy to Expression", () => {
~v="{x: 1,y: 99}", ~v="{x: 1,y: 99}",
(), (),
) )
}) })
describe("lambda", () => { describe("lambda", () => {

View File

@ -3,7 +3,10 @@ open Reducer_TestHelpers
describe("Parse function assignment", () => { describe("Parse function assignment", () => {
testParseToBe("f(x)=x", "Ok((:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [x] (:$$_block_$$ :x)))))") testParseToBe("f(x)=x", "Ok((:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [x] (:$$_block_$$ :x)))))")
testParseToBe("f(x)=2*x", "Ok((:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [x] (:$$_block_$$ (:multiply 2 :x))))))") testParseToBe(
"f(x)=2*x",
"Ok((:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [x] (:$$_block_$$ (:multiply 2 :x))))))",
)
//MathJs does not allow blocks in function definitions //MathJs does not allow blocks in function definitions
}) })

View File

@ -147,7 +147,10 @@ describe("parse on distribution functions", () => {
"normal(5,2) ./ normal(5,1)", "normal(5,2) ./ normal(5,1)",
"Ok((:$$_block_$$ (:dotDivide (:normal 5 2) (:normal 5 1))))", "Ok((:$$_block_$$ (:dotDivide (:normal 5 2) (:normal 5 1))))",
) )
testParse("normal(5,2) .^ normal(5,1)", "Ok((:$$_block_$$ (:dotPow (:normal 5 2) (:normal 5 1))))") testParse(
"normal(5,2) .^ normal(5,1)",
"Ok((:$$_block_$$ (:dotPow (:normal 5 2) (:normal 5 1))))",
)
}) })
describe("equality", () => { describe("equality", () => {
testParse("5 == normal(5,2)", "Ok((:$$_block_$$ (:equal 5 (:normal 5 2))))") testParse("5 == normal(5,2)", "Ok((:$$_block_$$ (:equal 5 (:normal 5 2))))")

View File

@ -174,7 +174,8 @@ let dispatchMacroCall = (
| list{ExpressionT.EValue(EvCall("$$_bindExpression_$$")), expression} => | list{ExpressionT.EValue(EvCall("$$_bindExpression_$$")), expression} =>
// bindings of the context are used when there is no binding expression // bindings of the context are used when there is no binding expression
doBindExpression(eRecord(Bindings.toExternalBindings(bindings)), expression, environment) doBindExpression(eRecord(Bindings.toExternalBindings(bindings)), expression, environment)
| list{ExpressionT.EValue(EvCall("$$_block_$$")), ...exprs} => doBlock(exprs, bindings, environment) | list{ExpressionT.EValue(EvCall("$$_block_$$")), ...exprs} =>
doBlock(exprs, bindings, environment)
| list{ | list{
ExpressionT.EValue(EvCall("$$_lambda_$$")), ExpressionT.EValue(EvCall("$$_lambda_$$")),
ExpressionT.EValue(EvArrayString(parameters)), ExpressionT.EValue(EvArrayString(parameters)),