This commit is contained in:
Umur Ozkul 2022-04-05 20:54:51 +02:00
parent 05e8540b31
commit a5bb390370
4 changed files with 29 additions and 38 deletions

View File

@ -1,18 +1,13 @@
open Jest open Jest
open Reducer_TestHelpers open Reducer_TestHelpers
let testParseToBe = (expr, answer) => test(expr, () => expectParseToBe(expr, answer))
let testParseToBe = (expr, answer) => let testDescParseToBe = (desc, expr, answer) => test(desc, () => expectParseToBe(expr, answer))
test(expr, () => expectParseToBe(expr, answer))
let testDescParseToBe = (desc, expr, answer) => let testEvalToBe = (expr, answer) => test(expr, () => expectEvalToBe(expr, answer))
test(desc, () => expectParseToBe(expr, answer))
let testEvalToBe = (expr, answer) => let testDescEvalToBe = (desc, expr, answer) => test(desc, () => expectEvalToBe(expr, answer))
test(expr, () => expectEvalToBe(expr, answer))
let testDescEvalToBe = (desc, expr, answer) =>
test(desc, () => expectEvalToBe(expr, answer))
describe("reducer using mathjs parse", () => { describe("reducer using mathjs parse", () => {
// Test the MathJs parser compatibility // Test the MathJs parser compatibility
@ -40,11 +35,12 @@ describe("reducer using mathjs parse", () => {
testDescParseToBe("index", "([0,1,2])[1]", "Ok((:$atIndex (0 1 2) (1)))") testDescParseToBe("index", "([0,1,2])[1]", "Ok((:$atIndex (0 1 2) (1)))")
}) })
describe("records", () => { describe("records", () => {
testDescParseToBe("define", testDescParseToBe("define", "{a: 1, b: 2}", "Ok((:$constructRecord (('a' 1) ('b' 2))))")
"{a: 1, b: 2}", "Ok((:$constructRecord (('a' 1) ('b' 2))))") testDescParseToBe(
testDescParseToBe("use", "use",
"{a: 1, b: 2}.a", "{a: 1, b: 2}.a",
"Ok((:$atIndex (:$constructRecord (('a' 1) ('b' 2))) ('a')))") "Ok((:$atIndex (:$constructRecord (('a' 1) ('b' 2))) ('a')))",
)
}) })
}) })
@ -68,8 +64,7 @@ describe("eval", () => {
testEvalToBe("[1, 2, 3]", "Ok([1, 2, 3])") testEvalToBe("[1, 2, 3]", "Ok([1, 2, 3])")
testEvalToBe("['hello', 'world']", "Ok(['hello', 'world'])") testEvalToBe("['hello', 'world']", "Ok(['hello', 'world'])")
testEvalToBe("([0,1,2])[1]", "Ok(1)") testEvalToBe("([0,1,2])[1]", "Ok(1)")
testDescEvalToBe("index not found", testDescEvalToBe("index not found", "([0,1,2])[10]", "Error(Array index not found: 10)")
"([0,1,2])[10]", "Error(Array index not found: 10)")
}) })
describe("records", () => { describe("records", () => {
test("define", () => expectEvalToBe("{a: 1, b: 2}", "Ok({a: 1, b: 2})")) test("define", () => expectEvalToBe("{a: 1, b: 2}", "Ok({a: 1, b: 2})"))
@ -79,8 +74,10 @@ describe("eval", () => {
}) })
describe("test exceptions", () => { describe("test exceptions", () => {
testDescEvalToBe("javascript exception", testDescEvalToBe(
"jsraise('div by 0')", "Error(JS Exception: Error: 'div by 0')") "javascript exception",
testDescEvalToBe("rescript exception", "jsraise('div by 0')",
"resraise()", "Error(TODO: unhandled rescript exception)") "Error(JS Exception: Error: 'div by 0')",
)
testDescEvalToBe("rescript exception", "resraise()", "Error(TODO: unhandled rescript exception)")
}) })

View File

@ -9,20 +9,8 @@ let parse: string => result<expression, Reducer_ErrorValue.t>
module MapString = Belt.Map.String module MapString = Belt.Map.String
type bindings = MapString.t<unit> type bindings = MapString.t<unit>
let defaultBindings: bindings let defaultBindings: bindings
let reduceValueList: list<expressionValue> => result< let reduceValueList: list<expressionValue> => result<expressionValue, Reducer_ErrorValue.t>
expressionValue, let reduceExpression: (expression, 'a) => result<expressionValue, Reducer_ErrorValue.t>
Reducer_ErrorValue.t, let evalWBindingsExpression: (expression, 'a) => result<expressionValue, Reducer_ErrorValue.t>
> let evalWBindings: (string, bindings) => Result.t<expressionValue, Reducer_ErrorValue.t>
let reduceExpression: (expression, 'a) => result<
expressionValue,
Reducer_ErrorValue.t,
>
let evalWBindingsExpression: (expression, 'a) => result<
expressionValue,
Reducer_ErrorValue.t,
>
let evalWBindings: (string, bindings) => Result.t<
expressionValue,
Reducer_ErrorValue.t,
>
let eval: string => Result.t<expressionValue, Reducer_ErrorValue.t> let eval: string => Result.t<expressionValue, Reducer_ErrorValue.t>

View File

@ -54,7 +54,10 @@ let rec fromNode = (mathJsNode: Parse.node): result<expression, errorValue> =>
Ok(list{}), Ok(list{}),
(racc, currentPropertyMathJsNode) => (racc, currentPropertyMathJsNode) =>
racc->Result.flatMap(acc => racc->Result.flatMap(acc =>
fromNode(currentPropertyMathJsNode)->Result.map(propertyCode => list{propertyCode, ...acc}) fromNode(currentPropertyMathJsNode)->Result.map(propertyCode => list{
propertyCode,
...acc,
})
), ),
) )
rpropertyCodeList->Result.map(propertyCodeList => ExtressionT.EList(propertyCodeList)) rpropertyCodeList->Result.map(propertyCodeList => ExtressionT.EList(propertyCodeList))

View File

@ -23,7 +23,10 @@ let rec toString = aValue =>
| EvSymbol(aString) => `:${aString}` | EvSymbol(aString) => `:${aString}`
| EvArray(anArray) => { | EvArray(anArray) => {
let args = let args =
anArray->Belt.Array.map(each => toString(each))->Extra_Array.interperse(", ")->Js.String.concatMany("") anArray
->Belt.Array.map(each => toString(each))
->Extra_Array.interperse(", ")
->Js.String.concatMany("")
`[${args}]` `[${args}]`
} }
| EvRecord(aRecord) => { | EvRecord(aRecord) => {