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)) 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("MathJs parse", () => {
describe("literals operators paranthesis", () => { describe("literals operators paranthesis", () => {
testParse("1", "1") testParse("1", "1")
@ -40,15 +46,15 @@ describe("MathJs parse", () => {
}) })
describe("functions", () => { describe("functions", () => {
MySkip.testParse("identity(x) = x", "???") testParse("identity(x) = x", "identity = (x) => x")
MySkip.testParse("identity(x)", "???") testParse("identity(x)", "identity(x)")
}) })
describe("arrays", () => { describe("arrays", () => {
testDescriptionParse("empty", "[]", "[]") testDescriptionParse("empty", "[]", "[]")
testDescriptionParse("define", "[0, 1, 2]", "[0, 1, 2]") testDescriptionParse("define", "[0, 1, 2]", "[0, 1, 2]")
testDescriptionParse("define with strings", "['hello', 'world']", "['hello', 'world']") 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]") testDescriptionParse("index", "([0,1,2])[1]", "([0, 1, 2])[1]")
}) })
@ -58,11 +64,6 @@ describe("MathJs parse", () => {
}) })
describe("comments", () => { describe("comments", () => {
MySkip.testDescriptionParse("define", "# This is a comment", "???") testDescriptionParse("define", "1 # This is a comment", "1")
})
describe("if statement", () => {
// TODO Tertiary operator instead
MySkip.testDescriptionParse("define", "if (true) { 1 } else { 0 }", "???")
}) })
}) })

View File

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