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
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))",
)
)
})
describe("Parse for Bindings", () => {
testParsePartialToBe(
"x",
"Ok((:$$bindExpression (:$$bindStatement (:$$bindings) :x) (:$exportVariablesExpression)))",
)
)
testParsePartialToBe(
"y=x",
"Ok((:$$bindExpression (:$$bindStatement (:$$bindings) (:$let :y :x)) (:$exportVariablesExpression)))",
)
)
testParsePartialToBe(
"y=x+1",
"Ok((:$$bindExpression (:$$bindStatement (:$$bindings) (:$let :y (:add :x 1))) (:$exportVariablesExpression)))",
)
)
testParsePartialToBe(
"y = x+1; z = y",
"Ok((:$$bindExpression (:$$bindStatement (:$$bindStatement (:$$bindings) (:$let :y (:add :x 1))) (:$let :z :y)) (:$exportVariablesExpression)))",
)
)
})
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)")
})
/*
@ -63,21 +45,16 @@ Only.describe("Eval Partial", () => {
"x",
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.))},
"Ok({x: 1, y: 2})",
)
)
MyOnly.testEvalPartialBindingsToBe(
"y = x+1; z = y",
list{("x", ExpressionValue.EvNumber(1.))},
"Ok({x: 1, y: 2, z: 2})",
)
)
})

View File

@ -1,2 +1,2 @@
module Builtin = Reducer_Dispatch_BuiltIn
module BuiltinMacros = Reducer_Dispatch_BuiltInMacros
module BuiltinMacros = Reducer_Dispatch_BuiltInMacros

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,17 +155,13 @@ 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
}