ternary operator mathjs parse (tested)
This commit is contained in:
parent
3788cb0c9a
commit
4d801c0b10
|
@ -66,4 +66,9 @@ describe("MathJs parse", () => {
|
||||||
describe("comments", () => {
|
describe("comments", () => {
|
||||||
testDescriptionParse("define", "1 # This is a comment", "1")
|
testDescriptionParse("define", "1 # This is a comment", "1")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("ternary operator", () => {
|
||||||
|
testParse("1 ? 2 : 3", "ternary(1, 2, 3)")
|
||||||
|
testParse("1 ? 2 : 3 ? 4 : 5", "ternary(1, 2, ternary(3, 4, 5))")
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -9,7 +9,7 @@ type node = {"type": string, "isNode": bool, "comment": string}
|
||||||
type arrayNode = {...node, "items": array<node>}
|
type arrayNode = {...node, "items": array<node>}
|
||||||
type block = {"node": node}
|
type block = {"node": node}
|
||||||
type blockNode = {...node, "blocks": array<block>}
|
type blockNode = {...node, "blocks": array<block>}
|
||||||
//conditionalNode
|
type conditionalNode = {...node, "condition": node, "trueExpr": node, "falseExpr": node}
|
||||||
type constantNode = {...node, "value": unit}
|
type constantNode = {...node, "value": unit}
|
||||||
type functionAssignmentNode = {...node, "name": string, "params": array<string>, "expr": node}
|
type functionAssignmentNode = {...node, "name": string, "params": array<string>, "expr": node}
|
||||||
type indexNode = {...node, "dimensions": array<node>}
|
type indexNode = {...node, "dimensions": array<node>}
|
||||||
|
@ -31,6 +31,7 @@ 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 castBlockNode: node => blockNode = "%identity"
|
||||||
|
external castConditionalNode: node => conditionalNode = "%identity"
|
||||||
external castConstantNode: node => constantNode = "%identity"
|
external castConstantNode: node => constantNode = "%identity"
|
||||||
external castFunctionAssignmentNode: node => functionAssignmentNode = "%identity"
|
external castFunctionAssignmentNode: node => functionAssignmentNode = "%identity"
|
||||||
external castFunctionNode: node => functionNode = "%identity"
|
external castFunctionNode: node => functionNode = "%identity"
|
||||||
|
@ -58,6 +59,7 @@ type mathJsNode =
|
||||||
| MjArrayNode(arrayNode)
|
| MjArrayNode(arrayNode)
|
||||||
| MjAssignmentNode(assignmentNode)
|
| MjAssignmentNode(assignmentNode)
|
||||||
| MjBlockNode(blockNode)
|
| MjBlockNode(blockNode)
|
||||||
|
| MjConditionalNode(conditionalNode)
|
||||||
| MjConstantNode(constantNode)
|
| MjConstantNode(constantNode)
|
||||||
| MjFunctionAssignmentNode(functionAssignmentNode)
|
| MjFunctionAssignmentNode(functionAssignmentNode)
|
||||||
| MjFunctionNode(functionNode)
|
| MjFunctionNode(functionNode)
|
||||||
|
@ -82,6 +84,7 @@ let castNodeType = (node: node) => {
|
||||||
| "ArrayNode" => node->castArrayNode->MjArrayNode->Ok
|
| "ArrayNode" => node->castArrayNode->MjArrayNode->Ok
|
||||||
| "AssignmentNode" => node->decideAssignmentNode
|
| "AssignmentNode" => node->decideAssignmentNode
|
||||||
| "BlockNode" => node->castBlockNode->MjBlockNode->Ok
|
| "BlockNode" => node->castBlockNode->MjBlockNode->Ok
|
||||||
|
| "ConditionalNode" => node->castConditionalNode->MjConditionalNode->Ok
|
||||||
| "ConstantNode" => node->castConstantNode->MjConstantNode->Ok
|
| "ConstantNode" => node->castConstantNode->MjConstantNode->Ok
|
||||||
| "FunctionAssignmentNode" => node->castFunctionAssignmentNode->MjFunctionAssignmentNode->Ok
|
| "FunctionAssignmentNode" => node->castFunctionAssignmentNode->MjFunctionAssignmentNode->Ok
|
||||||
| "FunctionNode" => node->castFunctionNode->MjFunctionNode->Ok
|
| "FunctionNode" => node->castFunctionNode->MjFunctionNode->Ok
|
||||||
|
@ -157,6 +160,10 @@ let rec toString = (mathJsNode: mathJsNode): string => {
|
||||||
| MjAssignmentNode(aNode) =>
|
| MjAssignmentNode(aNode) =>
|
||||||
`${aNode["object"]->toStringSymbolNode} = ${aNode["value"]->toStringMathJsNode}`
|
`${aNode["object"]->toStringSymbolNode} = ${aNode["value"]->toStringMathJsNode}`
|
||||||
| MjBlockNode(bNode) => `{${bNode["blocks"]->toStringBlocks}}`
|
| MjBlockNode(bNode) => `{${bNode["blocks"]->toStringBlocks}}`
|
||||||
|
| MjConditionalNode(cNode) =>
|
||||||
|
`ternary(${toStringMathJsNode(cNode["condition"])}, ${toStringMathJsNode(
|
||||||
|
cNode["trueExpr"],
|
||||||
|
)}, ${toStringMathJsNode(cNode["falseExpr"])})`
|
||||||
| MjConstantNode(cNode) => cNode["value"]->toStringValue
|
| MjConstantNode(cNode) => cNode["value"]->toStringValue
|
||||||
| MjFunctionAssignmentNode(faNode) => faNode->toStringFunctionAssignmentNode
|
| MjFunctionAssignmentNode(faNode) => faNode->toStringFunctionAssignmentNode
|
||||||
| MjFunctionNode(fNode) => fNode->toStringFunctionNode
|
| MjFunctionNode(fNode) => fNode->toStringFunctionNode
|
||||||
|
|
Loading…
Reference in New Issue
Block a user