MathJs functionAssingmentNode parsed, comments passed (tested)

Just found out that comments were already done
This commit is contained in:
Umur Ozkul 2022-04-23 17:55:34 +02:00
parent 99906446c5
commit a915e68049
2 changed files with 19 additions and 11 deletions

View File

@ -18,6 +18,12 @@ module MySkip = {
Skip.test(desc, () => expectParseToBe(expr, answer))
}
module MyOnly = {
let testParse = (expr, answer) => Only.test(expr, () => expectParseToBe(expr, answer))
let testDescriptionParse = (desc, expr, answer) =>
Only.test(desc, () => expectParseToBe(expr, answer))
}
describe("MathJs parse", () => {
describe("literals operators paranthesis", () => {
testParse("1", "1")
@ -40,15 +46,15 @@ describe("MathJs parse", () => {
})
describe("functions", () => {
MySkip.testParse("identity(x) = x", "???")
MySkip.testParse("identity(x)", "???")
testParse("identity(x) = x", "identity = (x) => x")
testParse("identity(x)", "identity(x)")
})
describe("arrays", () => {
testDescriptionParse("empty", "[]", "[]")
testDescriptionParse("define", "[0, 1, 2]", "[0, 1, 2]")
testDescriptionParse("define with strings", "['hello', 'world']", "['hello', 'world']")
MySkip.testParse("range(0, 4)", "range(0, 4)")
testParse("range(0, 4)", "range(0, 4)")
testDescriptionParse("index", "([0,1,2])[1]", "([0, 1, 2])[1]")
})
@ -58,11 +64,6 @@ describe("MathJs parse", () => {
})
describe("comments", () => {
MySkip.testDescriptionParse("define", "# This is a comment", "???")
})
describe("if statement", () => {
// TODO Tertiary operator instead
MySkip.testDescriptionParse("define", "if (true) { 1 } else { 0 }", "???")
testDescriptionParse("define", "1 # This is a comment", "1")
})
})

View File

@ -11,11 +11,10 @@ type block = {"node": node}
type blockNode = {...node, "blocks": array<block>}
//conditionalNode
type constantNode = {...node, "value": unit}
//functionAssignmentNode
type functionAssignmentNode = {...node, "name": string, "params": array<string>, "expr": node}
type indexNode = {...node, "dimensions": array<node>}
type objectNode = {...node, "properties": Js.Dict.t<node>}
type accessorNode = {...node, "object": node, "index": indexNode, "name": string}
type parenthesisNode = {...node, "content": node}
//rangeNode
//relationalNode
@ -33,6 +32,7 @@ external castAssignmentNodeWAccessor: node => assignmentNodeWAccessor = "%identi
external castAssignmentNodeWIndex: node => assignmentNodeWIndex = "%identity"
external castBlockNode: node => blockNode = "%identity"
external castConstantNode: node => constantNode = "%identity"
external castFunctionAssignmentNode: node => functionAssignmentNode ="%identity"
external castFunctionNode: node => functionNode = "%identity"
external castIndexNode: node => indexNode = "%identity"
external castObjectNode: node => objectNode = "%identity"
@ -59,6 +59,7 @@ type mathJsNode =
| MjAssignmentNode(assignmentNode)
| MjBlockNode(blockNode)
| MjConstantNode(constantNode)
| MjFunctionAssignmentNode(functionAssignmentNode)
| MjFunctionNode(functionNode)
| MjIndexNode(indexNode)
| MjObjectNode(objectNode)
@ -82,6 +83,7 @@ let castNodeType = (node: node) => {
| "AssignmentNode" => node->decideAssignmentNode
| "BlockNode" => node->castBlockNode->MjBlockNode->Ok
| "ConstantNode" => node->castConstantNode->MjConstantNode->Ok
| "FunctionAssignmentNode" => node->castFunctionAssignmentNode->MjFunctionAssignmentNode->Ok
| "FunctionNode" => node->castFunctionNode->MjFunctionNode->Ok
| "IndexNode" => node->castIndexNode->MjIndexNode->Ok
| "ObjectNode" => node->castObjectNode->MjObjectNode->Ok
@ -118,6 +120,10 @@ let rec toString = (mathJsNode: mathJsNode): string => {
->Extra.Array.interperse(", ")
->Js.String.concatMany("")
let toStringFunctionAssignmentNode = (faNode: functionAssignmentNode): string => {
let paramNames = Js.Array2.toString(faNode["params"])
`${faNode["name"]} = (${paramNames}) => ${toStringMathJsNode(faNode["expr"])}`
}
let toStringFunctionNode = (fnode: functionNode): string =>
`${fnode->nameOfFunctionNode}(${fnode["args"]->toStringNodeArray})`
@ -152,6 +158,7 @@ let rec toString = (mathJsNode: mathJsNode): string => {
`${aNode["object"]->toStringSymbolNode} = ${aNode["value"]->toStringMathJsNode}`
| MjBlockNode(bNode) => `{${bNode["blocks"]->toStringBlocks}}`
| MjConstantNode(cNode) => cNode["value"]->toStringValue
| MjFunctionAssignmentNode(faNode) => faNode->toStringFunctionAssignmentNode
| MjFunctionNode(fNode) => fNode->toStringFunctionNode
| MjIndexNode(iNode) => iNode->toStringIndexNode
| MjObjectNode(oNode) => oNode->toStringObjectNode