PR#107 show -> toString
This commit is contained in:
parent
12113cad7e
commit
17ca080ebb
|
@ -4,7 +4,7 @@ open Jest
|
||||||
open Expect
|
open Expect
|
||||||
|
|
||||||
let expectEvalToBe = (expr: string, answer: string) =>
|
let expectEvalToBe = (expr: string, answer: string) =>
|
||||||
Reducer.eval(expr)->ExpressionValue.showResult->expect->toBe(answer)
|
Reducer.eval(expr)->ExpressionValue.toStringResult->expect->toBe(answer)
|
||||||
|
|
||||||
describe("builtin", () => {
|
describe("builtin", () => {
|
||||||
// All MathJs operators and functions are available for string, number and boolean
|
// All MathJs operators and functions are available for string, number and boolean
|
||||||
|
|
|
@ -5,7 +5,7 @@ open Jest
|
||||||
open Expect
|
open Expect
|
||||||
|
|
||||||
let expectParseToBe = (expr, answer) =>
|
let expectParseToBe = (expr, answer) =>
|
||||||
Parse.parse(expr)->Result.flatMap(Parse.castNodeType)->Parse.showResult->expect->toBe(answer)
|
Parse.parse(expr)->Result.flatMap(Parse.castNodeType)->Parse.toStringResult->expect->toBe(answer)
|
||||||
|
|
||||||
describe("MathJs parse", () => {
|
describe("MathJs parse", () => {
|
||||||
describe("literals operators paranthesis", () => {
|
describe("literals operators paranthesis", () => {
|
||||||
|
|
|
@ -5,10 +5,10 @@ open Jest
|
||||||
open Expect
|
open Expect
|
||||||
|
|
||||||
let expectParseToBe = (expr: string, answer: string) =>
|
let expectParseToBe = (expr: string, answer: string) =>
|
||||||
Reducer.parse(expr)->Expression.showResult->expect->toBe(answer)
|
Reducer.parse(expr)->Expression.toStringResult->expect->toBe(answer)
|
||||||
|
|
||||||
let expectEvalToBe = (expr: string, answer: string) =>
|
let expectEvalToBe = (expr: string, answer: string) =>
|
||||||
Reducer.eval(expr)->ExpressionValue.showResult->expect->toBe(answer)
|
Reducer.eval(expr)->ExpressionValue.toStringResult->expect->toBe(answer)
|
||||||
|
|
||||||
// Current configuration does not ignore this file so we have to have a test
|
// Current configuration does not ignore this file so we have to have a test
|
||||||
test("test helpers", () => expect(1)->toBe(1))
|
test("test helpers", () => expect(1)->toBe(1))
|
||||||
|
|
|
@ -3,11 +3,11 @@ open Reducer_TestHelpers
|
||||||
|
|
||||||
describe("reducer using mathjs parse", () => {
|
describe("reducer using mathjs parse", () => {
|
||||||
// Test the MathJs parser compatibility
|
// Test the MathJs parser compatibility
|
||||||
// Those tests show that there is a semantic mapping from MathJs to Expression
|
// Those tests toString that there is a semantic mapping from MathJs to Expression
|
||||||
// Reducer.parse is called by Reducer.eval
|
// Reducer.parse is called by Reducer.eval
|
||||||
// See https://mathjs.org/docs/expressions/syntax.html
|
// See https://mathjs.org/docs/expressions/syntax.html
|
||||||
// See https://mathjs.org/docs/reference/functions.html
|
// See https://mathjs.org/docs/reference/functions.html
|
||||||
// Those tests show that we are converting mathjs parse tree to what we need
|
// Those tests toString that we are converting mathjs parse tree to what we need
|
||||||
|
|
||||||
describe("expressions", () => {
|
describe("expressions", () => {
|
||||||
test("1", () => expectParseToBe("1", "Ok(1)"))
|
test("1", () => expectParseToBe("1", "Ok(1)"))
|
||||||
|
|
|
@ -3,9 +3,9 @@ open Jest
|
||||||
open Expect
|
open Expect
|
||||||
|
|
||||||
describe("ExpressionValue", () => {
|
describe("ExpressionValue", () => {
|
||||||
test("showArgs", () => expect([EvNumber(1.), EvString("a")]->showArgs)->toBe("1, 'a'"))
|
test("argsToString", () => expect([EvNumber(1.), EvString("a")]->argsToString)->toBe("1, 'a'"))
|
||||||
|
|
||||||
test("showFunctionCall", () =>
|
test("toStringFunctionCall", () =>
|
||||||
expect(("fn", [EvNumber(1.), EvString("a")])->showFunctionCall)->toBe("fn(1, 'a')")
|
expect(("fn", [EvNumber(1.), EvString("a")])->toStringFunctionCall)->toBe("fn(1, 'a')")
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
|
@ -14,16 +14,16 @@ exception TestRescriptException
|
||||||
let callInternal = (call: functionCall): result<'b, errorValue> => {
|
let callInternal = (call: functionCall): result<'b, errorValue> => {
|
||||||
let callMathJs = (call: functionCall): result<'b, errorValue> =>
|
let callMathJs = (call: functionCall): result<'b, errorValue> =>
|
||||||
switch call {
|
switch call {
|
||||||
| ("jsraise", [msg]) => Js.Exn.raiseError(show(msg)) // For Tests
|
| ("jsraise", [msg]) => Js.Exn.raiseError(toString(msg)) // For Tests
|
||||||
| ("resraise", _) => raise(TestRescriptException) // For Tests
|
| ("resraise", _) => raise(TestRescriptException) // For Tests
|
||||||
| call => call->showFunctionCall->MathJs.Eval.eval
|
| call => call->toStringFunctionCall->MathJs.Eval.eval
|
||||||
}
|
}
|
||||||
|
|
||||||
let constructRecord = arrayOfPairs => {
|
let constructRecord = arrayOfPairs => {
|
||||||
Belt.Array.map(arrayOfPairs, pairValue => {
|
Belt.Array.map(arrayOfPairs, pairValue => {
|
||||||
switch pairValue {
|
switch pairValue {
|
||||||
| EvArray([EvString(key), valueValue]) => (key, valueValue)
|
| EvArray([EvString(key), valueValue]) => (key, valueValue)
|
||||||
| _ => ("wrong key type", pairValue->showWithType->EvString)
|
| _ => ("wrong key type", pairValue->toStringWithType->EvString)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
->Js.Dict.fromArray
|
->Js.Dict.fromArray
|
||||||
|
@ -52,7 +52,7 @@ let callInternal = (call: functionCall): result<'b, errorValue> => {
|
||||||
arrayAtIndex(aValueArray, fIndex)
|
arrayAtIndex(aValueArray, fIndex)
|
||||||
| ("$atIndex", [EvRecord(dict), EvArray([EvString(sIndex)])]) => recordAtIndex(dict, sIndex)
|
| ("$atIndex", [EvRecord(dict), EvArray([EvString(sIndex)])]) => recordAtIndex(dict, sIndex)
|
||||||
| ("$atIndex", [obj, index]) =>
|
| ("$atIndex", [obj, index]) =>
|
||||||
(showWithType(obj) ++ "??~~~~" ++ showWithType(index))->EvString->Ok
|
(toStringWithType(obj) ++ "??~~~~" ++ toStringWithType(index))->EvString->Ok
|
||||||
| call => callMathJs(call)
|
| call => callMathJs(call)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ type errorValue =
|
||||||
|
|
||||||
type t = errorValue
|
type t = errorValue
|
||||||
|
|
||||||
let showError = err =>
|
let errorToString = err =>
|
||||||
switch err {
|
switch err {
|
||||||
| REArrayIndexNotFound(msg, index) => `${msg}: ${Js.String.make(index)}`
|
| REArrayIndexNotFound(msg, index) => `${msg}: ${Js.String.make(index)}`
|
||||||
| REFunctionExpected(msg) => `Function expected: ${msg}`
|
| REFunctionExpected(msg) => `Function expected: ${msg}`
|
||||||
|
|
|
@ -13,19 +13,19 @@ type t = expression
|
||||||
/*
|
/*
|
||||||
Shows the Lisp Code as text lisp code
|
Shows the Lisp Code as text lisp code
|
||||||
*/
|
*/
|
||||||
let rec show = expression =>
|
let rec toString = expression =>
|
||||||
switch expression {
|
switch expression {
|
||||||
| T.EList(aList) =>
|
| T.EList(aList) =>
|
||||||
`(${Belt.List.map(aList, aValue => show(aValue))
|
`(${Belt.List.map(aList, aValue => toString(aValue))
|
||||||
->Extra.List.interperse(" ")
|
->Extra.List.interperse(" ")
|
||||||
->Belt.List.toArray
|
->Belt.List.toArray
|
||||||
->Js.String.concatMany("")})`
|
->Js.String.concatMany("")})`
|
||||||
| EValue(aValue) => ExpressionValue.show(aValue)
|
| EValue(aValue) => ExpressionValue.toString(aValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
let showResult = codeResult =>
|
let toStringResult = codeResult =>
|
||||||
switch codeResult {
|
switch codeResult {
|
||||||
| Ok(a) => `Ok(${show(a)})`
|
| Ok(a) => `Ok(${toString(a)})`
|
||||||
| Error(m) => `Error(${Js.String.make(m)})`
|
| Error(m) => `Error(${Js.String.make(m)})`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,8 @@ module T = Reducer_Expression_T
|
||||||
type expression = T.expression
|
type expression = T.expression
|
||||||
type expressionValue = ReducerInterface.ExpressionValue.expressionValue
|
type expressionValue = ReducerInterface.ExpressionValue.expressionValue
|
||||||
type t = expression
|
type t = expression
|
||||||
let show: T.expression => Js.String.t
|
let toString: T.expression => Js.String.t
|
||||||
let showResult: result<T.expression, 'a> => string
|
let toStringResult: result<T.expression, 'a> => string
|
||||||
let parse_: (
|
let parse_: (
|
||||||
string,
|
string,
|
||||||
string => Result.t<'a, Reducer_ErrorValue.t>,
|
string => Result.t<'a, Reducer_ErrorValue.t>,
|
||||||
|
|
|
@ -72,52 +72,52 @@ let castNodeType = (node: node) =>
|
||||||
| _ => RETodo(`Argg, unhandled MathJsNode: ${node["type"]}`)->Error
|
| _ => RETodo(`Argg, unhandled MathJsNode: ${node["type"]}`)->Error
|
||||||
}
|
}
|
||||||
|
|
||||||
let rec show = (mathJsNode: mathJsNode): string => {
|
let rec toString = (mathJsNode: mathJsNode): string => {
|
||||||
let showValue = (a: 'a): string =>
|
let toStringValue = (a: 'a): string =>
|
||||||
if Js.typeof(a) == "string" {
|
if Js.typeof(a) == "string" {
|
||||||
`'${Js.String.make(a)}'`
|
`'${Js.String.make(a)}'`
|
||||||
} else {
|
} else {
|
||||||
Js.String.make(a)
|
Js.String.make(a)
|
||||||
}
|
}
|
||||||
|
|
||||||
let showNodeArray = (nodeArray: array<node>): string =>
|
let toStringNodeArray = (nodeArray: array<node>): string =>
|
||||||
nodeArray
|
nodeArray
|
||||||
->Belt.Array.map(a => showMathJsNode(a))
|
->Belt.Array.map(a => toStringMathJsNode(a))
|
||||||
->Extra.Array.interperse(", ")
|
->Extra.Array.interperse(", ")
|
||||||
->Js.String.concatMany("")
|
->Js.String.concatMany("")
|
||||||
|
|
||||||
let showFunctionNode = (fnode: functionNode): string =>
|
let toStringFunctionNode = (fnode: functionNode): string =>
|
||||||
`${fnode["fn"]}(${fnode["args"]->showNodeArray})`
|
`${fnode["fn"]}(${fnode["args"]->toStringNodeArray})`
|
||||||
|
|
||||||
let showObjectEntry = ((key: string, value: node)): string => `${key}: ${value->showMathJsNode}`
|
let toStringObjectEntry = ((key: string, value: node)): string => `${key}: ${value->toStringMathJsNode}`
|
||||||
|
|
||||||
let showObjectNode = (oNode: objectNode): string =>
|
let toStringObjectNode = (oNode: objectNode): string =>
|
||||||
`{${oNode["properties"]
|
`{${oNode["properties"]
|
||||||
->Js.Dict.entries
|
->Js.Dict.entries
|
||||||
->Belt.Array.map(entry => entry->showObjectEntry)
|
->Belt.Array.map(entry => entry->toStringObjectEntry)
|
||||||
->Extra.Array.interperse(", ")
|
->Extra.Array.interperse(", ")
|
||||||
->Js.String.concatMany("")}}`
|
->Js.String.concatMany("")}}`
|
||||||
|
|
||||||
let showIndexNode = (iNode: indexNode): string =>
|
let toStringIndexNode = (iNode: indexNode): string =>
|
||||||
iNode["dimensions"]
|
iNode["dimensions"]
|
||||||
->Belt.Array.map(each => showResult(each->castNodeType))
|
->Belt.Array.map(each => toStringResult(each->castNodeType))
|
||||||
->Js.String.concatMany("")
|
->Js.String.concatMany("")
|
||||||
|
|
||||||
switch mathJsNode {
|
switch mathJsNode {
|
||||||
| MjAccessorNode(aNode) => `${aNode["object"]->showMathJsNode}[${aNode["index"]->showIndexNode}]`
|
| MjAccessorNode(aNode) => `${aNode["object"]->toStringMathJsNode}[${aNode["index"]->toStringIndexNode}]`
|
||||||
| MjArrayNode(aNode) => `[${aNode["items"]->showNodeArray}]`
|
| MjArrayNode(aNode) => `[${aNode["items"]->toStringNodeArray}]`
|
||||||
| MjConstantNode(cNode) => cNode["value"]->showValue
|
| MjConstantNode(cNode) => cNode["value"]->toStringValue
|
||||||
| MjFunctionNode(fNode) => fNode->showFunctionNode
|
| MjFunctionNode(fNode) => fNode->toStringFunctionNode
|
||||||
| MjIndexNode(iNode) => iNode->showIndexNode
|
| MjIndexNode(iNode) => iNode->toStringIndexNode
|
||||||
| MjObjectNode(oNode) => oNode->showObjectNode
|
| MjObjectNode(oNode) => oNode->toStringObjectNode
|
||||||
| MjOperatorNode(opNode) => opNode->castOperatorNodeToFunctionNode->showFunctionNode
|
| MjOperatorNode(opNode) => opNode->castOperatorNodeToFunctionNode->toStringFunctionNode
|
||||||
| MjParenthesisNode(pNode) => `(${showMathJsNode(pNode["content"])})`
|
| MjParenthesisNode(pNode) => `(${toStringMathJsNode(pNode["content"])})`
|
||||||
| MjSymbolNode(sNode) => sNode["name"]
|
| MjSymbolNode(sNode) => sNode["name"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
and showResult = (rMathJsNode: result<mathJsNode, errorValue>): string =>
|
and toStringResult = (rMathJsNode: result<mathJsNode, errorValue>): string =>
|
||||||
switch rMathJsNode {
|
switch rMathJsNode {
|
||||||
| Error(e) => showError(e)
|
| Error(e) => errorToString(e)
|
||||||
| Ok(mathJsNode) => show(mathJsNode)
|
| Ok(mathJsNode) => toString(mathJsNode)
|
||||||
}
|
}
|
||||||
and showMathJsNode = node => node->castNodeType->showResult
|
and toStringMathJsNode = node => node->castNodeType->toStringResult
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
Irreducible values. Reducer does not know about those. Only used for external calls
|
Irreducible values. Reducer does not know about those. Only used for external calls
|
||||||
This is a configuration to to make external calls of those types
|
This is a configuration to to make external calls of those types
|
||||||
*/
|
*/
|
||||||
module AE = Reducer_Extra_Array
|
module Extra_Array = Reducer_Extra_Array
|
||||||
module ErrorValue = Reducer_ErrorValue
|
module ErrorValue = Reducer_ErrorValue
|
||||||
|
|
||||||
type rec expressionValue =
|
type rec expressionValue =
|
||||||
|
@ -15,7 +15,7 @@ type rec expressionValue =
|
||||||
|
|
||||||
type functionCall = (string, array<expressionValue>)
|
type functionCall = (string, array<expressionValue>)
|
||||||
|
|
||||||
let rec show = aValue =>
|
let rec toString = aValue =>
|
||||||
switch aValue {
|
switch aValue {
|
||||||
| EvBool(aBool) => Js.String.make(aBool)
|
| EvBool(aBool) => Js.String.make(aBool)
|
||||||
| EvNumber(aNumber) => Js.String.make(aNumber)
|
| EvNumber(aNumber) => Js.String.make(aNumber)
|
||||||
|
@ -23,38 +23,38 @@ let rec show = aValue =>
|
||||||
| EvSymbol(aString) => `:${aString}`
|
| EvSymbol(aString) => `:${aString}`
|
||||||
| EvArray(anArray) => {
|
| EvArray(anArray) => {
|
||||||
let args =
|
let args =
|
||||||
anArray->Belt.Array.map(each => show(each))->AE.interperse(", ")->Js.String.concatMany("")
|
anArray->Belt.Array.map(each => toString(each))->Extra_Array.interperse(", ")->Js.String.concatMany("")
|
||||||
`[${args}]`
|
`[${args}]`
|
||||||
}
|
}
|
||||||
| EvRecord(aRecord) => {
|
| EvRecord(aRecord) => {
|
||||||
let pairs =
|
let pairs =
|
||||||
aRecord
|
aRecord
|
||||||
->Js.Dict.entries
|
->Js.Dict.entries
|
||||||
->Belt.Array.map(((eachKey, eachValue)) => `${eachKey}: ${show(eachValue)}`)
|
->Belt.Array.map(((eachKey, eachValue)) => `${eachKey}: ${toString(eachValue)}`)
|
||||||
->AE.interperse(", ")
|
->Extra_Array.interperse(", ")
|
||||||
->Js.String.concatMany("")
|
->Js.String.concatMany("")
|
||||||
`{${pairs}}`
|
`{${pairs}}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let showWithType = aValue =>
|
let toStringWithType = aValue =>
|
||||||
switch aValue {
|
switch aValue {
|
||||||
| EvBool(_) => `Bool::${show(aValue)}`
|
| EvBool(_) => `Bool::${toString(aValue)}`
|
||||||
| EvNumber(_) => `Number::${show(aValue)}`
|
| EvNumber(_) => `Number::${toString(aValue)}`
|
||||||
| EvString(_) => `String::${show(aValue)}`
|
| EvString(_) => `String::${toString(aValue)}`
|
||||||
| EvSymbol(_) => `Symbol::${show(aValue)}`
|
| EvSymbol(_) => `Symbol::${toString(aValue)}`
|
||||||
| EvArray(_) => `Array::${show(aValue)}`
|
| EvArray(_) => `Array::${toString(aValue)}`
|
||||||
| EvRecord(_) => `Record::${show(aValue)}`
|
| EvRecord(_) => `Record::${toString(aValue)}`
|
||||||
}
|
}
|
||||||
|
|
||||||
let showArgs = (args: array<expressionValue>): string => {
|
let argsToString = (args: array<expressionValue>): string => {
|
||||||
args->Belt.Array.map(arg => arg->show)->AE.interperse(", ")->Js.String.concatMany("")
|
args->Belt.Array.map(arg => arg->toString)->Extra_Array.interperse(", ")->Js.String.concatMany("")
|
||||||
}
|
}
|
||||||
|
|
||||||
let showFunctionCall = ((fn, args)): string => `${fn}(${showArgs(args)})`
|
let toStringFunctionCall = ((fn, args)): string => `${fn}(${argsToString(args)})`
|
||||||
|
|
||||||
let showResult = x =>
|
let toStringResult = x =>
|
||||||
switch x {
|
switch x {
|
||||||
| Ok(a) => `Ok(${show(a)})`
|
| Ok(a) => `Ok(${toString(a)})`
|
||||||
| Error(m) => `Error(${ErrorValue.showError(m)})`
|
| Error(m) => `Error(${ErrorValue.errorToString(m)})`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user