From 63a765a4fe515f48d3e3efbbce3508d318c4ffb9 Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Tue, 5 Apr 2022 23:03:32 +0200 Subject: [PATCH] mathjs parse assignment --- .../Reducer_MathJs/Reducer_MathJsParse_test.res | 4 ++-- .../Reducer_MathJs/Reducer_MathJs_Parse.res | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_MathJs/Reducer_MathJsParse_test.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_MathJs/Reducer_MathJsParse_test.res index ea302776..4ec86785 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_MathJs/Reducer_MathJsParse_test.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_MathJs/Reducer_MathJsParse_test.res @@ -28,8 +28,8 @@ describe("MathJs parse", () => { describe("variables", () => { testParse("x = 1", "x = 1") - skipTestParse("x", "???") - skipTestParse("x = 1; x", "???") + testParse("x", "x") + testParse("x = 1; x", "{x = 1; x}") }) describe("functions", () => { diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_MathJs/Reducer_MathJs_Parse.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_MathJs/Reducer_MathJs_Parse.res index 15ef6e26..7f20bacf 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_MathJs/Reducer_MathJs_Parse.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_MathJs/Reducer_MathJs_Parse.res @@ -7,7 +7,8 @@ open Reducer_ErrorValue type node = {"type": string, "isNode": bool, "comment": string} type arrayNode = {...node, "items": array} -//blockNode +type block = {"node": node} +type blockNode = {...node, "blocks": array} //conditionalNode type constantNode = {...node, "value": unit} //functionAssignmentNode @@ -31,6 +32,7 @@ external castArrayNode: node => arrayNode = "%identity" external castAssignmentNode: node => assignmentNode = "%identity" external castAssignmentNodeWAccessor: node => assignmentNodeWAccessor = "%identity" external castAssignmentNodeWIndex: node => assignmentNodeWIndex = "%identity" +external castBlockNode: node => blockNode = "%identity" external castConstantNode: node => constantNode = "%identity" external castFunctionNode: node => functionNode = "%identity" external castIndexNode: node => indexNode = "%identity" @@ -56,6 +58,7 @@ type mathJsNode = | MjAccessorNode(accessorNode) | MjArrayNode(arrayNode) | MjAssignmentNode(assignmentNode) + | MjBlockNode(blockNode) | MjConstantNode(constantNode) | MjFunctionNode(functionNode) | MjIndexNode(indexNode) @@ -67,7 +70,7 @@ type mathJsNode = let castNodeType = (node: node) => { let decideAssignmentNode = node => { 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 } else { RESyntaxError("Assignment to index or property not supported")->Error @@ -78,6 +81,7 @@ let castNodeType = (node: node) => { | "AccessorNode" => node->castAccessorNode->MjAccessorNode->Ok | "ArrayNode" => node->castArrayNode->MjArrayNode->Ok | "AssignmentNode" => node->decideAssignmentNode + | "BlockNode" => node->castBlockNode->MjBlockNode->Ok | "ConstantNode" => node->castConstantNode->MjConstantNode->Ok | "FunctionNode" => node->castFunctionNode->MjFunctionNode->Ok | "IndexNode" => node->castIndexNode->MjIndexNode->Ok @@ -123,12 +127,19 @@ let rec toString = (mathJsNode: mathJsNode): string => { let toStringSymbolNode = (sNode: symbolNode): string => sNode["name"] + let toStringBlocks = (blocks: array): string => + blocks + ->Belt.Array.map(each => each["node"]->castNodeType->toStringResult) + ->Extra.Array.interperse("; ") + ->Js.String.concatMany("") + switch mathJsNode { | MjAccessorNode(aNode) => `${aNode["object"]->toStringMathJsNode}[${aNode["index"]->toStringIndexNode}]` | MjArrayNode(aNode) => `[${aNode["items"]->toStringNodeArray}]` | MjAssignmentNode(aNode) => `${aNode["object"]->toStringSymbolNode} = ${aNode["value"]->toStringMathJsNode}` + | MjBlockNode(bNode) => `{${bNode["blocks"]->toStringBlocks}}` | MjConstantNode(cNode) => cNode["value"]->toStringValue | MjFunctionNode(fNode) => fNode->toStringFunctionNode | MjIndexNode(iNode) => iNode->toStringIndexNode