MCPcopy
hub / github.com/josdejong/mathjs / parseAssignment

Function parseAssignment

src/expression/parse.js:697–742  ·  view source on GitHub ↗

* Assignment of a function or variable, * - can be a variable like 'a=2.3' * - or a updating an existing variable like 'matrix(2,3:5)=[6,7,8]' * - defining a function like 'f(x) = x^2' * @return {Node} node * @private

(state)

Source from the content-addressed store, hash-verified

695 * @private
696 */
697 function parseAssignment (state) {
698 let name, args, value, valid
699
700 const node = parseConditional(state)
701
702 if (state.token === '=') {
703 if (isSymbolNode(node)) {
704 // parse a variable assignment like 'a = 2/3'
705 name = node.name
706 getTokenSkipNewline(state)
707 value = parseAssignment(state)
708 return new AssignmentNode(new SymbolNode(name), value)
709 } else if (isAccessorNode(node)) {
710 // parse a matrix subset assignment like 'A[1,2] = 4'
711 if (node.optionalChaining) {
712 throw createSyntaxError(state, 'Cannot assign to optional chain')
713 }
714 getTokenSkipNewline(state)
715 value = parseAssignment(state)
716 return new AssignmentNode(node.object, node.index, value)
717 } else if (isFunctionNode(node) && isSymbolNode(node.fn)) {
718 // parse function assignment like 'f(x) = x^2'
719 valid = true
720 args = []
721
722 name = node.name
723 node.args.forEach(function (arg, index) {
724 if (isSymbolNode(arg)) {
725 args[index] = arg.name
726 } else {
727 valid = false
728 }
729 })
730
731 if (valid) {
732 getTokenSkipNewline(state)
733 value = parseAssignment(state)
734 return new FunctionAssignmentNode(name, args, value)
735 }
736 }
737
738 throw createSyntaxError(state, 'Invalid left hand side of assignment operator =')
739 }
740
741 return node
742 }
743
744 /**
745 * conditional operation

Callers 7

parseBlockFunction · 0.85
parseConditionalFunction · 0.85
parseCustomNodesFunction · 0.85
parseAccessorsFunction · 0.85
parseRowFunction · 0.85
parseObjectFunction · 0.85
parseParenthesesFunction · 0.85

Calls 7

isSymbolNodeFunction · 0.90
isAccessorNodeFunction · 0.90
isFunctionNodeFunction · 0.90
parseConditionalFunction · 0.85
getTokenSkipNewlineFunction · 0.85
createSyntaxErrorFunction · 0.85
forEachMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…