mathjs parse assignment

This commit is contained in:
Umur Ozkul 2022-04-05 23:03:32 +02:00
parent cd7a4b748e
commit 63a765a4fe
2 changed files with 15 additions and 4 deletions

View File

@ -28,8 +28,8 @@ describe("MathJs parse", () => {
describe("variables", () => { describe("variables", () => {
testParse("x = 1", "x = 1") testParse("x = 1", "x = 1")
skipTestParse("x", "???") testParse("x", "x")
skipTestParse("x = 1; x", "???") testParse("x = 1; x", "{x = 1; x}")
}) })
describe("functions", () => { describe("functions", () => {

View File

@ -7,7 +7,8 @@ open Reducer_ErrorValue
type node = {"type": string, "isNode": bool, "comment": string} type node = {"type": string, "isNode": bool, "comment": string}
type arrayNode = {...node, "items": array<node>} type arrayNode = {...node, "items": array<node>}
//blockNode type block = {"node": node}
type blockNode = {...node, "blocks": array<block>}
//conditionalNode //conditionalNode
type constantNode = {...node, "value": unit} type constantNode = {...node, "value": unit}
//functionAssignmentNode //functionAssignmentNode
@ -31,6 +32,7 @@ external castArrayNode: node => arrayNode = "%identity"
external castAssignmentNode: node => assignmentNode = "%identity" external castAssignmentNode: node => assignmentNode = "%identity"
external castAssignmentNodeWAccessor: node => assignmentNodeWAccessor = "%identity" external castAssignmentNodeWAccessor: node => assignmentNodeWAccessor = "%identity"
external castAssignmentNodeWIndex: node => assignmentNodeWIndex = "%identity" external castAssignmentNodeWIndex: node => assignmentNodeWIndex = "%identity"
external castBlockNode: node => blockNode = "%identity"
external castConstantNode: node => constantNode = "%identity" external castConstantNode: node => constantNode = "%identity"
external castFunctionNode: node => functionNode = "%identity" external castFunctionNode: node => functionNode = "%identity"
external castIndexNode: node => indexNode = "%identity" external castIndexNode: node => indexNode = "%identity"
@ -56,6 +58,7 @@ type mathJsNode =
| MjAccessorNode(accessorNode) | MjAccessorNode(accessorNode)
| MjArrayNode(arrayNode) | MjArrayNode(arrayNode)
| MjAssignmentNode(assignmentNode) | MjAssignmentNode(assignmentNode)
| MjBlockNode(blockNode)
| MjConstantNode(constantNode) | MjConstantNode(constantNode)
| MjFunctionNode(functionNode) | MjFunctionNode(functionNode)
| MjIndexNode(indexNode) | MjIndexNode(indexNode)
@ -67,7 +70,7 @@ type mathJsNode =
let castNodeType = (node: node) => { let castNodeType = (node: node) => {
let decideAssignmentNode = node => { let decideAssignmentNode = node => {
let iNode = node->castAssignmentNodeWIndex let iNode = node->castAssignmentNodeWIndex
if Js.Null.test(iNode["index"]) && iNode["object"]["type"] == "SymbolNode" { if Js.null == iNode["index"] && iNode["object"]["type"] == "SymbolNode" {
node->castAssignmentNode->MjAssignmentNode->Ok node->castAssignmentNode->MjAssignmentNode->Ok
} else { } else {
RESyntaxError("Assignment to index or property not supported")->Error RESyntaxError("Assignment to index or property not supported")->Error
@ -78,6 +81,7 @@ let castNodeType = (node: node) => {
| "AccessorNode" => node->castAccessorNode->MjAccessorNode->Ok | "AccessorNode" => node->castAccessorNode->MjAccessorNode->Ok
| "ArrayNode" => node->castArrayNode->MjArrayNode->Ok | "ArrayNode" => node->castArrayNode->MjArrayNode->Ok
| "AssignmentNode" => node->decideAssignmentNode | "AssignmentNode" => node->decideAssignmentNode
| "BlockNode" => node->castBlockNode->MjBlockNode->Ok
| "ConstantNode" => node->castConstantNode->MjConstantNode->Ok | "ConstantNode" => node->castConstantNode->MjConstantNode->Ok
| "FunctionNode" => node->castFunctionNode->MjFunctionNode->Ok | "FunctionNode" => node->castFunctionNode->MjFunctionNode->Ok
| "IndexNode" => node->castIndexNode->MjIndexNode->Ok | "IndexNode" => node->castIndexNode->MjIndexNode->Ok
@ -123,12 +127,19 @@ let rec toString = (mathJsNode: mathJsNode): string => {
let toStringSymbolNode = (sNode: symbolNode): string => sNode["name"] let toStringSymbolNode = (sNode: symbolNode): string => sNode["name"]
let toStringBlocks = (blocks: array<block>): string =>
blocks
->Belt.Array.map(each => each["node"]->castNodeType->toStringResult)
->Extra.Array.interperse("; ")
->Js.String.concatMany("")
switch mathJsNode { switch mathJsNode {
| MjAccessorNode(aNode) => | MjAccessorNode(aNode) =>
`${aNode["object"]->toStringMathJsNode}[${aNode["index"]->toStringIndexNode}]` `${aNode["object"]->toStringMathJsNode}[${aNode["index"]->toStringIndexNode}]`
| MjArrayNode(aNode) => `[${aNode["items"]->toStringNodeArray}]` | MjArrayNode(aNode) => `[${aNode["items"]->toStringNodeArray}]`
| MjAssignmentNode(aNode) => | MjAssignmentNode(aNode) =>
`${aNode["object"]->toStringSymbolNode} = ${aNode["value"]->toStringMathJsNode}` `${aNode["object"]->toStringSymbolNode} = ${aNode["value"]->toStringMathJsNode}`
| MjBlockNode(bNode) => `{${bNode["blocks"]->toStringBlocks}}`
| MjConstantNode(cNode) => cNode["value"]->toStringValue | MjConstantNode(cNode) => cNode["value"]->toStringValue
| MjFunctionNode(fNode) => fNode->toStringFunctionNode | MjFunctionNode(fNode) => fNode->toStringFunctionNode
| MjIndexNode(iNode) => iNode->toStringIndexNode | MjIndexNode(iNode) => iNode->toStringIndexNode