renamed internal variables starting with $ so that they are not overriden by $ variables in Squigle
This commit is contained in:
parent
870b0c9d4e
commit
1557c197a0
|
@ -19,7 +19,7 @@ describe("bindStatement", () => {
|
|||
testMacro(
|
||||
[],
|
||||
eBindStatement(eBindings([]), exampleStatementY),
|
||||
"Ok((:$setBindings {} :y 1) context: {})",
|
||||
"Ok((:$_setBindings_$ {} :y 1) context: {})",
|
||||
)
|
||||
// Then it answers the bindings for the next statement when reduced
|
||||
testMacroEval([], eBindStatement(eBindings([]), exampleStatementY), "Ok({y: 1})")
|
||||
|
@ -27,7 +27,7 @@ describe("bindStatement", () => {
|
|||
testMacro(
|
||||
[],
|
||||
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
|
||||
testMacro([], eBindStatement(eBindings([]), exampleExpression), "Assignment expected")
|
||||
|
@ -35,7 +35,7 @@ describe("bindStatement", () => {
|
|||
testMacro(
|
||||
[("z", EvNumber(99.))],
|
||||
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(
|
||||
[],
|
||||
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
|
||||
testMacroEval(
|
||||
|
@ -71,20 +71,20 @@ describe("block", () => {
|
|||
testMacro([], eBlock(list{exampleExpression}), "Ok((:$$_bindExpression_$$ 1))")
|
||||
testMacroEval([], eBlock(list{exampleExpression}), "Ok(1)")
|
||||
// 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})")
|
||||
// Block with a statement and an expression
|
||||
testMacro(
|
||||
[],
|
||||
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)")
|
||||
// Block with a statement and another statement
|
||||
testMacro(
|
||||
[],
|
||||
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})")
|
||||
// Block inside a block
|
||||
|
@ -98,7 +98,7 @@ describe("block", () => {
|
|||
testMacro(
|
||||
[],
|
||||
eBlock(list{eLetStatement("z", eBlock(list{eBlock(list{exampleExpressionY})}))}),
|
||||
"Ok((:$$_bindExpression_$$ (:$let :z (:$$_block_$$ (:$$_block_$$ :y)))))",
|
||||
"Ok((:$$_bindExpression_$$ (:$_let_$ :z (:$$_block_$$ (:$$_block_$$ :y)))))",
|
||||
)
|
||||
testMacroEval(
|
||||
[],
|
||||
|
@ -107,7 +107,7 @@ describe("block", () => {
|
|||
)
|
||||
// Empty block
|
||||
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(
|
||||
[],
|
||||
eBlock(list{
|
||||
|
@ -116,7 +116,7 @@ describe("block", () => {
|
|||
eSymbol("y"),
|
||||
}),
|
||||
}),
|
||||
"Ok((:$$_bindExpression_$$ (:$$_block_$$ (:$let :y (:add :x 1)) :y)))",
|
||||
"Ok((:$$_bindExpression_$$ (:$$_block_$$ (:$_let_$ :y (:add :x 1)) :y)))",
|
||||
)
|
||||
testMacroEval(
|
||||
[("x", EvNumber(1.))],
|
||||
|
|
|
@ -49,8 +49,8 @@ describe("Peggy parse", () => {
|
|||
testParse("-1", "{(::unaryMinus 1)}")
|
||||
testParse("!true", "{(::not true)}")
|
||||
testParse("1 + -1", "{(::add 1 (::unaryMinus 1))}")
|
||||
testParse("-a[0]", "{(::unaryMinus (::$atIndex :a 0))}")
|
||||
testParse("!a[0]", "{(::not (::$atIndex :a 0))}")
|
||||
testParse("-a[0]", "{(::unaryMinus (::$_atIndex_$ :a 0))}")
|
||||
testParse("!a[0]", "{(::not (::$_atIndex_$ :a 0))}")
|
||||
})
|
||||
|
||||
describe("multiplicative", () => {
|
||||
|
@ -75,7 +75,7 @@ describe("Peggy parse", () => {
|
|||
"1 * 2 - 3 * 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", () => {
|
||||
|
@ -95,23 +95,23 @@ describe("Peggy parse", () => {
|
|||
})
|
||||
|
||||
describe("arrays", () => {
|
||||
testParse("[]", "{(::$constructArray ())}")
|
||||
testParse("[0, 1, 2]", "{(::$constructArray (0 1 2))}")
|
||||
testParse("['hello', 'world']", "{(::$constructArray ('hello' 'world'))}")
|
||||
testParse("([0,1,2])[1]", "{(::$atIndex (::$constructArray (0 1 2)) 1)}")
|
||||
testParse("[]", "{(::$_constructArray_$ ())}")
|
||||
testParse("[0, 1, 2]", "{(::$_constructArray_$ (0 1 2))}")
|
||||
testParse("['hello', 'world']", "{(::$_constructArray_$ ('hello' 'world'))}")
|
||||
testParse("([0,1,2])[1]", "{(::$_atIndex_$ (::$_constructArray_$ (0 1 2)) 1)}")
|
||||
})
|
||||
|
||||
describe("records", () => {
|
||||
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("record.property", "{(::$atIndex :record 'property')}")
|
||||
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("record.property", "{(::$_atIndex_$ :record 'property')}")
|
||||
})
|
||||
|
||||
describe("post 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 (::$atIndex :b 1)))}")
|
||||
testParse("a==!b.one", "{(::equal :a (::not (::$atIndex :b 'one')))}")
|
||||
testParse("a==!b[1]", "{(::equal :a (::not (::$_atIndex_$ :b 1)))}")
|
||||
testParse("a==!b.one", "{(::equal :a (::not (::$_atIndex_$ :b 'one')))}")
|
||||
})
|
||||
|
||||
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 (::larger :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<1+2 || d", "{(::or (::and :a (::smaller :b (::add 1 2))) :d)}")
|
||||
testParse(
|
||||
|
@ -179,7 +179,7 @@ describe("Peggy parse", () => {
|
|||
describe("pipe", () => {
|
||||
testParse("1 -> add(2)", "{(::add 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("1 + 2 -> add(3)", "{(::add 1 (::add 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(
|
||||
"a[1] to a[2]",
|
||||
"{(::credibleIntervalToDistribution (::$atIndex :a 1) (::$atIndex :a 2))}",
|
||||
"{(::credibleIntervalToDistribution (::$_atIndex_$ :a 1) (::$_atIndex_$ :a 2))}",
|
||||
) // lower than post
|
||||
testParse(
|
||||
"a.p1 to a.p2",
|
||||
"{(::credibleIntervalToDistribution (::$atIndex :a 'p1') (::$atIndex :a 'p2'))}",
|
||||
"{(::credibleIntervalToDistribution (::$_atIndex_$ :a 'p1') (::$_atIndex_$ :a 'p2'))}",
|
||||
) // lower than post
|
||||
testParse("1 to 2 + 3", "{(::add (::credibleIntervalToDistribution 1 2) 3)}") // higher than binary operators
|
||||
testParse(
|
||||
|
@ -231,21 +231,21 @@ describe("Peggy parse", () => {
|
|||
)
|
||||
testParse(
|
||||
"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(
|
||||
"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("map(arr, {|x| x+1})", "{(::map :arr {|:x| {(::add :x 1)}})}")
|
||||
testParse(
|
||||
"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(
|
||||
"[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)}})}",
|
||||
)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -53,48 +53,48 @@ describe("Peggy to Expression", () => {
|
|||
testToExpression("-1", "(:$$_block_$$ (:unaryMinus 1))", ~v="-1", ())
|
||||
testToExpression("!true", "(:$$_block_$$ (:not true))", ~v="false", ())
|
||||
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", () => {
|
||||
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(
|
||||
"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}",
|
||||
(),
|
||||
)
|
||||
})
|
||||
|
||||
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 = 1; x", "(:$$_block_$$ (:$let :x (:$$_block_$$ 1)) :x)", ~v="1", ())
|
||||
testToExpression("x = 1; x", "(:$$_block_$$ (:$_let_$ :x (:$$_block_$$ 1)) :x)", ~v="1", ())
|
||||
})
|
||||
|
||||
describe("functions", () => {
|
||||
testToExpression(
|
||||
"identity(x) = x",
|
||||
"(:$$_block_$$ (:$let :identity (:$$_lambda_$$ [x] (:$$_block_$$ :x))))",
|
||||
"(:$$_block_$$ (:$_let_$ :identity (:$$_lambda_$$ [x] (:$$_block_$$ :x))))",
|
||||
~v="{identity: lambda(x=>internal code)}",
|
||||
(),
|
||||
) // Function definitions become lambda assignments
|
||||
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", () => {
|
||||
testToExpression("[]", "(:$$_block_$$ (:$constructArray ()))", ~v="[]", ())
|
||||
testToExpression("[0, 1, 2]", "(:$$_block_$$ (:$constructArray (0 1 2)))", ~v="[0,1,2]", ())
|
||||
testToExpression("[]", "(:$$_block_$$ (:$_constructArray_$ ()))", ~v="[]", ())
|
||||
testToExpression("[0, 1, 2]", "(:$$_block_$$ (:$_constructArray_$ (0 1 2)))", ~v="[0,1,2]", ())
|
||||
testToExpression(
|
||||
"['hello', 'world']",
|
||||
"(:$$_block_$$ (:$constructArray ('hello' 'world')))",
|
||||
"(:$$_block_$$ (:$_constructArray_$ ('hello' 'world')))",
|
||||
~v="['hello','world']",
|
||||
(),
|
||||
)
|
||||
testToExpression(
|
||||
"([0,1,2])[1]",
|
||||
"(:$$_block_$$ (:$atIndex (:$constructArray (0 1 2)) 1))",
|
||||
"(:$$_block_$$ (:$_atIndex_$ (:$_constructArray_$ (0 1 2)) 1))",
|
||||
~v="1",
|
||||
(),
|
||||
)
|
||||
|
@ -103,19 +103,19 @@ describe("Peggy to Expression", () => {
|
|||
describe("records", () => {
|
||||
testToExpression(
|
||||
"{a: 1, b: 2}",
|
||||
"(:$$_block_$$ (:$constructRecord (('a' 1) ('b' 2))))",
|
||||
"(:$$_block_$$ (:$_constructRecord_$ (('a' 1) ('b' 2))))",
|
||||
~v="{a: 1,b: 2}",
|
||||
(),
|
||||
)
|
||||
testToExpression(
|
||||
"{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
|
||||
testToExpression("record.property", "(:$$_block_$$ (:$atIndex :record 'property'))", ())
|
||||
testToExpression("record.property", "(:$$_block_$$ (:$_atIndex_$ :record 'property'))", ())
|
||||
testToExpression(
|
||||
"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",
|
||||
(),
|
||||
)
|
||||
|
@ -180,7 +180,7 @@ describe("Peggy to Expression", () => {
|
|||
// Like lambdas they have a local scope.
|
||||
testToExpression(
|
||||
"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}",
|
||||
(),
|
||||
)
|
||||
|
@ -196,19 +196,19 @@ describe("Peggy to Expression", () => {
|
|||
)
|
||||
testToExpression(
|
||||
"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)}",
|
||||
(),
|
||||
)
|
||||
testToExpression(
|
||||
"f(x)=x",
|
||||
"(:$$_block_$$ (:$let :f (:$$_lambda_$$ [x] (:$$_block_$$ :x))))",
|
||||
"(:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [x] (:$$_block_$$ :x))))",
|
||||
~v="{f: lambda(x=>internal code)}",
|
||||
(),
|
||||
) // Function definitions are lambda assignments
|
||||
testToExpression(
|
||||
"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)}",
|
||||
(),
|
||||
)
|
||||
|
|
|
@ -5,7 +5,7 @@ open Reducer_TestHelpers
|
|||
describe("Eval with Bindings", () => {
|
||||
testEvalBindingsToBe("x", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(1)")
|
||||
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", list{("x", ExpressionValue.EvNumber(1.))}, "Ok({x: 1,y: 2})")
|
||||
})
|
||||
|
|
|
@ -2,8 +2,8 @@ open Jest
|
|||
open Reducer_TestHelpers
|
||||
|
||||
describe("Parse function assignment", () => {
|
||||
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)=x", "Ok((:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [x] (:$$_block_$$ :x)))))")
|
||||
testParseToBe("f(x)=2*x", "Ok((:$$_block_$$ (:$_let_$ :f (:$$_lambda_$$ [x] (:$$_block_$$ (:multiply 2 :x))))))")
|
||||
//MathJs does not allow blocks in function definitions
|
||||
})
|
||||
|
||||
|
|
|
@ -51,7 +51,7 @@ describe("call and bindings", () => {
|
|||
)
|
||||
testParseToBe(
|
||||
"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(x)=x; g(x)=f(x); g(2)", "Ok(2)")
|
||||
|
|
|
@ -130,12 +130,12 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce
|
|||
}
|
||||
|
||||
switch call {
|
||||
| ("$atIndex", [EvArray(aValueArray), EvNumber(fIndex)]) => arrayAtIndex(aValueArray, fIndex)
|
||||
| ("$atIndex", [EvRecord(dict), EvString(sIndex)]) => recordAtIndex(dict, sIndex)
|
||||
| ("$constructArray", [EvArray(aValueArray)]) => EvArray(aValueArray)->Ok
|
||||
| ("$constructRecord", [EvArray(arrayOfPairs)]) => constructRecord(arrayOfPairs)
|
||||
| ("$exportBindings", [EvRecord(externalBindings)]) => doExportBindings(externalBindings)
|
||||
| ("$setBindings", [EvRecord(externalBindings), EvSymbol(symbol), value]) =>
|
||||
| ("$_atIndex_$", [EvArray(aValueArray), EvNumber(fIndex)]) => arrayAtIndex(aValueArray, fIndex)
|
||||
| ("$_atIndex_$", [EvRecord(dict), EvString(sIndex)]) => recordAtIndex(dict, sIndex)
|
||||
| ("$_constructArray_$", [EvArray(aValueArray)]) => EvArray(aValueArray)->Ok
|
||||
| ("$_constructRecord_$", [EvArray(arrayOfPairs)]) => constructRecord(arrayOfPairs)
|
||||
| ("$_exportBindings_$", [EvRecord(externalBindings)]) => doExportBindings(externalBindings)
|
||||
| ("$_setBindings_$", [EvRecord(externalBindings), EvSymbol(symbol), value]) =>
|
||||
doSetBindings(externalBindings, symbol, value)
|
||||
| ("inspect", [value, EvString(label)]) => inspectLabel(value, label)
|
||||
| ("inspect", [value]) => inspect(value)
|
||||
|
|
|
@ -25,7 +25,7 @@ let dispatchMacroCall = (
|
|||
): result<expressionWithContext, errorValue> => {
|
||||
let doBindStatement = (bindingExpr: expression, statement: expression, environment) =>
|
||||
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)
|
||||
|
||||
rExternalBindingsValue->Result.flatMap(externalBindingsValue => {
|
||||
|
@ -34,7 +34,7 @@ let dispatchMacroCall = (
|
|||
// Js.log(
|
||||
// `bindStatement ${Bindings.toString(newBindings)}<==${ExpressionT.toString(
|
||||
// bindingExpr,
|
||||
// )} statement: $let ${ExpressionT.toString(symbolExpr)}=${ExpressionT.toString(
|
||||
// )} statement: $_let_$ ${ExpressionT.toString(symbolExpr)}=${ExpressionT.toString(
|
||||
// statement,
|
||||
// )}`,
|
||||
// )
|
||||
|
@ -43,7 +43,7 @@ let dispatchMacroCall = (
|
|||
rNewStatement->Result.map(newStatement =>
|
||||
ExpressionWithContext.withContext(
|
||||
eFunction(
|
||||
"$setBindings",
|
||||
"$_setBindings_$",
|
||||
list{newBindings->Bindings.toExternalBindings->eRecord, symbolExpr, newStatement},
|
||||
),
|
||||
newBindings,
|
||||
|
@ -59,7 +59,7 @@ let dispatchMacroCall = (
|
|||
errorValue,
|
||||
> =>
|
||||
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)
|
||||
|
||||
rExternalBindingsValue->Result.flatMap(externalBindingsValue => {
|
||||
|
@ -68,10 +68,10 @@ let dispatchMacroCall = (
|
|||
rNewStatement->Result.map(newStatement =>
|
||||
ExpressionWithContext.withContext(
|
||||
eFunction(
|
||||
"$exportBindings",
|
||||
"$_exportBindings_$",
|
||||
list{
|
||||
eFunction(
|
||||
"$setBindings",
|
||||
"$_setBindings_$",
|
||||
list{
|
||||
newBindings->Bindings.toExternalBindings->eRecord,
|
||||
symbolExpr,
|
||||
|
|
|
@ -51,7 +51,7 @@ let eList = (list: list<expression>): expression => list->BExpressionT.EList
|
|||
let eBlock = (exprs: list<expression>): expression => eFunction("$$_block_$$", exprs)
|
||||
|
||||
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 =>
|
||||
eFunction("$$_bindStatement_$$", list{bindingExpr, letStatement})
|
||||
|
|
|
@ -35,9 +35,9 @@
|
|||
}
|
||||
|
||||
var postOperatorToFunction = {
|
||||
'.': '$atIndex',
|
||||
'.': '$_atIndex_$',
|
||||
'()': '$$_applyAll_$$',
|
||||
'[]': '$atIndex',
|
||||
'[]': '$_atIndex_$',
|
||||
}
|
||||
|
||||
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)
|
||||
return nodeLambda(args, nodeBlock(statements)) };
|
||||
var peg$f30 = function(args, finalExpression) { return nodeLambda(args, nodeBlock([finalExpression])) };
|
||||
var peg$f31 = function() { return makeFunctionCall('$constructArray', [nodeExpression([])])};
|
||||
var peg$f32 = function(args) { return makeFunctionCall('$constructArray', [nodeExpression(args)])};
|
||||
var peg$f33 = function(args) { return makeFunctionCall('$constructRecord', [nodeExpression(args)])};
|
||||
var peg$f31 = function() { return makeFunctionCall('$_constructArray_$', [nodeExpression([])])};
|
||||
var peg$f32 = function(args) { return makeFunctionCall('$_constructArray_$', [nodeExpression(args)])};
|
||||
var peg$f33 = function(args) { return makeFunctionCall('$_constructRecord_$', [nodeExpression(args)])};
|
||||
var peg$f34 = function(key, value) { return nodeKeyValue(key, value)};
|
||||
|
||||
var peg$currPos = 0;
|
||||
|
|
|
@ -31,9 +31,9 @@
|
|||
}
|
||||
|
||||
var postOperatorToFunction = {
|
||||
'.': '$atIndex',
|
||||
'.': '$_atIndex_$',
|
||||
'()': '$$_applyAll_$$',
|
||||
'[]': '$atIndex',
|
||||
'[]': '$_atIndex_$',
|
||||
}
|
||||
|
||||
function nodeBlock(statements) {return{type: 'Block', statements: statements}}
|
||||
|
@ -275,9 +275,9 @@ lambda
|
|||
|
||||
arrayConstructor 'array'
|
||||
= '[' _nl ']'
|
||||
{ return makeFunctionCall('$constructArray', [nodeExpression([])])}
|
||||
{ return makeFunctionCall('$_constructArray_$', [nodeExpression([])])}
|
||||
/ '[' _nl args:array_elements _nl ']'
|
||||
{ return makeFunctionCall('$constructArray', [nodeExpression(args)])}
|
||||
{ return makeFunctionCall('$_constructArray_$', [nodeExpression(args)])}
|
||||
|
||||
array_elements
|
||||
= head:expression tail:(_ ',' _nl @expression)*
|
||||
|
@ -285,7 +285,7 @@ arrayConstructor 'array'
|
|||
|
||||
recordConstructor 'record'
|
||||
= '{' _nl args:array_recordArguments _nl '}'
|
||||
{ return makeFunctionCall('$constructRecord', [nodeExpression(args)])}
|
||||
{ return makeFunctionCall('$_constructRecord_$', [nodeExpression(args)])}
|
||||
|
||||
array_recordArguments
|
||||
= head:keyValuePair tail:(_ ',' _nl @keyValuePair)*
|
||||
|
|
Loading…
Reference in New Issue
Block a user