renamed internal variables starting with $ so that they are not overriden by $ variables in Squigle

This commit is contained in:
Umur Ozkul 2022-05-20 17:11:32 +02:00
parent 870b0c9d4e
commit 1557c197a0
11 changed files with 77 additions and 77 deletions

View File

@ -19,7 +19,7 @@ describe("bindStatement", () => {
testMacro( testMacro(
[], [],
eBindStatement(eBindings([]), exampleStatementY), eBindStatement(eBindings([]), exampleStatementY),
"Ok((:$setBindings {} :y 1) context: {})", "Ok((:$_setBindings_$ {} :y 1) context: {})",
) )
// Then it answers the bindings for the next statement when reduced // Then it answers the bindings for the next statement when reduced
testMacroEval([], eBindStatement(eBindings([]), exampleStatementY), "Ok({y: 1})") testMacroEval([], eBindStatement(eBindings([]), exampleStatementY), "Ok({y: 1})")
@ -27,7 +27,7 @@ describe("bindStatement", () => {
testMacro( testMacro(
[], [],
eBindStatement(eBindings([("x", EvNumber(2.))]), exampleStatementX), eBindStatement(eBindings([("x", EvNumber(2.))]), exampleStatementX),
"Ok((:$setBindings {x: 2} :y 2) context: {x: 2})", "Ok((:$_setBindings_$ {x: 2} :y 2) context: {x: 2})",
) )
// An expression does not return a binding, thus error // An expression does not return a binding, thus error
testMacro([], eBindStatement(eBindings([]), exampleExpression), "Assignment expected") testMacro([], eBindStatement(eBindings([]), exampleExpression), "Assignment expected")
@ -35,7 +35,7 @@ describe("bindStatement", () => {
testMacro( testMacro(
[("z", EvNumber(99.))], [("z", EvNumber(99.))],
eBindStatementDefault(exampleStatementY), eBindStatementDefault(exampleStatementY),
"Ok((:$setBindings {z: 99} :y 1) context: {z: 99})", "Ok((:$_setBindings_$ {z: 99} :y 1) context: {z: 99})",
) )
}) })
@ -50,7 +50,7 @@ describe("bindExpression", () => {
testMacro( testMacro(
[], [],
eBindExpression(eBindings([("x", EvNumber(2.))]), exampleStatementY), eBindExpression(eBindings([("x", EvNumber(2.))]), exampleStatementY),
"Ok((:$exportBindings (:$setBindings {x: 2} :y 1)) context: {x: 2})", "Ok((:$_exportBindings_$ (:$_setBindings_$ {x: 2} :y 1)) context: {x: 2})",
) )
// Now let's reduce that expression // Now let's reduce that expression
testMacroEval( testMacroEval(
@ -71,20 +71,20 @@ describe("block", () => {
testMacro([], eBlock(list{exampleExpression}), "Ok((:$$_bindExpression_$$ 1))") testMacro([], eBlock(list{exampleExpression}), "Ok((:$$_bindExpression_$$ 1))")
testMacroEval([], eBlock(list{exampleExpression}), "Ok(1)") testMacroEval([], eBlock(list{exampleExpression}), "Ok(1)")
// Block with a single statement // Block with a single statement
testMacro([], eBlock(list{exampleStatementY}), "Ok((:$$_bindExpression_$$ (:$let :y 1)))") testMacro([], eBlock(list{exampleStatementY}), "Ok((:$$_bindExpression_$$ (:$_let_$ :y 1)))")
testMacroEval([], eBlock(list{exampleStatementY}), "Ok({y: 1})") testMacroEval([], eBlock(list{exampleStatementY}), "Ok({y: 1})")
// Block with a statement and an expression // Block with a statement and an expression
testMacro( testMacro(
[], [],
eBlock(list{exampleStatementY, exampleExpressionY}), eBlock(list{exampleStatementY, exampleExpressionY}),
"Ok((:$$_bindExpression_$$ (:$$_bindStatement_$$ (:$let :y 1)) :y))", "Ok((:$$_bindExpression_$$ (:$$_bindStatement_$$ (:$_let_$ :y 1)) :y))",
) )
testMacroEval([], eBlock(list{exampleStatementY, exampleExpressionY}), "Ok(1)") testMacroEval([], eBlock(list{exampleStatementY, exampleExpressionY}), "Ok(1)")
// Block with a statement and another statement // Block with a statement and another statement
testMacro( testMacro(
[], [],
eBlock(list{exampleStatementY, exampleStatementZ}), eBlock(list{exampleStatementY, exampleStatementZ}),
"Ok((:$$_bindExpression_$$ (:$$_bindStatement_$$ (:$let :y 1)) (:$let :z :y)))", "Ok((:$$_bindExpression_$$ (:$$_bindStatement_$$ (:$_let_$ :y 1)) (:$_let_$ :z :y)))",
) )
testMacroEval([], eBlock(list{exampleStatementY, exampleStatementZ}), "Ok({y: 1,z: 1})") testMacroEval([], eBlock(list{exampleStatementY, exampleStatementZ}), "Ok({y: 1,z: 1})")
// Block inside a block // Block inside a block
@ -98,7 +98,7 @@ describe("block", () => {
testMacro( testMacro(
[], [],
eBlock(list{eLetStatement("z", eBlock(list{eBlock(list{exampleExpressionY})}))}), eBlock(list{eLetStatement("z", eBlock(list{eBlock(list{exampleExpressionY})}))}),
"Ok((:$$_bindExpression_$$ (:$let :z (:$$_block_$$ (:$$_block_$$ :y)))))", "Ok((:$$_bindExpression_$$ (:$_let_$ :z (:$$_block_$$ (:$$_block_$$ :y)))))",
) )
testMacroEval( testMacroEval(
[], [],
@ -107,7 +107,7 @@ describe("block", () => {
) )
// Empty block // Empty block
testMacro([], eBlock(list{}), "Ok(:undefined block)") //TODO: should be an error testMacro([], eBlock(list{}), "Ok(:undefined block)") //TODO: should be an error
// :$$_block_$$ (:$$_block_$$ (:$let :y (:add :x 1)) :y)" // :$$_block_$$ (:$$_block_$$ (:$_let_$ :y (:add :x 1)) :y)"
testMacro( testMacro(
[], [],
eBlock(list{ eBlock(list{
@ -116,7 +116,7 @@ describe("block", () => {
eSymbol("y"), eSymbol("y"),
}), }),
}), }),
"Ok((:$$_bindExpression_$$ (:$$_block_$$ (:$let :y (:add :x 1)) :y)))", "Ok((:$$_bindExpression_$$ (:$$_block_$$ (:$_let_$ :y (:add :x 1)) :y)))",
) )
testMacroEval( testMacroEval(
[("x", EvNumber(1.))], [("x", EvNumber(1.))],

View File

@ -49,8 +49,8 @@ describe("Peggy parse", () => {
testParse("-1", "{(::unaryMinus 1)}") testParse("-1", "{(::unaryMinus 1)}")
testParse("!true", "{(::not true)}") testParse("!true", "{(::not true)}")
testParse("1 + -1", "{(::add 1 (::unaryMinus 1))}") testParse("1 + -1", "{(::add 1 (::unaryMinus 1))}")
testParse("-a[0]", "{(::unaryMinus (::$atIndex :a 0))}") testParse("-a[0]", "{(::unaryMinus (::$_atIndex_$ :a 0))}")
testParse("!a[0]", "{(::not (::$atIndex :a 0))}") testParse("!a[0]", "{(::not (::$_atIndex_$ :a 0))}")
}) })
describe("multiplicative", () => { describe("multiplicative", () => {
@ -75,7 +75,7 @@ describe("Peggy parse", () => {
"1 * 2 - 3 * 4^5^6", "1 * 2 - 3 * 4^5^6",
"{(::subtract (::multiply 1 2) (::multiply 3 (::pow (::pow 4 5) 6)))}", "{(::subtract (::multiply 1 2) (::multiply 3 (::pow (::pow 4 5) 6)))}",
) )
testParse("1 * -a[-2]", "{(::multiply 1 (::unaryMinus (::$atIndex :a (::unaryMinus 2))))}") testParse("1 * -a[-2]", "{(::multiply 1 (::unaryMinus (::$_atIndex_$ :a (::unaryMinus 2))))}")
}) })
describe("multi-line", () => { describe("multi-line", () => {
@ -95,23 +95,23 @@ describe("Peggy parse", () => {
}) })
describe("arrays", () => { describe("arrays", () => {
testParse("[]", "{(::$constructArray ())}") testParse("[]", "{(::$_constructArray_$ ())}")
testParse("[0, 1, 2]", "{(::$constructArray (0 1 2))}") testParse("[0, 1, 2]", "{(::$_constructArray_$ (0 1 2))}")
testParse("['hello', 'world']", "{(::$constructArray ('hello' 'world'))}") testParse("['hello', 'world']", "{(::$_constructArray_$ ('hello' 'world'))}")
testParse("([0,1,2])[1]", "{(::$atIndex (::$constructArray (0 1 2)) 1)}") testParse("([0,1,2])[1]", "{(::$_atIndex_$ (::$_constructArray_$ (0 1 2)) 1)}")
}) })
describe("records", () => { describe("records", () => {
testParse("{a: 1, b: 2}", "{(::$constructRecord ('a': 1 'b': 2))}") testParse("{a: 1, b: 2}", "{(::$_constructRecord_$ ('a': 1 'b': 2))}")
testParse("{1+0: 1, 2+0: 2}", "{(::$constructRecord ((::add 1 0): 1 (::add 2 0): 2))}") // key can be any expression testParse("{1+0: 1, 2+0: 2}", "{(::$_constructRecord_$ ((::add 1 0): 1 (::add 2 0): 2))}") // key can be any expression
testParse("record.property", "{(::$atIndex :record 'property')}") testParse("record.property", "{(::$_atIndex_$ :record 'property')}")
}) })
describe("post operators", () => { describe("post operators", () => {
//function call, array and record access are post operators with higher priority than unary operators //function call, array and record access are post operators with higher priority than unary operators
testParse("a==!b(1)", "{(::equal :a (::not (::b 1)))}") testParse("a==!b(1)", "{(::equal :a (::not (::b 1)))}")
testParse("a==!b[1]", "{(::equal :a (::not (::$atIndex :b 1)))}") testParse("a==!b[1]", "{(::equal :a (::not (::$_atIndex_$ :b 1)))}")
testParse("a==!b.one", "{(::equal :a (::not (::$atIndex :b 'one')))}") testParse("a==!b.one", "{(::equal :a (::not (::$_atIndex_$ :b 'one')))}")
}) })
describe("comments", () => { describe("comments", () => {
@ -158,8 +158,8 @@ describe("Peggy parse", () => {
testParse("a && b<=c || d", "{(::or (::and :a (::smallerEq :b :c)) :d)}") testParse("a && b<=c || d", "{(::or (::and :a (::smallerEq :b :c)) :d)}")
testParse("a && b>c || d", "{(::or (::and :a (::larger :b :c)) :d)}") testParse("a && b>c || d", "{(::or (::and :a (::larger :b :c)) :d)}")
testParse("a && b<c || d", "{(::or (::and :a (::smaller :b :c)) :d)}") testParse("a && b<c || d", "{(::or (::and :a (::smaller :b :c)) :d)}")
testParse("a && b<c[i] || d", "{(::or (::and :a (::smaller :b (::$atIndex :c :i))) :d)}") testParse("a && b<c[i] || d", "{(::or (::and :a (::smaller :b (::$_atIndex_$ :c :i))) :d)}")
testParse("a && b<c.i || d", "{(::or (::and :a (::smaller :b (::$atIndex :c 'i'))) :d)}") testParse("a && b<c.i || d", "{(::or (::and :a (::smaller :b (::$_atIndex_$ :c 'i'))) :d)}")
testParse("a && b<c(i) || d", "{(::or (::and :a (::smaller :b (::c :i))) :d)}") testParse("a && b<c(i) || d", "{(::or (::and :a (::smaller :b (::c :i))) :d)}")
testParse("a && b<1+2 || d", "{(::or (::and :a (::smaller :b (::add 1 2))) :d)}") testParse("a && b<1+2 || d", "{(::or (::and :a (::smaller :b (::add 1 2))) :d)}")
testParse( testParse(
@ -179,7 +179,7 @@ describe("Peggy parse", () => {
describe("pipe", () => { describe("pipe", () => {
testParse("1 -> add(2)", "{(::add 1 2)}") testParse("1 -> add(2)", "{(::add 1 2)}")
testParse("-1 -> add(2)", "{(::add (::unaryMinus 1) 2)}") testParse("-1 -> add(2)", "{(::add (::unaryMinus 1) 2)}")
testParse("-a[1] -> add(2)", "{(::add (::unaryMinus (::$atIndex :a 1)) 2)}") testParse("-a[1] -> add(2)", "{(::add (::unaryMinus (::$_atIndex_$ :a 1)) 2)}")
testParse("-f(1) -> add(2)", "{(::add (::unaryMinus (::f 1)) 2)}") testParse("-f(1) -> add(2)", "{(::add (::unaryMinus (::f 1)) 2)}")
testParse("1 + 2 -> add(3)", "{(::add 1 (::add 2 3))}") testParse("1 + 2 -> add(3)", "{(::add 1 (::add 2 3))}")
testParse("1 -> add(2) * 3", "{(::multiply (::add 1 2) 3)}") testParse("1 -> add(2) * 3", "{(::multiply (::add 1 2) 3)}")
@ -198,11 +198,11 @@ describe("Peggy parse", () => {
testParse("-1 to -2", "{(::credibleIntervalToDistribution (::unaryMinus 1) (::unaryMinus 2))}") // lower than unary testParse("-1 to -2", "{(::credibleIntervalToDistribution (::unaryMinus 1) (::unaryMinus 2))}") // lower than unary
testParse( testParse(
"a[1] to a[2]", "a[1] to a[2]",
"{(::credibleIntervalToDistribution (::$atIndex :a 1) (::$atIndex :a 2))}", "{(::credibleIntervalToDistribution (::$_atIndex_$ :a 1) (::$_atIndex_$ :a 2))}",
) // lower than post ) // lower than post
testParse( testParse(
"a.p1 to a.p2", "a.p1 to a.p2",
"{(::credibleIntervalToDistribution (::$atIndex :a 'p1') (::$atIndex :a 'p2'))}", "{(::credibleIntervalToDistribution (::$_atIndex_$ :a 'p1') (::$_atIndex_$ :a 'p2'))}",
) // lower than post ) // lower than post
testParse("1 to 2 + 3", "{(::add (::credibleIntervalToDistribution 1 2) 3)}") // higher than binary operators testParse("1 to 2 + 3", "{(::add (::credibleIntervalToDistribution 1 2) 3)}") // higher than binary operators
testParse( testParse(
@ -231,21 +231,21 @@ describe("Peggy parse", () => {
) )
testParse( testParse(
"myadd(x,y)=x+y; z=[myadd]; z", "myadd(x,y)=x+y; z=[myadd]; z",
"{:myadd = {|:x,:y| {(::add :x :y)}}; :z = {(::$constructArray (:myadd))}; :z}", "{:myadd = {|:x,:y| {(::add :x :y)}}; :z = {(::$_constructArray_$ (:myadd))}; :z}",
) )
testParse( testParse(
"myaddd(x,y)=x+y; z={x: myaddd}; z", "myaddd(x,y)=x+y; z={x: myaddd}; z",
"{:myaddd = {|:x,:y| {(::add :x :y)}}; :z = {(::$constructRecord ('x': :myaddd))}; :z}", "{:myaddd = {|:x,:y| {(::add :x :y)}}; :z = {(::$_constructRecord_$ ('x': :myaddd))}; :z}",
) )
testParse("f({|x| x+1})", "{(::f {|:x| {(::add :x 1)}})}") testParse("f({|x| x+1})", "{(::f {|:x| {(::add :x 1)}})}")
testParse("map(arr, {|x| x+1})", "{(::map :arr {|:x| {(::add :x 1)}})}") testParse("map(arr, {|x| x+1})", "{(::map :arr {|:x| {(::add :x 1)}})}")
testParse( testParse(
"map([1,2,3], {|x| x+1})", "map([1,2,3], {|x| x+1})",
"{(::map (::$constructArray (1 2 3)) {|:x| {(::add :x 1)}})}", "{(::map (::$_constructArray_$ (1 2 3)) {|:x| {(::add :x 1)}})}",
) )
testParse( testParse(
"[1,2,3]->map({|x| x+1})", "[1,2,3]->map({|x| x+1})",
"{(::map (::$constructArray (1 2 3)) {|:x| {(::add :x 1)}})}", "{(::map (::$_constructArray_$ (1 2 3)) {|:x| {(::add :x 1)}})}",
) )
}) })
}) })

View File

@ -53,48 +53,48 @@ describe("Peggy to Expression", () => {
testToExpression("-1", "(:$$_block_$$ (:unaryMinus 1))", ~v="-1", ()) testToExpression("-1", "(:$$_block_$$ (:unaryMinus 1))", ~v="-1", ())
testToExpression("!true", "(:$$_block_$$ (:not true))", ~v="false", ()) testToExpression("!true", "(:$$_block_$$ (:not true))", ~v="false", ())
testToExpression("1 + -1", "(:$$_block_$$ (:add 1 (:unaryMinus 1)))", ~v="0", ()) testToExpression("1 + -1", "(:$$_block_$$ (:add 1 (:unaryMinus 1)))", ~v="0", ())
testToExpression("-a[0]", "(:$$_block_$$ (:unaryMinus (:$atIndex :a 0)))", ()) testToExpression("-a[0]", "(:$$_block_$$ (:unaryMinus (:$_atIndex_$ :a 0)))", ())
}) })
describe("multi-line", () => { describe("multi-line", () => {
testToExpression("x=1; 2", "(:$$_block_$$ (:$let :x (:$$_block_$$ 1)) 2)", ~v="2", ()) testToExpression("x=1; 2", "(:$$_block_$$ (:$_let_$ :x (:$$_block_$$ 1)) 2)", ~v="2", ())
testToExpression( testToExpression(
"x=1; y=2", "x=1; y=2",
"(:$$_block_$$ (:$let :x (:$$_block_$$ 1)) (:$let :y (:$$_block_$$ 2)))", "(:$$_block_$$ (:$_let_$ :x (:$$_block_$$ 1)) (:$_let_$ :y (:$$_block_$$ 2)))",
~v="{x: 1,y: 2}", ~v="{x: 1,y: 2}",
(), (),
) )
}) })
describe("variables", () => { describe("variables", () => {
testToExpression("x = 1", "(:$$_block_$$ (:$let :x (:$$_block_$$ 1)))", ~v="{x: 1}", ()) testToExpression("x = 1", "(:$$_block_$$ (:$_let_$ :x (:$$_block_$$ 1)))", ~v="{x: 1}", ())
testToExpression("x", "(:$$_block_$$ :x)", ~v=":x", ()) //TODO: value should return error testToExpression("x", "(:$$_block_$$ :x)", ~v=":x", ()) //TODO: value should return error
testToExpression("x = 1; x", "(:$$_block_$$ (:$let :x (:$$_block_$$ 1)) :x)", ~v="1", ()) testToExpression("x = 1; x", "(:$$_block_$$ (:$_let_$ :x (:$$_block_$$ 1)) :x)", ~v="1", ())
}) })
describe("functions", () => { describe("functions", () => {
testToExpression( testToExpression(
"identity(x) = x", "identity(x) = x",
"(:$$_block_$$ (:$let :identity (:$$_lambda_$$ [x] (:$$_block_$$ :x))))", "(:$$_block_$$ (:$_let_$ :identity (:$$_lambda_$$ [x] (:$$_block_$$ :x))))",
~v="{identity: lambda(x=>internal code)}", ~v="{identity: lambda(x=>internal code)}",
(), (),
) // 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", () => {
testToExpression("[]", "(:$$_block_$$ (:$constructArray ()))", ~v="[]", ()) testToExpression("[]", "(:$$_block_$$ (:$_constructArray_$ ()))", ~v="[]", ())
testToExpression("[0, 1, 2]", "(:$$_block_$$ (:$constructArray (0 1 2)))", ~v="[0,1,2]", ()) testToExpression("[0, 1, 2]", "(:$$_block_$$ (:$_constructArray_$ (0 1 2)))", ~v="[0,1,2]", ())
testToExpression( testToExpression(
"['hello', 'world']", "['hello', 'world']",
"(:$$_block_$$ (:$constructArray ('hello' 'world')))", "(:$$_block_$$ (:$_constructArray_$ ('hello' 'world')))",
~v="['hello','world']", ~v="['hello','world']",
(), (),
) )
testToExpression( testToExpression(
"([0,1,2])[1]", "([0,1,2])[1]",
"(:$$_block_$$ (:$atIndex (:$constructArray (0 1 2)) 1))", "(:$$_block_$$ (:$_atIndex_$ (:$_constructArray_$ (0 1 2)) 1))",
~v="1", ~v="1",
(), (),
) )
@ -103,19 +103,19 @@ describe("Peggy to Expression", () => {
describe("records", () => { describe("records", () => {
testToExpression( testToExpression(
"{a: 1, b: 2}", "{a: 1, b: 2}",
"(:$$_block_$$ (:$constructRecord (('a' 1) ('b' 2))))", "(:$$_block_$$ (:$_constructRecord_$ (('a' 1) ('b' 2))))",
~v="{a: 1,b: 2}", ~v="{a: 1,b: 2}",
(), (),
) )
testToExpression( testToExpression(
"{1+0: 1, 2+0: 2}", "{1+0: 1, 2+0: 2}",
"(:$$_block_$$ (:$constructRecord (((:add 1 0) 1) ((:add 2 0) 2))))", "(:$$_block_$$ (:$_constructRecord_$ (((:add 1 0) 1) ((:add 2 0) 2))))",
(), (),
) // key can be any expression ) // key can be any expression
testToExpression("record.property", "(:$$_block_$$ (:$atIndex :record 'property'))", ()) testToExpression("record.property", "(:$$_block_$$ (:$_atIndex_$ :record 'property'))", ())
testToExpression( testToExpression(
"record={property: 1}; record.property", "record={property: 1}; record.property",
"(:$$_block_$$ (:$let :record (:$$_block_$$ (:$constructRecord (('property' 1))))) (:$atIndex :record 'property'))", "(:$$_block_$$ (:$_let_$ :record (:$$_block_$$ (:$_constructRecord_$ (('property' 1))))) (:$_atIndex_$ :record 'property'))",
~v="1", ~v="1",
(), (),
) )
@ -180,7 +180,7 @@ describe("Peggy to Expression", () => {
// Like lambdas they have a local scope. // Like lambdas they have a local scope.
testToExpression( testToExpression(
"y=99; x={y=1; y}", "y=99; x={y=1; y}",
"(:$$_block_$$ (:$let :y (:$$_block_$$ 99)) (:$let :x (:$$_block_$$ (:$let :y (:$$_block_$$ 1)) :y)))", "(:$$_block_$$ (:$_let_$ :y (:$$_block_$$ 99)) (:$_let_$ :x (:$$_block_$$ (:$_let_$ :y (:$$_block_$$ 1)) :y)))",
~v="{x: 1,y: 99}", ~v="{x: 1,y: 99}",
(), (),
) )
@ -196,19 +196,19 @@ describe("Peggy to Expression", () => {
) )
testToExpression( testToExpression(
"f={|x| x}", "f={|x| x}",
"(:$$_block_$$ (:$let :f (:$$_block_$$ (:$$_lambda_$$ [x] (:$$_block_$$ :x)))))", "(:$$_block_$$ (:$_let_$ :f (:$$_block_$$ (:$$_lambda_$$ [x] (:$$_block_$$ :x)))))",
~v="{f: lambda(x=>internal code)}", ~v="{f: lambda(x=>internal code)}",
(), (),
) )
testToExpression( testToExpression(
"f(x)=x", "f(x)=x",
"(:$$_block_$$ (:$let :f (:$$_lambda_$$ [x] (:$$_block_$$ :x))))", "(:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [x] (:$$_block_$$ :x))))",
~v="{f: lambda(x=>internal code)}", ~v="{f: lambda(x=>internal code)}",
(), (),
) // Function definitions are lambda assignments ) // Function definitions are lambda assignments
testToExpression( testToExpression(
"f(x)=x ? 1 : 0", "f(x)=x ? 1 : 0",
"(:$$_block_$$ (:$let :f (:$$_lambda_$$ [x] (:$$_block_$$ (:$$_ternary_$$ :x 1 0)))))", "(:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [x] (:$$_block_$$ (:$$_ternary_$$ :x 1 0)))))",
~v="{f: lambda(x=>internal code)}", ~v="{f: lambda(x=>internal code)}",
(), (),
) )

View File

@ -5,7 +5,7 @@ open Reducer_TestHelpers
describe("Eval with Bindings", () => { describe("Eval with Bindings", () => {
testEvalBindingsToBe("x", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(1)") testEvalBindingsToBe("x", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(1)")
testEvalBindingsToBe("x+1", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(2)") testEvalBindingsToBe("x+1", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(2)")
testParseToBe("y = x+1; y", "Ok((:$$_block_$$ (:$let :y (:$$_block_$$ (:add :x 1))) :y))") testParseToBe("y = x+1; y", "Ok((:$$_block_$$ (:$_let_$ :y (:$$_block_$$ (:add :x 1))) :y))")
testEvalBindingsToBe("y = x+1; y", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(2)") testEvalBindingsToBe("y = x+1; y", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(2)")
testEvalBindingsToBe("y = x+1", list{("x", ExpressionValue.EvNumber(1.))}, "Ok({x: 1,y: 2})") testEvalBindingsToBe("y = x+1", list{("x", ExpressionValue.EvNumber(1.))}, "Ok({x: 1,y: 2})")
}) })

View File

@ -2,8 +2,8 @@ open Jest
open Reducer_TestHelpers 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

@ -51,7 +51,7 @@ describe("call and bindings", () => {
) )
testParseToBe( testParseToBe(
"f=99; g(x)=f; g(2)", "f=99; g(x)=f; g(2)",
"Ok((:$$_block_$$ (:$let :f (:$$_block_$$ 99)) (:$let :g (:$$_lambda_$$ [x] (:$$_block_$$ :f))) (:g 2)))", "Ok((:$$_block_$$ (:$_let_$ :f (:$$_block_$$ 99)) (:$_let_$ :g (:$$_lambda_$$ [x] (:$$_block_$$ :f))) (:g 2)))",
) )
testEvalToBe("f=99; g(x)=f; g(2)", "Ok(99)") testEvalToBe("f=99; g(x)=f; g(2)", "Ok(99)")
testEvalToBe("f(x)=x; g(x)=f(x); g(2)", "Ok(2)") testEvalToBe("f(x)=x; g(x)=f(x); g(2)", "Ok(2)")

View File

@ -130,12 +130,12 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce
} }
switch call { switch call {
| ("$atIndex", [EvArray(aValueArray), EvNumber(fIndex)]) => arrayAtIndex(aValueArray, fIndex) | ("$_atIndex_$", [EvArray(aValueArray), EvNumber(fIndex)]) => arrayAtIndex(aValueArray, fIndex)
| ("$atIndex", [EvRecord(dict), EvString(sIndex)]) => recordAtIndex(dict, sIndex) | ("$_atIndex_$", [EvRecord(dict), EvString(sIndex)]) => recordAtIndex(dict, sIndex)
| ("$constructArray", [EvArray(aValueArray)]) => EvArray(aValueArray)->Ok | ("$_constructArray_$", [EvArray(aValueArray)]) => EvArray(aValueArray)->Ok
| ("$constructRecord", [EvArray(arrayOfPairs)]) => constructRecord(arrayOfPairs) | ("$_constructRecord_$", [EvArray(arrayOfPairs)]) => constructRecord(arrayOfPairs)
| ("$exportBindings", [EvRecord(externalBindings)]) => doExportBindings(externalBindings) | ("$_exportBindings_$", [EvRecord(externalBindings)]) => doExportBindings(externalBindings)
| ("$setBindings", [EvRecord(externalBindings), EvSymbol(symbol), value]) => | ("$_setBindings_$", [EvRecord(externalBindings), EvSymbol(symbol), value]) =>
doSetBindings(externalBindings, symbol, value) doSetBindings(externalBindings, symbol, value)
| ("inspect", [value, EvString(label)]) => inspectLabel(value, label) | ("inspect", [value, EvString(label)]) => inspectLabel(value, label)
| ("inspect", [value]) => inspect(value) | ("inspect", [value]) => inspect(value)

View File

@ -25,7 +25,7 @@ let dispatchMacroCall = (
): result<expressionWithContext, errorValue> => { ): result<expressionWithContext, errorValue> => {
let doBindStatement = (bindingExpr: expression, statement: expression, environment) => let doBindStatement = (bindingExpr: expression, statement: expression, environment) =>
switch statement { switch statement {
| ExpressionT.EList(list{ExpressionT.EValue(EvCall("$let")), symbolExpr, statement}) => { | ExpressionT.EList(list{ExpressionT.EValue(EvCall("$_let_$")), symbolExpr, statement}) => {
let rExternalBindingsValue = reduceExpression(bindingExpr, bindings, environment) let rExternalBindingsValue = reduceExpression(bindingExpr, bindings, environment)
rExternalBindingsValue->Result.flatMap(externalBindingsValue => { rExternalBindingsValue->Result.flatMap(externalBindingsValue => {
@ -34,7 +34,7 @@ let dispatchMacroCall = (
// Js.log( // Js.log(
// `bindStatement ${Bindings.toString(newBindings)}<==${ExpressionT.toString( // `bindStatement ${Bindings.toString(newBindings)}<==${ExpressionT.toString(
// bindingExpr, // bindingExpr,
// )} statement: $let ${ExpressionT.toString(symbolExpr)}=${ExpressionT.toString( // )} statement: $_let_$ ${ExpressionT.toString(symbolExpr)}=${ExpressionT.toString(
// statement, // statement,
// )}`, // )}`,
// ) // )
@ -43,7 +43,7 @@ let dispatchMacroCall = (
rNewStatement->Result.map(newStatement => rNewStatement->Result.map(newStatement =>
ExpressionWithContext.withContext( ExpressionWithContext.withContext(
eFunction( eFunction(
"$setBindings", "$_setBindings_$",
list{newBindings->Bindings.toExternalBindings->eRecord, symbolExpr, newStatement}, list{newBindings->Bindings.toExternalBindings->eRecord, symbolExpr, newStatement},
), ),
newBindings, newBindings,
@ -59,7 +59,7 @@ let dispatchMacroCall = (
errorValue, errorValue,
> => > =>
switch statement { switch statement {
| ExpressionT.EList(list{ExpressionT.EValue(EvCall("$let")), symbolExpr, statement}) => { | ExpressionT.EList(list{ExpressionT.EValue(EvCall("$_let_$")), symbolExpr, statement}) => {
let rExternalBindingsValue = reduceExpression(bindingExpr, bindings, environment) let rExternalBindingsValue = reduceExpression(bindingExpr, bindings, environment)
rExternalBindingsValue->Result.flatMap(externalBindingsValue => { rExternalBindingsValue->Result.flatMap(externalBindingsValue => {
@ -68,10 +68,10 @@ let dispatchMacroCall = (
rNewStatement->Result.map(newStatement => rNewStatement->Result.map(newStatement =>
ExpressionWithContext.withContext( ExpressionWithContext.withContext(
eFunction( eFunction(
"$exportBindings", "$_exportBindings_$",
list{ list{
eFunction( eFunction(
"$setBindings", "$_setBindings_$",
list{ list{
newBindings->Bindings.toExternalBindings->eRecord, newBindings->Bindings.toExternalBindings->eRecord,
symbolExpr, symbolExpr,

View File

@ -51,7 +51,7 @@ let eList = (list: list<expression>): expression => list->BExpressionT.EList
let eBlock = (exprs: list<expression>): expression => eFunction("$$_block_$$", exprs) let eBlock = (exprs: list<expression>): expression => eFunction("$$_block_$$", exprs)
let eLetStatement = (symbol: string, valueExpression: expression): expression => let eLetStatement = (symbol: string, valueExpression: expression): expression =>
eFunction("$let", list{eSymbol(symbol), valueExpression}) eFunction("$_let_$", list{eSymbol(symbol), valueExpression})
let eBindStatement = (bindingExpr: expression, letStatement: expression): expression => let eBindStatement = (bindingExpr: expression, letStatement: expression): expression =>
eFunction("$$_bindStatement_$$", list{bindingExpr, letStatement}) eFunction("$$_bindStatement_$$", list{bindingExpr, letStatement})

View File

@ -35,9 +35,9 @@
} }
var postOperatorToFunction = { var postOperatorToFunction = {
'.': '$atIndex', '.': '$_atIndex_$',
'()': '$$_applyAll_$$', '()': '$$_applyAll_$$',
'[]': '$atIndex', '[]': '$_atIndex_$',
} }
function nodeBlock(statements) {return{type: 'Block', statements: statements}} function nodeBlock(statements) {return{type: 'Block', statements: statements}}
@ -405,9 +405,9 @@ function peg$parse(input, options) {
var peg$f29 = function(args, statements, finalExpression) { statements.push(finalExpression) var peg$f29 = function(args, statements, finalExpression) { statements.push(finalExpression)
return nodeLambda(args, nodeBlock(statements)) }; return nodeLambda(args, nodeBlock(statements)) };
var peg$f30 = function(args, finalExpression) { return nodeLambda(args, nodeBlock([finalExpression])) }; var peg$f30 = function(args, finalExpression) { return nodeLambda(args, nodeBlock([finalExpression])) };
var peg$f31 = function() { return makeFunctionCall('$constructArray', [nodeExpression([])])}; var peg$f31 = function() { return makeFunctionCall('$_constructArray_$', [nodeExpression([])])};
var peg$f32 = function(args) { return makeFunctionCall('$constructArray', [nodeExpression(args)])}; var peg$f32 = function(args) { return makeFunctionCall('$_constructArray_$', [nodeExpression(args)])};
var peg$f33 = function(args) { return makeFunctionCall('$constructRecord', [nodeExpression(args)])}; var peg$f33 = function(args) { return makeFunctionCall('$_constructRecord_$', [nodeExpression(args)])};
var peg$f34 = function(key, value) { return nodeKeyValue(key, value)}; var peg$f34 = function(key, value) { return nodeKeyValue(key, value)};
var peg$currPos = 0; var peg$currPos = 0;

View File

@ -31,9 +31,9 @@
} }
var postOperatorToFunction = { var postOperatorToFunction = {
'.': '$atIndex', '.': '$_atIndex_$',
'()': '$$_applyAll_$$', '()': '$$_applyAll_$$',
'[]': '$atIndex', '[]': '$_atIndex_$',
} }
function nodeBlock(statements) {return{type: 'Block', statements: statements}} function nodeBlock(statements) {return{type: 'Block', statements: statements}}
@ -275,9 +275,9 @@ lambda
arrayConstructor 'array' arrayConstructor 'array'
= '[' _nl ']' = '[' _nl ']'
{ return makeFunctionCall('$constructArray', [nodeExpression([])])} { return makeFunctionCall('$_constructArray_$', [nodeExpression([])])}
/ '[' _nl args:array_elements _nl ']' / '[' _nl args:array_elements _nl ']'
{ return makeFunctionCall('$constructArray', [nodeExpression(args)])} { return makeFunctionCall('$_constructArray_$', [nodeExpression(args)])}
array_elements array_elements
= head:expression tail:(_ ',' _nl @expression)* = head:expression tail:(_ ',' _nl @expression)*
@ -285,7 +285,7 @@ arrayConstructor 'array'
recordConstructor 'record' recordConstructor 'record'
= '{' _nl args:array_recordArguments _nl '}' = '{' _nl args:array_recordArguments _nl '}'
{ return makeFunctionCall('$constructRecord', [nodeExpression(args)])} { return makeFunctionCall('$_constructRecord_$', [nodeExpression(args)])}
array_recordArguments array_recordArguments
= head:keyValuePair tail:(_ ',' _nl @keyValuePair)* = head:keyValuePair tail:(_ ',' _nl @keyValuePair)*