This commit is contained in:
Umur Ozkul 2022-04-22 00:02:27 +02:00
parent 5038e2c691
commit 2aa3a5ac48
4 changed files with 28 additions and 50 deletions

View File

@ -2,14 +2,8 @@ open Jest
open Reducer_TestHelpers
describe("Parse for Bindings", () => {
testParseOuterToBe(
"x",
"Ok((:$$bindExpression (:$$bindings) :x))",
)
testParseOuterToBe(
"x+1",
"Ok((:$$bindExpression (:$$bindings) (:add :x 1)))",
)
testParseOuterToBe("x", "Ok((:$$bindExpression (:$$bindings) :x))")
testParseOuterToBe("x+1", "Ok((:$$bindExpression (:$$bindings) (:add :x 1)))")
testParseOuterToBe(
"y = x+1; y",
"Ok((:$$bindExpression (:$$bindStatement (:$$bindings) (:$let :y (:add :x 1))) :y))",
@ -36,21 +30,9 @@ describe("Parse for Bindings", () => {
})
describe("Eval with Bindings", () => {
testEvalBindingsToBe(
"x",
list{("x", ExpressionValue.EvNumber(1.))},
"Ok(1)",
)
testEvalBindingsToBe(
"x+1",
list{("x", ExpressionValue.EvNumber(1.))},
"Ok(2)",
)
testEvalBindingsToBe(
"y = x+1; y",
list{("x", ExpressionValue.EvNumber(1.))},
"Ok(2)",
)
testEvalBindingsToBe("x", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(1)")
testEvalBindingsToBe("x+1", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(2)")
testEvalBindingsToBe("y = x+1; y", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(2)")
})
/*
@ -64,11 +46,7 @@ Only.describe("Eval Partial", () => {
list{("x", ExpressionValue.EvNumber(1.))},
"Error(Assignment expected)",
)
testEvalPartialBindingsToBe(
"y=x",
list{("x", ExpressionValue.EvNumber(1.))},
"Ok({x: 1, y: 1})",
)
testEvalPartialBindingsToBe("y=x", list{("x", ExpressionValue.EvNumber(1.))}, "Ok({x: 1, y: 1})")
testEvalPartialBindingsToBe(
"y=x+1",
list{("x", ExpressionValue.EvNumber(1.))},
@ -80,4 +58,3 @@ Only.describe("Eval Partial", () => {
"Ok({x: 1, y: 2, z: 2})",
)
})

View File

@ -161,7 +161,7 @@ let externalBindingsToBindings = (externalBindings: externalBindings): T.binding
Evaluates code with external bindings. External bindings are a record of expression values.
*/
let evalWBindings = (code: string, externalBindings: externalBindings) => {
let bindings = externalBindings -> externalBindingsToBindings
let bindings = externalBindings->externalBindingsToBindings
evalOuterWBindings_(code, bindings)
}
@ -169,11 +169,16 @@ let evalWBindings = (code: string, externalBindings: externalBindings) => {
Evaluates code with external bindings. External bindings are a record of expression values.
The code is a partial code as if it is cut from a larger code. Therefore all statments are assignments.
*/
let evalPartialWBindings = (code: string, externalBindings: externalBindings): result<externalBindings, 'e> => {
let bindings = externalBindings -> externalBindingsToBindings
let evalPartialWBindings = (code: string, externalBindings: externalBindings): result<
externalBindings,
'e,
> => {
let bindings = externalBindings->externalBindingsToBindings
let answer = evalPartialWBindings_(code, bindings)
answer->Result.flatMap(answer => switch answer {
answer->Result.flatMap(answer =>
switch answer {
| EvRecord(aRecord) => Ok(aRecord)
| _ => RETodo("TODO: External bindings must be returned")->Error
})
}
)
}

View File

@ -155,16 +155,12 @@ let fromPartialNode = (mathJsNode: Parse.node): result<expression, errorValue> =
Parse.castNodeType(mathJsNode)->Result.flatMap(typedMathJsNode => {
let casePartialBlockNode = (bNode: Parse.blockNode) => {
let blocksOrTags = bNode["blocks"]->Belt.Array.map(toTagOrNode)
let completed = Js.Array2.concat(
blocksOrTags,
[BlockTag(ExportVariablesExpression)]
)
let completed = Js.Array2.concat(blocksOrTags, [BlockTag(ExportVariablesExpression)])
completed->caseTagOrNodes
}
let casePartialExpression = (node: Parse.node) => {
let completed =
[BlockNode(node), BlockTag(ExportVariablesExpression)]
let completed = [BlockNode(node), BlockTag(ExportVariablesExpression)]
completed->caseTagOrNodes
}