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,55 +2,37 @@ open Jest
open Reducer_TestHelpers open Reducer_TestHelpers
describe("Parse for Bindings", () => { describe("Parse for Bindings", () => {
testParseOuterToBe( testParseOuterToBe("x", "Ok((:$$bindExpression (:$$bindings) :x))")
"x", testParseOuterToBe("x+1", "Ok((:$$bindExpression (:$$bindings) (:add :x 1)))")
"Ok((:$$bindExpression (:$$bindings) :x))",
)
testParseOuterToBe(
"x+1",
"Ok((:$$bindExpression (:$$bindings) (:add :x 1)))",
)
testParseOuterToBe( testParseOuterToBe(
"y = x+1; y", "y = x+1; y",
"Ok((:$$bindExpression (:$$bindStatement (:$$bindings) (:$let :y (:add :x 1))) :y))", "Ok((:$$bindExpression (:$$bindStatement (:$$bindings) (:$let :y (:add :x 1))) :y))",
) )
}) })
describe("Parse for Bindings", () => { describe("Parse for Bindings", () => {
testParsePartialToBe( testParsePartialToBe(
"x", "x",
"Ok((:$$bindExpression (:$$bindStatement (:$$bindings) :x) (:$exportVariablesExpression)))", "Ok((:$$bindExpression (:$$bindStatement (:$$bindings) :x) (:$exportVariablesExpression)))",
) )
testParsePartialToBe( testParsePartialToBe(
"y=x", "y=x",
"Ok((:$$bindExpression (:$$bindStatement (:$$bindings) (:$let :y :x)) (:$exportVariablesExpression)))", "Ok((:$$bindExpression (:$$bindStatement (:$$bindings) (:$let :y :x)) (:$exportVariablesExpression)))",
) )
testParsePartialToBe( testParsePartialToBe(
"y=x+1", "y=x+1",
"Ok((:$$bindExpression (:$$bindStatement (:$$bindings) (:$let :y (:add :x 1))) (:$exportVariablesExpression)))", "Ok((:$$bindExpression (:$$bindStatement (:$$bindings) (:$let :y (:add :x 1))) (:$exportVariablesExpression)))",
) )
testParsePartialToBe( testParsePartialToBe(
"y = x+1; z = y", "y = x+1; z = y",
"Ok((:$$bindExpression (:$$bindStatement (:$$bindStatement (:$$bindings) (:$let :y (:add :x 1))) (:$let :z :y)) (:$exportVariablesExpression)))", "Ok((:$$bindExpression (:$$bindStatement (:$$bindStatement (:$$bindings) (:$let :y (:add :x 1))) (:$let :z :y)) (:$exportVariablesExpression)))",
) )
}) })
describe("Eval with Bindings", () => { describe("Eval with Bindings", () => {
testEvalBindingsToBe( testEvalBindingsToBe("x", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(1)")
"x", testEvalBindingsToBe("x+1", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(2)")
list{("x", ExpressionValue.EvNumber(1.))}, testEvalBindingsToBe("y = x+1; y", list{("x", ExpressionValue.EvNumber(1.))}, "Ok(2)")
"Ok(1)",
)
testEvalBindingsToBe(
"x+1",
list{("x", ExpressionValue.EvNumber(1.))},
"Ok(2)",
)
testEvalBindingsToBe(
"y = x+1; y",
list{("x", ExpressionValue.EvNumber(1.))},
"Ok(2)",
)
}) })
/* /*
@ -63,21 +45,16 @@ Only.describe("Eval Partial", () => {
"x", "x",
list{("x", ExpressionValue.EvNumber(1.))}, list{("x", ExpressionValue.EvNumber(1.))},
"Error(Assignment expected)", "Error(Assignment expected)",
) )
testEvalPartialBindingsToBe( testEvalPartialBindingsToBe("y=x", list{("x", ExpressionValue.EvNumber(1.))}, "Ok({x: 1, y: 1})")
"y=x",
list{("x", ExpressionValue.EvNumber(1.))},
"Ok({x: 1, y: 1})",
)
testEvalPartialBindingsToBe( testEvalPartialBindingsToBe(
"y=x+1", "y=x+1",
list{("x", ExpressionValue.EvNumber(1.))}, list{("x", ExpressionValue.EvNumber(1.))},
"Ok({x: 1, y: 2})", "Ok({x: 1, y: 2})",
) )
MyOnly.testEvalPartialBindingsToBe( MyOnly.testEvalPartialBindingsToBe(
"y = x+1; z = y", "y = x+1; z = y",
list{("x", ExpressionValue.EvNumber(1.))}, list{("x", ExpressionValue.EvNumber(1.))},
"Ok({x: 1, y: 2, z: 2})", "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. Evaluates code with external bindings. External bindings are a record of expression values.
*/ */
let evalWBindings = (code: string, externalBindings: externalBindings) => { let evalWBindings = (code: string, externalBindings: externalBindings) => {
let bindings = externalBindings -> externalBindingsToBindings let bindings = externalBindings->externalBindingsToBindings
evalOuterWBindings_(code, bindings) 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. 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. 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 evalPartialWBindings = (code: string, externalBindings: externalBindings): result<
let bindings = externalBindings -> externalBindingsToBindings externalBindings,
'e,
> => {
let bindings = externalBindings->externalBindingsToBindings
let answer = evalPartialWBindings_(code, bindings) let answer = evalPartialWBindings_(code, bindings)
answer->Result.flatMap(answer => switch answer { answer->Result.flatMap(answer =>
switch answer {
| EvRecord(aRecord) => Ok(aRecord) | EvRecord(aRecord) => Ok(aRecord)
| _ => RETodo("TODO: External bindings must be returned")->Error | _ => 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 => { Parse.castNodeType(mathJsNode)->Result.flatMap(typedMathJsNode => {
let casePartialBlockNode = (bNode: Parse.blockNode) => { let casePartialBlockNode = (bNode: Parse.blockNode) => {
let blocksOrTags = bNode["blocks"]->Belt.Array.map(toTagOrNode) let blocksOrTags = bNode["blocks"]->Belt.Array.map(toTagOrNode)
let completed = Js.Array2.concat( let completed = Js.Array2.concat(blocksOrTags, [BlockTag(ExportVariablesExpression)])
blocksOrTags,
[BlockTag(ExportVariablesExpression)]
)
completed->caseTagOrNodes completed->caseTagOrNodes
} }
let casePartialExpression = (node: Parse.node) => { let casePartialExpression = (node: Parse.node) => {
let completed = let completed = [BlockNode(node), BlockTag(ExportVariablesExpression)]
[BlockNode(node), BlockTag(ExportVariablesExpression)]
completed->caseTagOrNodes completed->caseTagOrNodes
} }