Support for objects #533

Merged
merged 40 commits into from Feb 27, 2016

Conversation

Projects
None yet
2 participants
Owner

josdejong commented Jan 5, 2016

I've implemented support for objects:

• In the expression parser you can now do:

math.eval('{a: 2, b: 3}')
math.eval('obj.prop')
math.eval('obj["prop"]')
math.eval('obj.prop = 4')
• The functions math.subset and math.format are extended with object/property support.

To be discussed:

I think we have to make one breaking change.

Right now, assignments in the expression parser are implemented such that they return the updated object or matrix rather than the assigned value. For matrices this made sense as you want to see the whole updated matrix as output in a maths application. But I think we have to distinguish the operation of the assignment, and the processing of output. All languages I know of return the assigned value, which also makes sense as you can chain multiple assignments. Example:

A = eye(3)
A[1,1] = 2        # math.js currently returns the the updated matrix A instead of 2

obj = {}
obj.prop = 42     # math.js currently returns obj instead of 42


What do you think?

josdejong added some commits Dec 4, 2015

 Added support for getting/setting object properties to Index and m… 
…ath.subset
 9317366 
 Fixed broken unit test 
 2ef1a96 
 Merge branch 'develop' into feature/objectsupport 
 90a9602 
 Merge branch 'develop' into feature/objectsupport 
 180f969 
 Getting/setting object properties working (still needs a refactoring … 
…step)
 2adf01e 
 Fixed a bug in the toString method of an IndexNode 
 476b75b 
 Some refactoring 
 050573f 
 Some refactoring 
 edf4e24 
 Implemented ObjectNode and object support in the expression parser 
 8d3d58e 
 Implemented dot notation for object properties 
 5ee4b81 
 The command line interface does no longer round BigNumbers to 14 digi… 
…ts as it does with numbers
 c792136 
 Implemented support for JSON objects in the function math.format 
 96891dd 
 Added docs and an example on objects 
 9955274 
 Merge branch 'develop' into feature/objectsupport 
Conflicts:
HISTORY.md
 61f6ab0 
Owner

josdejong commented on lib/expression/node/ObjectNode.js in 8d3d58eJan 3, 2016

 @FSMaxB I'm working on implementing support for objects in the expression parser (branch feature/objectsupport). I've introduced a new Node for this: ObjectNode. I'm not sure how we should format an (JSON like) object in LaTeX, do you have an idea?
Collaborator

FSMaxB replied Jan 4, 2016

 Hm, that's quite a hard problem. Some kind of tree would be nice, but that's probably not possible with MathJAX. Maybe something like this: \left\{\begin{matrix} \mathbf{key:} & \mathbf{value}\\ \mathbf{some\_object:} & \left\{\begin{matrix} \mathbf{key1:} & \mathbf{value1}\\ \mathbf{key2:} & \mathbf{value2}\\ \end{matrix}\right.\\ \end{matrix}\right. Or maybe just print regular JSON?
Owner

josdejong replied Jan 4, 2016

 Thanks for the feedback. I think we should be careful not to over-engineer this (after all, people can always override the default formatting if they want something special :) ). At least we should not rely on specific non-standard packages for indentation and stuff. How about something that looks more traditional, with newlines and indentation blocks? I'm only not sure how to do that with LaTeX, at least the following doesn't work: \{\newline \indent key: value\newline \}\newline
Collaborator

FSMaxB replied Jan 4, 2016

 Even if it did work, this would be problematic when nesting objects because this would have to keep track of the current indentation level to work properly. I am by no means a LaTeX expert, but I'll think about how this can be done. Maybe use the matrices from my former example code to achieve something similar to your indentation blocks.
Owner

josdejong replied Jan 4, 2016

 Ok thanks for looking into it. If it becomes to too complicated or fragile I think we should just keep it simple though.

FSMaxB and others added some commits Jan 4, 2016

 ObjectNode: prettier LaTeX output 
 a75abeb 
 Merge pull request #531 from FSMaxB/object-latex 
ObjectNode: prettier LaTeX output
 9eb1168 
 Format result 
 ace4611 
 Added some (skipped) tests 
 04d933a 
 Updated favicon 
 01a7713 
 Merge branch 'develop' into feature/objectsupport 
Conflicts:
HISTORY.md
 15b640f 
 Fixed #529: Matrix multiplication no longer squeezes scalar outputs (… 
…breaking change)
 3aa38ee 
 Assignments in the expression parser now return the assigned value ra… 
…ther than the created or updated object (see #533)
 1a1461c 
Owner

josdejong commented Jan 8, 2016

 I've now changed this behavior: assignments in the expression parser now return the assigned value rather than the created or updated object.

josdejong added some commits Jan 8, 2016

 Fixed non-working autocompletion of user defined variables in the com… 
…mand line interface
 9fc1453 
 Added name property to UpdateNode 
 b7bd078 
 CLI outputs the variable name of assignments 
 a177a5a 
 Improved handling of result sets 
 164ace3 
 Merged parseFunctions and parseIndex into parseAccessors. Restricted … 
…support for implicit multiplication, execute as accessors instead
 fad5d8c 
 Updated syntax.md 
 2b7c00e 

josdejong added some commits Feb 3, 2016

 Merge branch 'develop' into feature/objectsupport 
Conflicts:
HISTORY.md
 86a3af2 
 Updated history 
 8d9f476 

Closed

josdejong added some commits Feb 24, 2016

 Merge branch 'develop' into feature/objectsupport 
Conflicts:
HISTORY.md
 9498676 
 Large refactoring of nodes: AssignmentNode. UpdateNode, AccessorNode,… 
… IndexNode
 9173fbd 
 Implemented new AssignmentNode and everything around it. Changed matr… 
…ices not to clone elements when getting/setting a subset
 4d1cd06 
 More unit tests 
 0d6e752 
 Updated CLI for new AssignmentNode 
 ed0a3b9 
 Renamed Index.ranges to Index.dimensions 
 2869c37 
 Updated docs and history on expression parser nodes 
 dc1e800 
 Removed a few redundant clone operations 
 e0c82f1 
 Implemented read-only name-properties for AccessorNode and Assignment… 
…Node, improved the name property of FunctionNode
 89e1238 
 Store dot notation in IndexNode 
 a60a70e 

josdejong merged commit a60a70e into develop Feb 27, 2016 0 of 2 checks passed

0 of 2 checks passed

continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
continuous-integration/travis-ci/push The Travis CI build is in progress
Details